3 (defmacro closure-make (fun &rest fvs)
4 "Make closure from function FUN and free variable list FVS.
5 CAUTION: Do not assign to free variables."
6 (let* ((funv (make-symbol "funv"))
7 (args (make-symbol "args")))
10 (lambda (,funv ,args ,@fvs)
14 (defun closure-call (clo &rest args)
18 (apply (cadr clo) (car clo) args (cddr clo))))
20 (defun closure-compose (c1 c2)
23 If either C1 or C2 is nil, another one is returned.
24 If C1 and C2 is non-nil, C1 must be closure with one argument."
31 (closure-call c1 (apply 'closure-call c2 args)))
36 (setq c1 (let ((a 1)) (closure-make (lambda (b) (+ a b)) a)))
37 (closure-call c1 2) ; => 3
39 (let ((a 1)) (setq plus1 (closure-make (lambda (b) (+ a b)) a)))
40 (let ((a 2)) (setq plus2 (closure-make (lambda (b) (+ a b)) a)))
41 (setq plus3 (closure-compose plus1 plus2))
42 (closure-call plus3 4) ; => 7