A newbie's attempt to explain lambdas


C: Trying To Explain

A newbie's attempt to explain lambdas

A lambda is a function.

A function is an ordinary value, just like numbers, lists, and characters.

Just like how a list can be created with (list 'item 'item 'item), a function can be created with (lambda (input) body-expr).

(lambda (x y) (+ x y)) produces a function that takes two arguments and sends them to the + function. In a typical environment:

((lambda (x y) (+ x y)) 2 3)
; => 5


The way the argument list is given depends on the language. In Scheme-based languages, the first argument to lambda is:

Binding to an identifier

define is used to assign an identifier to a value: (define x 9) binds x to 9. In the same way, (define my-add (lambda (x y z) (+ x y z))) binds my-add to the value (lambda (x y z) (+ x y z)). Now my-add can be used like the lambda itself.

((lambda (x y z) (+ x y z)) 1 2 3)
; => 6

(define my-add
  (lambda (x y z) (+ x y z)))
(my-add 1 2 3)
; => 6

Shorthand for defining functions

define has a shorthand for creating functions: racket (define my-add (lambda (x y) (+ x y))) can be written as: racket (define (my-add x y) (+ x y))