Scheme: Why does evaluating this recursive function defined in letrec fail?

386 views Asked by At

I am writing a silly letrec in Scheme (DrRacket Pretty Big):

(letrec
    ((is-creative? 
      (lambda (writing)
        (if (null? writing)
            #f
            (is-creative? 
             (eval writing))))))
(is-creative? 
 (quote is-creative?)))

Syntax check was ok, but running it fails with:

reference to undefined identifier: is-creative?

The debugger says at the point of failure that:

is-creative? => #<procedure:is-creative?>

Can you please tell me what am I missing? Correction would be nice as well, but please no defines, not necessary though.

Thank you!

1

There are 1 answers

2
Nate C-K On BEST ANSWER

Eval does not see local variables. In the scope where the eval is running, is-creative? is bound as a local variable but, because it's inside the (letrec) and not after it, it hasn't been bound in the global scope yet. See the documentation for eval, which discusses this:

http://docs.racket-lang.org/guide/eval.html

I don't think you can do what you're trying to do with eval. I don't know the reason why you're trying to do it, so it's hard for me to suggest an alternative. You might try using (apply) instead, though.