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.
If you already have the even elements separated from the odd ones as you seemed to suggest, the next step would be:
Which one comes first,
evenlist
oroddlist
, depends on whether you started counting from 0 or 1.Or, the whole problem can be tackled with a single
loop
expression:This is actually very short with loop:
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:
You need to make a procedure that does the following:
(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: