#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep 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.

    Thanks in advance for your time and help!
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep 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))))
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    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.

IMN logo majestic logo threadwatch logo seochat tools logo