开发者

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

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开发者_如何学JAVA-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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜