我想编写一个函数,它接受 2 个列表作为参数并返回它们在列表中的乘积。 像这样:
(3 4) (3 5 6) => (9 15 18 12 20 24)
这是我想出的代码,但我收到一个错误,告诉我我的 map 参数太少。
(defun multip (lst lst2)
;this is a function to flatten the result
(defun flatten (tree)
(let ((result '()))
(labels ((scan (item)
(if (listp item)
(map nil #'scan item)
(push item result))))
(scan tree))
(nreverse result)))
(flatten (map (lambda (i) (map (lambda (j) (* i j)) lst )) lst2))
)
(write (multip '(3 4 6) '(3 2) ))
我不明白我做错了什么。感谢您的评论。
最佳答案
如果创建平面列表,则不需要展开列表。
使用 MAPCAN:
CL-USER 4 > (flet ((mult (a b)
(mapcan #'(lambda (a1)
(mapcar (lambda (b1) (* a1 b1))
b))
a)))
(mult '(3 4) '(3 5 6)))
(9 15 18 12 20 24)
关于lisp - 将列表的每个元素与 lisp 中另一个列表的每个元素相乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40571735/