Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
October 29th, 2012, 05:37 PM
 Kyndar
Registered User

Join Date: Oct 2012
Posts: 2
Time spent in forums: 31 m 20 sec
Reputation Power: 0
DrRacket help [I need help please]

So i am working on an assignment for school and am pretty confused about racket. I know cond, cons, car, cdr, and a couple other basic operations, but I am having trouble wrapping them together into something useful. my problem is this:

I need to implement a Scheme function, RemoveFirst, that takes as arguments a natural number n and a list L and
removes the fi rst n elements of L. That is, the function returns a new list containing elements (n+1) through
the end of the input list L. If n is greater than the length of L, the function returns the empty list. If n is
negative, the function returns #f.
For example, (RemoveFirst 2 '(fee fi fo fum)) returns '(fo fum).

If someone can show me what this would look like, or give me a push in the right direction that would be super helpful. My biggest issue is getting the syntax right.

#2
October 29th, 2012, 07:21 PM
 Kyndar
Registered User

Join Date: Oct 2012
Posts: 2
Time spent in forums: 31 m 20 sec
Reputation Power: 0
Heres what i have so far

(define (removefirst n L)
(cond
((< n 0)(#f))
((= n 0)(L))
((> n (length L))(" "))
(else removefirst(- n 1)(cdr L))))

#3
October 30th, 2012, 04:12 AM
 Lux Perpetua
Contributing User

Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,939
Time spent in forums: 1 Month 1 Week 3 h 27 m 29 sec
Reputation Power: 1312
You basically have it, but as you foreshadowed, your syntax is completely wrong. First thing: as an expression, an unquoted list is a function call. Thus, (#f) is not the literal #f, but the function #f (which doesn't exist) evaluated with no arguments. This is an error. Similarly, (L) is not the list L, but the value of L considered as a function and evaluated with no arguments. Next: the empty list is (), not (" ") (which is a singleton list with its one element being a string with one character). Next:
Code:
`(else removefirst(- n 1)(cdr L))`
does not do what you think it does: when the 'else' clause is reached, the three expressions 'removefirst', '(- n 1)', and '(cdr L)' are evaluated in turn, and the value of (cdr L) is returned as the value of the cond expression. This is not what you want. As I stated earlier (and as you should know, since you used the syntax in your first post), to evaluate removefirst with the two arguments, you use the expression (removefirst (- n 1) (cdr L)). Finally, 'removefirst' and 'RemoveFirst' are not equivalent, since Scheme is case-sensitive.

 Viewing: Dev Shed Forums > Other > Beginner Programming > DrRacket help [I need help please]