Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top