[英]Strange recursive definition in Scheme
Consider the following Scheme definition: 考虑以下方案定义:
(define f (lambda () (procedure? f)))
Strangely, when I evaluate (f)
I get #t
. 奇怪的是,当我评估
(f)
我得到#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? 鉴于
f
尚未定义,内部lambda
求值什么?
There is no recursion involved here. 这里没有递归。
When you execute this code, a procedure f
is defined (not executed): 当您执行此代码时,将定义过程
f
(未执行):
> (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
:
> (f)
#t
To be recursive, the procedure would have to call itself using (f)
. 为了递归,该过程必须使用
(f)
进行调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.