【问题标题】:Named lambda in common lisp在 common lisp 中命名为 lambda
【发布时间】:2015-01-17 15:11:37
【问题描述】:

我需要写一个递归辅助函数,不需要给它全局作用域,但是我需要引用它来实现递归。

Emacs lisp 有fset,它分配给符号的功能单元。 common lisp 中的等价物是什么?

【问题讨论】:

标签: common-lisp


【解决方案1】:

我刚刚发现这是一个常见的用法,可以在alexandria 中使用。来自亚历山大代码:

(defmacro named-lambda (name lambda-list &body body)
  "Expands into a lambda-expression within whose BODY NAME denotes the corresponding function."
  `(labels ((,name ,lambda-list ,@body))
 #',name))

这是一个使用示例:

(let ((! ;; Everything can be a symbol :D
   ;; Definition here
   (named-lambda self (n)
         (case n
           (1 1)
           (otherwise (* n
                 (self (1- n)))))))) ;; Recurse here
  ;; How to call it
  (funcall ! 6))

【讨论】:

  • P.S.我只是想为 Lars Brinkhoff 的回答添加一个示例。
【解决方案2】:

恶意软件说了什么。

或者如果你真的需要一个命名的 lambda:

(defmacro named-lambda (name args &body body)
  `(labels ((,name ,args ,@body))
     #',name))

【讨论】:

    猜你喜欢
    • 2011-03-20
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    相关资源
    最近更新 更多