4

Today I am trying to understand how let works in context of lambda calculus in Scheme / Racket, but I can't figure out how to write the equivalent of let as a lambda function.

I think that the general form of it should be something like this:

((lambda (p1 p2...) body) v1 v2...)

but definitely that's not a complete function definition.

Any ideas of correct/complete definition for that?

Thank you in advance!

2 Answers2

5

From R5RS, the definition of let is:

(define-syntax let
  (syntax-rules ()
    ((let ((name val) ...) body1 body2 ...)
      ((lambda (name ...) body1 body2 ...)
        val ...))
    ((let tag ((name val) ...) body1 body2 ...)
      ((letrec ((tag (lambda (name ...)
                       body1 body2 ...)))
        tag)
      val ...))))
user448810
  • 17,381
  • 4
  • 34
  • 59
5

Your example

((lambda (p1 p2 ...) body) v1 v2 ...)

is exactly what

(let ([p1 v1] [p2 v2] ...) body)

means.

You can turn your example into a macro like this:

#lang racket

(define-syntax my-let
  (syntax-rules ()
    [(_my-let ([p1 v1] [p2 v2] ...) body)
     ((lambda (p1 p2 ...) body) v1 v2 ...)]))

(my-let ([x 1]
         [y 2])
  (+ x y))

The result of the program is 3.

soegaard
  • 30,661
  • 4
  • 57
  • 106