5

假设我有一个普通的闭包,像这个简单的样本:

(let ((alpha 0) #| etc. |# )
  (lambda ()
    (incf alpha)
    #| more code here |#
    alpha))

假设我(funcall)三次执行该闭包的实例,并且在第三次执行的中间,这个闭包想要将自己保存在某个地方(例如在哈希表中)。然后我暂时没有(funcall)这个实例。然后我从哈希表中检索这个实例并(funcall)再次检索,得到返回值 4。

闭包中的函数是如何引用自身的,所以它可以将自己保存在那个哈希表中?

编辑1:这是一个更详细的例子。我通过将闭包作为参数传递给自身来实现目标。但我希望闭包能够自行完成所有这些操作,而无需进行自我参数化。

 1 (defparameter *listeriosis* nil)
 2 (defparameter *a*
 3   (lambda ()
 4     (let ((count 0))
 5       (lambda (param1 param2 param3 self)
 6         (incf count)
 7         (when (= 3 count)
 8           (push self *listeriosis*)
 9           (push self *listeriosis*)
10           (push self *listeriosis*))
11         count))))
12 (let ((bee (funcall *a*)))
13   (princ (funcall bee 1 2 3 bee)) (terpri)
14   (princ (funcall bee 1 2 3 bee)) (terpri)
15   (princ (funcall bee 1 2 3 bee)) (terpri)
16   (princ (funcall bee 1 2 3 bee)) (terpri)
17   (princ (funcall bee 1 2 3 bee)) (terpri))
18 (princ "///") (terpri)
19 (princ (funcall (pop *listeriosis*) 1 2 3 nil)) (terpri)
20 (princ (funcall (pop *listeriosis*) 1 2 3 nil)) (terpri)
21 (princ (funcall (pop *listeriosis*) 1 2 3 nil)) (terpri)
1
2
3
4
5
///
6
7
8

编辑2:是的,我知道我可以使用宏将函数的名称作为其第一个参数,然后使用该宏而不是(funcall),但我仍然想知道如何让闭包引用它自己的实例。

编辑 3:响应 SK-logic 的友好建议,我做了以下事情,但它没有做我想要的。它将三个新的闭包推入堆栈,而不是对同一个闭包的三个引用。看看当我从堆栈中弹出它们时,调用的值是 1、1 和 1 而不是 6、7 和 8?

 1 (defparameter *listeriosis* nil)
 2 (defun Y (f) 
 3   ((lambda (x) (funcall x x)) 
 4    (lambda (y) 
 5      (funcall f (lambda (&rest args) 
 6                (apply (funcall y y) args))))))
 7 (defparameter *a*
 8   (lambda (self)
 9     (let ((count 0))
10       (lambda (param1 param2 param3)
11         (incf count)
12         (when (= 3 count)
13           (push self *listeriosis*)
14           (push self *listeriosis*)
15           (push self *listeriosis*))
16         count))))
17 (let ((bee (Y *a*)))
18   (princ (funcall bee 1 2 3 #| bee |# )) (terpri)
19   (princ (funcall bee 1 2 3 #| bee |# )) (terpri)
20   (princ (funcall bee 1 2 3 #| bee |# )) (terpri)
21   (princ (funcall bee 1 2 3 #| bee |# )) (terpri)
22   (princ (funcall bee 1 2 3 #| bee |# )) (terpri))
23 (princ "///") (terpri)
24 (princ (funcall (pop *listeriosis*) 1 2 3)) (terpri)
25 (princ (funcall (pop *listeriosis*) 1 2 3)) (terpri)
26 (princ (funcall (pop *listeriosis*) 1 2 3)) (terpri)
1
2
3
4
5
///
1
1
1

编辑 4: Jon O 的建议完全符合要求。这是代码和输出:

 1 (defparameter *listeriosis* nil)
 2 (defparameter *a*
 3   (lambda ()
 4     (let ((count 0))
 5       (labels ((self (param1 param2 param3)
 6                  (incf count)
 7                  (when (= 3 count)
 8                    (push (function self) *listeriosis*)
 9                    (push (function self) *listeriosis*)
10                    (push (function self) *listeriosis*))
11                  count))
12         (function self)))))
13 (let ((bee (funcall *a*)))
14   (princ (funcall bee 1 2 3)) (terpri)
15   (princ (funcall bee 1 2 3)) (terpri)
16   (princ (funcall bee 1 2 3)) (terpri)
17   (princ (funcall bee 1 2 3)) (terpri)
18   (princ (funcall bee 1 2 3)) (terpri))
19 (princ "///") (terpri)
20 (princ (funcall (pop *listeriosis*) 1 2 3)) (terpri)
21 (princ (funcall (pop *listeriosis*) 1 2 3)) (terpri)
22 (princ (funcall (pop *listeriosis*) 1 2 3)) (terpri)
1
2
3
4
5
///
6
7
8

编辑 5: Miron 的建议也达到了目标,实际上使代码更具可读性:

 1 (defmacro alambda (parms &body body)
 2   `(labels ((self ,parms ,@body))
 3     #'self))
 4 ;
 5 (defparameter *listeriosis* nil)
 6 (defparameter *a*
 7   (lambda ()
 8     (let ((count 0))
 9       (alambda (param1 param2 param3)
10         (incf count)
11         (when (= 3 count)
12           (push #'self *listeriosis*)
13           (push #'self *listeriosis*)
14           (push #'self *listeriosis*))
15         count))))
16 ;
17 (let ((bee (funcall *a*)))
18   (princ (funcall bee 1 2 3)) (terpri)
19   (princ (funcall bee 1 2 3)) (terpri)
20   (princ (funcall bee 1 2 3)) (terpri)
21   (princ (funcall bee 1 2 3)) (terpri)
22   (princ (funcall bee 1 2 3)) (terpri))
23 (princ "///") (terpri)
24 (princ (funcall (pop *listeriosis*) 1 2 3)) (terpri)
25 (princ (funcall (pop *listeriosis*) 1 2 3)) (terpri)
26 (princ (funcall (pop *listeriosis*) 1 2 3)) (terpri)
1
2
3
4
5
///
6
7
8
4

2 回答 2

7

我认为您不需要为自己定义 Y 组合器就可以做到这一点。内置labels表单将创建您需要的自引用绑定。根据HyperSpec

"labels等价于flet为标签定义的函数名称的范围包括函数定义本身以及主体。"

这是每个人最喜欢的玩具闭包示例,展示了本地定义的f闭包如何覆盖其自己的绑定:

(defun make-counter (n)
   (labels ((f () (values (incf n) (function f))))
     (function f)))

这将返回一个闭包,该闭包返回两个值:计数器的新值和它自己的函数值。示例使用:

CL-USER> (setq g (make-counter 5))
#<FUNCTION F NIL (BLOCK F (VALUES (INCF N) #'F))>
CL-USER> (multiple-value-bind (n q) (funcall g) (list n (funcall q)))
(6 7)

将其扩展到将闭包存储在数据结构中而不是返回它应该很简单。

于 2012-02-08T15:46:00.393 回答
5

那么alambda(也在 On Lisp 中)呢?

;; Graham's alambda
(defmacro alambda (parms &body body)
  `(labels ((self ,parms ,@body))
     #'self))
于 2012-02-08T18:47:35.530 回答