2

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!

Gabriel Ščerbák
  • 18,240
  • 8
  • 37
  • 52
  • Thank you for repairing formatting of the code! – Gabriel Ščerbák Feb 18 '11 at 18:53
  • Another thing besides the combination of `letrec` and `eval` not working: I suppose your conditional should use `procedure?` instead of `null?`, otherwise you'ld get an infinite loop even if `eval` would work the way you were expecting. – Rörd Feb 18 '11 at 21:34
  • @Rörd: don't mind that, it was intended, I said this is silly... it is actually outline of an essay I am writing... – Gabriel Ščerbák Feb 18 '11 at 23:14

1 Answers1

2

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.

Nate C-K
  • 5,744
  • 2
  • 29
  • 45
  • Thanks, pity the solution would be quite nasty:/. – Gabriel Ščerbák Feb 18 '11 at 17:57
  • apply won't work for exactly the same reason, although the eror changes, because apply expects a procedure. My reason to write this non-practical, I just wanted a function, which will take a function name, evaluate its body recursively until "nothing" is left. I am aware that doesn't do that, but it is as close as I've got. – Gabriel Ščerbák Feb 18 '11 at 19:18