如何在Scheme中将let实现为lambda函数
作为练习,我尝试将 let 定义为 lambda 函数,如下所示:
(define let_as_lambda
(lambda (var)
(lambda (value body)
(var body) val)))
我希望这样调用它:
((let_as_lambda a) (3 (+ a 2)))
但是,无法将未绑定变量(在本例中为“a”)作为参数传递给 a功能。 (我知道它看起来有点奇怪,但我需要 let_as_lambda(var) 返回一个函数。)
任何人都可以告诉我如何做到这一点吗?任何建议表示赞赏。
事实上,仅使用这个 lambda 等价表达式:
(let ((p1 v1) (p2 v2)...) body) = ((lambda (p1 p2...) body) v1 v2...)
我什至无法让它工作:
(define let_as_lambda
(lambda (var val body)
((var body) val)))
调用者:(let_as_lambda a 3 (+ a 2))
没有得到相同的抱怨:
引用定义之前的标识符:a
As an exercise I am trying to define let as a lambda function something like this:
(define let_as_lambda
(lambda (var)
(lambda (value body)
(var body) val)))
And I am hoping to call it like this:
((let_as_lambda a) (3 (+ a 2)))
However there is no way to pass an unbound variable (in this case "a") as an argument to a function. (I know it looks a little strange but I need let_as_lambda(var) to return a function.)
Can anyone show me how to do this? Any advice is appreciated.
In fact, just using this lambda-equivalent expression:
(let ((p1 v1) (p2 v2)...) body) = ((lambda (p1 p2...) body) v1 v2...)
I can't even get this to work:
(define let_as_lambda
(lambda (var val body)
((var body) val)))
Called by: (let_as_lambda a 3 (+ a 2))
Without getting the same complaint:
reference to an identifier before its definition: a
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
let
是根据lambda
定义的语法扩展。我不认为你可以将它定义为一个函数。看一下 Scheme 编程语言 中的示例let
is a syntatic extension defined in terms oflambda
. I don't think you can define it as a function. Take a look at the example from The Scheme Programming Language