问题
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