October 29th, 2012, 05:37 PM
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 first 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.
Thanks in advance for your time and help!
October 29th, 2012, 07:21 PM
Heres what i have so far
(define (removefirst n L)
((< n 0)(#f))
((= n 0)(L))
((> n (length L))(" "))
(else removefirst(- n 1)(cdr L))))
October 30th, 2012, 04:12 AM
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:
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.
(else removefirst(- n 1)(cdr L))