gpt4 book ai didi

list - 如何在 lisp 中将多个列表的元素合并为一个

转载 作者:太空宇宙 更新时间:2023-11-03 18:54:52 25 4
gpt4 key购买 nike

我正在尝试获取列表列表的列表(别担心,我会举个例子)并将每个最后一个最少的元素转换为一个。这是我到目前为止所做的:

(defun cost(state)
(let ((list_elements '()))
(dolist (element state)
(dolist (subElement element)
(setq list_elements (append list_elements (list subElement))))
finally (return list_elements))))

示例:

(list
(list
(list
(list 1 9 't121)
(list 1 10 't122))
(list
(list 2 10 't123)
(list 2 11 't124)))
(list
(list
(list 1 9 't121)
(list 1 11 't132))
(list
(list 2 11 't133)
(list 2 12 't134))))

所以,这应该返回((1 9 T121) (1 10 T122) (2 10 T123) (2 11 T124) (1 9 T121) (1 11 T132) (1 11 T132) (2 11 T133) (2 12 T134)) 它只返回 ((1 9 T121) (1 11 T132))

之后,我应该计算列表中不同元素的数量。

有人知道这个函数有什么问题吗?

最佳答案

(defun double-append (list)
(reduce #'append (reduce #'append list)))

;; or like this:
(defun mapcan-mapcon (list)
(mapcan #'append (mapcon #'car list)))

(double-append (list
(list
(list
(list 1 9 't121)
(list 1 10 't122))
(list
(list 2 10 't123)
(list 2 11 't124)))
(list
(list
(list 1 9 't121)
(list 1 11 't132))
(list
(list 2 11 't133)
(list 2 12 't134)))))

((1 9 T121) (1 10 T122) (2 10 T123) (2 11 T124) (1 9 T121) (1 11 T132)
(2 11 T133) (2 12 T134))

到目前为止,我可以根据预期结果判断,那一定是类似的结果。

;; Using Alexandria, just as an example, of how currying can save
;; some repetitive coding:
(ql:quickload "alexandria")
(defun curried-append ()
(let ((reducer (alexandria:curry #'reduce #'append)))
(alexandria:compose reducer reducer)))

(funcall
(curried-append)
(list
(list
(list
(list 1 9 't121)
(list 1 10 't122))
(list
(list 2 10 't123)
(list 2 11 't124)))
(list
(list
(list 1 9 't121)
(list 1 11 't132))
(list
(list 2 11 't133)
(list 2 12 't134)))))

关于list - 如何在 lisp 中将多个列表的元素合并为一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13610130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com