1

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

(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))

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

有谁看到这个函数有什么问题?

4

1 回答 1

1
(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)))))
于 2012-11-28T17:23:11.293 回答