为什么 lisp 函数评估错误的参数?
Why is the lisp function evaluating the wrong argument?
我定义了一个函数来重复函数调用:
(defun repeat (n f x)
(if (zerop n) x
(repeat ((- n 1) f (funcall f x)))))
现在我想申请cdr
:
(repeat (1 (function cdr) '(1 2 4 5 6 7)))
我明确提供 n=1
、f=cdr
和 x='(1 2 3 4 5 6 7)
。它应该应用 cdr
一次。这是我收到的错误消息:
Error: Funcall of 1 which is a non-function.
[condition type: TYPE-ERROR]
但是我有一个 funcall
的 cdr
,而不是 1
。
我正在使用 Franz 的 Allegro Lisp 免费版。
Lisp 中的函数调用语法是:
(<function> <arg1> <arg2> <arg3> ...)
所以表达式...
(1 (function cdr) '(1 2 4 5 6 7))
... 被评估为 "call the function 1
on the arguments cdr
and '(1 2 4 5 6 7)
"。
换句话说,你多了一组括号。尝试:
(repeat 1 (function cdr) '(1 2 4 5 6 7))
你的递归调用也存在同样的问题。
我定义了一个函数来重复函数调用:
(defun repeat (n f x)
(if (zerop n) x
(repeat ((- n 1) f (funcall f x)))))
现在我想申请cdr
:
(repeat (1 (function cdr) '(1 2 4 5 6 7)))
我明确提供 n=1
、f=cdr
和 x='(1 2 3 4 5 6 7)
。它应该应用 cdr
一次。这是我收到的错误消息:
Error: Funcall of 1 which is a non-function.
[condition type: TYPE-ERROR]
但是我有一个 funcall
的 cdr
,而不是 1
。
我正在使用 Franz 的 Allegro Lisp 免费版。
Lisp 中的函数调用语法是:
(<function> <arg1> <arg2> <arg3> ...)
所以表达式...
(1 (function cdr) '(1 2 4 5 6 7))
... 被评估为 "call the function 1
on the arguments cdr
and '(1 2 4 5 6 7)
"。
换句话说,你多了一组括号。尝试:
(repeat 1 (function cdr) '(1 2 4 5 6 7))
你的递归调用也存在同样的问题。