【发布时间】:2018-09-02 02:43:21
【问题描述】:
我刚开始学习 lisp。我正在尝试用 Lisp 编写产品功能。该函数应采用任意参数 x,并返回 x 中包含的所有数值的乘积。它应该产生以下内容:
>(product 'x) -> 1
>(product '(x 5)) -> 5
>(product '((2 2)3)) -> 12
>(product '((a 3)(2 1))) -> 6
我能够做到以下几点:
(defun product (x)
"This function takes in an arbitrary parameter x, and returns the product of all numeric values contained within x."
(cond
((consp x) (* (car x)(product (cadr x))))
((numberp x) x)
(t 1)
)
)
这处理像
这样的情况(product '(2 5))-> 10
(product 'x) -> 1
但不适用于:
>(product '(x 5)) -> 5
>(product '((2 2)3)) -> 12
我不知道从这里去哪里。
【问题讨论】:
标签: function common-lisp multiplication