How to implement let as a lambda function in Scheme

半世苍凉 提交于 2019-12-25 14:46:33

问题


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


回答1:


let is a syntatic extension defined in terms of lambda. I don't think you can define it as a function. Take a look at the example from The Scheme Programming Language



来源:https://stackoverflow.com/questions/5059841/how-to-implement-let-as-a-lambda-function-in-scheme

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!