1

I'm currently studying Scheme with The Little Schemer and ran into an odd trouble. Here is my code:

(define rember
  (lambda (a lat)
    ((if (null? lat)
         '()
         (cond
           ((eq? a (car lat)) (cdr lat))
           (else (rember a (cdr lat))))))))

(rember 'aaa '(bbb aaa))

I used an "if" instead of "cond" in the textbook. When returning from the tail-recursion, it shows this error:

application: not a procedure;
 expected a procedure that can be applied to arguments
  given: '()
  arguments...: [none]

I guess this is because it treats '() in the if-statement as a function and the return value of the tail-recursion as its argument. But since the book doesn't give me so many details about the language, could you explain this a bit for me? (e.g. Is this in fact some kind of language feature? Is there any way that I can stick to "if" in this piece of code? And when can I use "if" safely?)

Thanks.

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
babel92
  • 767
  • 1
  • 11
  • 21
  • possible duplicate of [Racket PLAI Application not a Procedure](http://stackoverflow.com/questions/19022704/racket-plai-application-not-a-procedure) – Joshua Taylor Oct 13 '13 at 03:23
  • Thanks for your reminding. I did searched for it but could hardly figure out the correlation since I'm a rookie who just started today:). – babel92 Oct 13 '13 at 04:21
  • Generally, since it's best practice to include the complete error message in questions, searching for the error message in quotes will help you find it (since earlier users, assuming they followed practice) will have included it in their questions. – Joshua Taylor Oct 13 '13 at 11:56

1 Answers1

6

You have an extra set of parentheses around your if. This

((if (null? lat)
     '()
      (cond
        ((eq? a (car lat)) (cdr lat))
        (else (rember a (cdr lat))))))

should be this:

(if (null? lat)
    '()
     (cond
       ((eq? a (car lat)) (cdr lat))
       (else (rember a (cdr lat)))))

Those extra parentheses tell Scheme that you want to call the result of the if like a function, so you get the error saying that '() isn't a function.

David Young
  • 10,713
  • 2
  • 33
  • 47