common-lisp 定义接受函数和返回函数的函数
示例
一个简单的例子:
CL-USER> (defun make-apply-twice (fun)"return a new function that applies twice the function`fun' to its argument"
(lambda (x)
(funcall fun (funcall fun x))))
MAKE-APPLY-TWICE
CL-USER> (funcall (make-apply-twice #'1+) 3)
5
CL-USER> (let ((pow4 (make-apply-twice (lambda (x) (* x x)))))
(funcall pow4 3))
81
功能组合物的典型例子:(˚F ∘克∘ ħ)(X)= ˚F(克(ħ(X)):
CL-USER> (defun compose (&rest funs)"return a new function obtained by the functional compositions of the parameters"
(if (null funs)
#'identity
(let ((rest-funs (apply #'compose (rest funs))))
(lambda (x) (funcall (first funs) (funcall rest-funs x))))))
COMPOSE
CL-USER> (defun square (x) (* x x))
SQUARE
CL-USER> (funcall (compose #'square #'1+ #'square) 3)
100 ;; => equivalent to (square (1+ (square 3)))
以上是 common-lisp 定义接受函数和返回函数的函数 的全部内容, 来源链接: utcz.com/z/334597.html