lambdaspeech
::
closure
1
[
pages
][
login
][
load
]
_h1 closure _p {b Closure} is not mandatory in functional programming provided functions accept {b partial application}. For instance in Scheme we could write the [cons,car,cdr] structure using a closure {pre (def cons (lambda (x y) (lambda (z) (z x y)))) (def car (lambda (z) (z (lambda (x y) x)))) (def cdr (lambda (z) (z (lambda (x y) y)))) (car (cons '♥ '♠)) -> ♥ (cdr (cons '♥ '♠)) -> ♠ } _p The {b cons} function waits for 2 values and returns a function waiting for 1 function to be applied to the 2 values kept in a closure created by the {b cons} function. _p In '{lambda speech} we use partiel application {pre °° {def cons {lambda {x y z} {z x y}}} {def car {lambda {z} {z {lambda {x y} x}}}} {def cdr {lambda {z} {z {lambda {x y} y}}}} {car {cons ♥ ♠}} -> ♥ {cdr {cons ♥ ♠}} -> ♠ °°} _p The {b cons} function waits for 3 values but will be feeded with only 2. The result will be a function waiting for 1 function to be applied to these values stored in the function's body. Let's trace the {b car} application to a pair, replacing names by their lambda expressions {pre °° . {{lambda {z} {z {lambda {x y} x}}} {{lambda {x y z} {z x y}} ♥ ♠}} == ♥ -> {{lambda {z} {z {lambda {x y} x}}} {{lambda {y z} {z ♥ y}} ♠}} == ♥ -> {{lambda {z} {z {lambda {x y} x}}} {{lambda {z} {z ♥ ♠}}}} == ♥ -> {{lambda {z} {z {lambda {x y} x}}} {lambda {z} {z ♥ ♠}} } == ♥ -> {{lambda {z} {z ♥ ♠}} {lambda {x y} x}} == ♥ -> {{lambda {x y} x} ♥ ♠} == ♥ -> ♥ °°} _p It's just text substitutions. _p Note that in Scheme words must be quoted and not in '{lambda speech}. Another property of lambdas in '{lambda speech} is that they accept a number of values greater than the number of arguments. Extra arguments are simply gathrered in the last one. A kind of variadicity.
lambdaspeech v.20180812