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) ""))))
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 54 |
| Nodes: | 6 (1 / 5) |
| Uptime: | 21:46:57 |
| Calls: | 742 |
| Files: | 1,218 |
| D/L today: |
6 files (8,794K bytes) |
| Messages: | 186,234 |
| Posted today: | 1 |