1

I'm trying to take a list of lists of lists (don't worry, I'll put an example) and convert the elements of each last least into one. This is what I've done so far:

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

Example:

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

So, this is supposed to return ((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)) And it is only returning ((1 9 T121) (1 11 T132))

After that, I'm supposed to count the number of different elements in the list.

Does anyone see what the problem is in this function?

0

1 Answer 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))

So far I could tell by the expected result, that must be something like it.

;; 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)))))
Sign up to request clarification or add additional context in comments.

3 Comments

That's almost it. That functions returns this: (1 9 T121 1 11 T132 2 11 T133 2 12 T134 3 11 T111 3 10 T001) but that's ok, found the solution
Do you know how I can find the number of different elements in a list?
I asked Google, and for the first time in my life, he didn't know the answer (or didn't want to show it o me)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.