Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 23 |
Nodes: | 6 (0 / 6) |
Uptime: | 52:43:18 |
Calls: | 583 |
Files: | 1,139 |
D/L today: |
179 files (27,921K bytes) |
Messages: | 111,617 |
I want to read a list and return a list of cons pairs:
(a b c d e f) -> ((a . b) (c . d) (e . f))
(a b c d e f g) -> ((a . b) (c . d) (e . f) (g . ""))
This function kind of works:
(defun process-form-x (x)
"create a list of (key . value) pairs from a list"
(cond ((endp x) nil);; terminate on empty list
((not (endp (rest x))) ;; recurse if there are at least two left
( cons (cons (first x) (second x))
(process-form-x (rest (rest x)))))))
but if the list has an odd number of members, I want to pair the
last member with "". The function above just drops the last
member of a list with an odd number of members.
[snip]
Any ideas? Is using flet and labels the right approach? Should I
just be happy with the helper function calling the recursive
function and stop there? Should I use loop to walk through a list,
rather than try recursion?
How about (to answer your last question first):
(defun make-pairs (list)
(loop while list collect (cons (pop list) (or (pop list) ""))))