How to create list of lists from one single list w

2019-09-20 09:54发布

问题:

I am new in Lisp programming and trying to create sublists from one single list in lisp with pair of odd and even from the list. for example: I have a list

ListA ("a" "b" "c" "d" "e" "f" "g" "h") 

now I want to convert into the following list:

enter code here
ListB  ( ("a" "b") ("c" "d") ("e" "f") ("g" "h") )

so always sublist will be generated with the value of ( (first second) (third fourth) (fifth sixth) ............)

I have tried with mutiple ways for example first take out odd item and even item separate and used the function (list (oddlist evenlist)) but not getting above expected values in the above ListB.Could someone please help me in this regard. Your help would be highly appreciated.

回答1:

This is actually very short with loop:

(loop for (x y) on '(a b c d e f) by #'cddr
      collect (list x y))
;=> ((A B) (C D) (E F))

This does give you a NIL in the last pair if you have an odd number of elements, but you didn't mention what should happen in that case:

(loop for (x y) on '(a b c d e f g ) by #'cddr
      collect (list x y))
;=> ((A B) (C D) (E F) (G NIL))


回答2:

You need to make a procedure that does the following:

  1. handle when argument is (). Usually the result is ()
  2. default case you handle one chain of two. eg. (cons (list "a" "b") recursive-call-here)

So the result of '("g" "h") becomes (cons (list "g" "h") ()) and if you add that to recursive-call-here backwards you end up with:

(cons (list "a" "b") 
      (cons (list "c" "d") 
            (cons (list "e" "f") 
                  (cons (list "g" "h") ())))) 
; ==> (("a" "b") ("c" "d") ("e" "f") ("g" "h"))


回答3:

If you already have the even elements separated from the odd ones as you seemed to suggest, the next step would be:

(mapcar #'list evenlist oddlist)

Which one comes first, evenlist or oddlist, depends on whether you started counting from 0 or 1.

Or, the whole problem can be tackled with a single loop expression:

(loop for head = '(a b c d e f g h i j k l m n o p) then (cddr head)
   until (null head)
     if (= (length head) 1) collect head
     else collect (subseq head 0 2))