Elisp 定义阿克曼函数
Elisp to define a Ackermann's function
我正在阅读SICP并参考它
Exercise 1.10. The following procedure computes a mathematical function called Ackermann's function.
(define (A x y)
(cond ((= y 0) 0)
((= x 0) (* 2 y))
((= y 1) 2)
(else (A (- x 1)
(A x (- y 1))))))
用elisp重写
(defun A (x y)
(cond ((= y 0) 0)
((= x 0) (* 2 y))
((= y 1) 2)
(else (A (- x 1)
(A x (- y 1))))))
(A 1 10)
报错:
A: Symbol’s value as variable is void: else
参考 elisp 的控制结构,因此在各种替代方案中丢失。
能否请您提供提示以使用 cond
和 else
定义该函数
在 Emacs Lisp 中,您使用 t
而不是 else
。在这种情况下,语言参考 是开始研究的好地方...https://www.gnu.org/software/emacs/manual/html_node/elisp/Conditionals.html:
Often we want to execute the last clause whenever none of the previous
clauses was successful. To do this, we use t
as the condition of the
last clause, like this: (t body-forms)
. The form t
evaluates to t
,
which is never nil
, so this clause never fails, provided the cond
gets
to it at all. For example:
(setq a 5)
(cond ((eq a 'hack) 'foo)
(t "default"))
⇒ "default"
我正在阅读SICP并参考它
Exercise 1.10. The following procedure computes a mathematical function called Ackermann's function.
(define (A x y)
(cond ((= y 0) 0)
((= x 0) (* 2 y))
((= y 1) 2)
(else (A (- x 1)
(A x (- y 1))))))
用elisp重写
(defun A (x y)
(cond ((= y 0) 0)
((= x 0) (* 2 y))
((= y 1) 2)
(else (A (- x 1)
(A x (- y 1))))))
(A 1 10)
报错:
A: Symbol’s value as variable is void: else
参考 elisp 的控制结构,因此在各种替代方案中丢失。
能否请您提供提示以使用 cond
和 else
在 Emacs Lisp 中,您使用 t
而不是 else
。在这种情况下,语言参考 是开始研究的好地方...https://www.gnu.org/software/emacs/manual/html_node/elisp/Conditionals.html:
Often we want to execute the last clause whenever none of the previous clauses was successful. To do this, we use
t
as the condition of the last clause, like this:(t body-forms)
. The formt
evaluates tot
, which is nevernil
, so this clause never fails, provided thecond
gets to it at all. For example:(setq a 5) (cond ((eq a 'hack) 'foo) (t "default")) ⇒ "default"