Reading — step 1 of 5
Read
~1 min readFunctions & Closures
Lambda (Functions)
(lambda (x y) (+ x y))
Creates an ANONYMOUS function. Equivalent to Python's lambda x, y: x + y.
((lambda (x y) (+ x y)) 3 4) -> 7
(define add (lambda (x y) (+ x y)))
(add 3 4) -> 7
Implementation:
python
The KEY insight: the procedure captures env. Later when called, the new environment has env as parent — so the function can access variables that existed when it was DEFINED.
This is lexical scoping + closures — the foundation of Lisp.
Common shorthand: (define (add x y) (+ x y)) is sugar for (define add (lambda (x y) (+ x y))).
In Scheme, named functions are just lambdas bound to names. There's no special "function definition" syntax.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…