简体   繁体   中英

Strange recursive definition in Scheme

Consider the following Scheme definition:

(define f (lambda () (procedure? f)))

Strangely, when I evaluate (f) I get #t . The question is: why does this evaluation terminate? I was expecting it to loop indefinitely. What does the inner lambda evaluate to, given that f hasn't yet been defined?

There is no recursion involved here.

When you execute this code, a procedure f is defined (not executed):

> (define f (lambda () (procedure? f)))
> f
#<procedure:f>

When you then execute it, it checks if there is a procedure associated with the symbol f, which is true at that point in time, so it returns #t :

> (f)
#t

To be recursive, the procedure would have to call itself using (f) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM