Common Lisp包含许多高阶函数,这些函数是传递给参数的函数并调用它们。也许最根本的是funcall和apply:
CL-USER> (list 1 2 3) (1 2 3) CL-USER> (funcall #'list 1 2 3) (1 2 3) CL-USER> (funcall #'list 1 2 3 4 5) (1 2 3 4 5) CL-USER> (apply #'list '(1 2 3)) (1 2 3) CL-USER> (apply #'list 1 2 '(4 5)) (1 2 3 4 5) CL-USER> (apply #'+ 1 (list 2 3)) 6 CL-USER> (defun my-funcall (function &rest args) (apply function args)) MY-FUNCALL CL-USER> (my-funcall #'list 1 2 3) (1 2 3)
还有许多其他高阶函数,例如,将函数多次应用于列表的元素。
CL-USER> (map 'list #'/ '(1 2 3 4)) (1 1/2 1/3 1/4) CL-USER> (map 'vector #'+ '(1 2 3 4 5) #(5 4 3 2 10)) #(6 6 6 6 15) CL-USER> (reduce #'+ '(1 2 3 4 5)) 15 CL-USER> (remove-if #'evenp '(1 2 3 4 5)) (1 3 5)