【发布时间】:2021-04-11 17:11:12
【问题描述】:
经过一番思考,我理解了 Graham 的 ANSI Common Lisp 第 6.6 章(第 110 页)中描述的 compose 函数的工作原理:
(defun compose (&rest functions)
(destructuring-bind (fn . fns) (nreverse functions)
#'(lambda (&rest arg)
(reduce #'(lambda (x y) (funcall y x))
fns
:initial-value (apply fn arg)))))
(setf (symbol-function 'lst-gt10p)
(compose #'list
#'(lambda (x) (and (> x 10) t))))
(lst-gt10p 11)
但不知何故,我无法提供 compose 的递归定义。
例如这是递归实现的尝试:
(defun rec-compose (&rest functions)
(destructuring-bind (fn . fns) functions
#'(lambda (&rest args)
(cond
((null fns) (apply fn args))
(t (funcall fn
(apply #'rec-compose fns)))))))
(funcall (rec-compose #'list #'round #'sqrt) 11)
我们的想法是继续调用(funcall fn (apply #'rec-compse fns)),直到遇到基本情况(apply fn args)。然而,这返回的不是结果,而是另一个闭包..
有什么想法吗?
【问题讨论】:
标签: common-lisp