Re: Idiomatic lisp - loops



oversby@xxxxxxxxxxxxxx writes:

I'm trying to write a loop that inserts a dotted key/value pair in
order into a list of ordered
key/value pairs.

So far I've got a recursive solution:

(defun kv-insert (list elt)
(if (null list) '()
(let* ((h (car list))
(k1 (car elt))
(k2 (car h)))
(cond ((string< k1 k2) (cons elt list))
((string= k1 k2) (cons elt (cdr list)))
(t (cons h (kv-insert (cdr list) elt)))))))

I find this pretty comprehensible. Is it by design that if an element
has a key that's string> that the key of the last element of the list,
it's discarded?

(KV-INSERT '((a . 1) (b . 2)) '(c . 3))
((a . 1) (b . 2))
.