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) ""))))
Gauche Scheme
(define (mpairs x)
(if (null? x) x
(acons (pop! x) (pop~ x "") (mpairs x))))
(mpairs '(a b c d))
===>
((a . b) (c . d))
(mpairs '(a b c d e))
===>
((a . b) (c . d) (e . ""))
Given:
(define-syntax pop~
(syntax-rules ()
[ (_ xs) (pop~ xs #f) ]
[ (_ xs default)
(if (pair? xs)
(begin0 (car xs) (set! xs (cdr xs)))
default) ] ))
--
[T]he problem is that lispniks are as cultish as any other devout group and basically fall down frothing at the mouth if they see [heterodoxy].
--- Kenny Tilton
The good news is, it's not Lisp that sucks, but Common Lisp. --- Paul Graham
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)