我目前正在尝试为一个类实现阶乘函数。我的函数接受一个参数n ,并列出 0, 1, 2, ... n的阶乘。
这就是我目前所拥有的:
这是我将要调用的函数,现在它只返回N的阶乘:
(defun factor3 (N)
(apply #'* (loop :for n :from 1 :below (+ 1 N) :collect n))
)
Run Code Online (Sandbox Code Playgroud)
这是一个辅助函数,返回从 1 到num的整数列表。Acc 在最初调用时将NIL作为空列表来累积值:
(defun my-seq (num acc)
(if (eq num 0)
acc
(my-seq (- num 1) (cons num acc)))
Run Code Online (Sandbox Code Playgroud)
这些函数中的每一个都按预期单独运行,现在我想将Factor3函数应用于从my-seq返回的列表中的每个成员,并且我的教授暗示了两个关键字:
使用apply
使用mapcar
然而,当我打电话时
(mapcar 'factor3 '(my-seq 5 NIL))
我明白了
*** - +: MY-SEQ is not a number
因此,我尝试将my-seq返回的列表保存到变量b,然后调用 …