gpt4 book ai didi

common-lisp - 在普通的 lisp 中命名为 lambda

转载 作者:行者123 更新时间:2023-12-01 11:35:00 25 4
gpt4 key购买 nike

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

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

最佳答案

我刚刚发现这是一个足够常见的 idion,可以出现在 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))

关于common-lisp - 在普通的 lisp 中命名为 lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28000829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com