0

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

3
  • Sorry, i do not understand you. You have the definition of let as a function already. Variables are bound by parameters. If you change the order in ((let_as_lambda a) (3 (+ a 2))) to something like ((let_as_lambda (a) (+ a 2)) 3) then let_as_lambda is the same as lambda. ... But if you want to build an interpreter then you need the concept of an environment as a mapping from names (strings) to values. But this needs a little bit of extra work. Commented Feb 20, 2011 at 22:32
  • Hi. I'll simplify my exercise a little. I already know that: (let ((p1 v1) (p2 v2)...) body) = ((lambda (p1 p2...) body) v1 v2...) and I would like to just write this as a new rule such as: (define let_as_lambda (lambda ... The problem occurs with any function call like: (let_as_lambda a 3 (+ a 2)) since a is being referenced before it is being defined -- the definition occuring in the body of let_as_lambda. Is there any way to pass "a" as an unbound variable or is there some other way to define let_as_lambda? Commented Feb 20, 2011 at 23:18
  • For simplicity: No, you can not pass an unbound variable as a paramameter to functions. There are some tricks but probably that is not what you want. Commented Feb 21, 2011 at 9:04

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.