wiki:Fundamentals/Closures

Blocks, Lambdas, Closures

Lists as closures

CDR of a List is itself a List. '() is a List AND an Atom NIL

* (atom nil)
T

To keep the closure property CDR of an empty list is the empty list.

* (cdr '())
NIL

which is an Atom and a List

* (listp nil)
T

Procedures as closures

capturing a symbol

(lambda (x)
   (lambda ()
      x))

capturing a variable

(lambda (n)
   (lambda (x)
      (+ x n)))

capturing a function

(lambda (f)
   (lambda (x)
      (f x)))

constant

((lambda xs 1))
(\x -> 1) 2

identity

((lambda x x) (lambda x x))
(\x -> x) (\x -> x)

currying

(((lambda (f)
   (lambda (x)
       (f x))) 1+) 2)

(((lambda (f)
   (lambda xs
      (apply f xs))) +) 1 2 3)

partial application

(define partial (lambda (f . as)
                   (lambda xs
                      (apply f (append as xs)))))
(define (uncurry f . xs)
    (if (null? xs)
        f
        (apply uncurry (f (car xs)) (cdr xs))))

some unification

(define (flip f g) (g f))

(define (foldl f a xs)
    (if (null? xs)
        a
        (foldl f (f (car xs) a) (cdr xs))))

(define (uncurry f . xs)
   (foldl flip f xs))
Last modified 2 years ago Last modified on Oct 19, 2018, 3:49:02 PM
Note: See TracWiki for help on using the wiki.