, , , . Common Lisp:
(defun fact-cps (n &optional (k
(if (zerop n)
(funcall k 1)
(fact-cps (- n 1)
(lambda (v)
(funcall k (* v n))))))
(fact-cps 10) ; ==> 3628800
, if, , . funcall - LISP -2 Common Lisp Scheme.
-, CPS:
(defun fmapcar (fun lst &optional (k
(if (not lst)
(funcall k lst)
(let ((r (funcall fun (car lst))))
(fmapcar fun
(cdr lst)
(lambda (x)
(funcall k (cons r x)))))))
(fmapcar
v ?
, , v. fmapcar, (fmapcar #'list '(1 2 3)),
;; base case calls the stacked lambdas with NIL as argument
((lambda (x) ; third iteration
((lambda (x) ; second iteration
((lambda (x) ; first iteration
(values (cons (list 1) x)))
(cons (list 2) x)))
(cons (list 3) x))
NIL)
, - , , , . , , , . , , .
-? , , ?
(), , -cps n k, . , , flet labels , -. , .
/ Lisp ?
, . funcall, apply. .