Idiomatic lisp - loops



Hi,

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)))))))

and an iterative solution:

(defun kv-insert (list elt)
(let ((k1 (car elt))
(acc '()))
(while (not (null list))
(let* ((h (car list))
(k2 (car h)))
(cond ((string< k1 k2)
(setq acc (append (reverse list)
(list elt)
acc))
(setq list '()))
((string= k1 k2)
(setq acc (append (reverse (cdr list))
(list elt)
acc))
(setq list '()))
(t (setq acc (cons h acc)))))
(setq list (cdr list)))
(reverse acc)))

neither of which I'm particularly happy with.

In scheme I would use letrec to avoid needing to pass the elt
parameter to each
loop iteration. Is there something similar in lisp or an alternative
idiomatic way
to write this loop?

(define (kv-insert l elt)
(letrec ((k1 (car elt))
(loop (lambda (l)
(if (null? l) '()
(let* ((h (car l))
(k2 (car h)))
(cond ((string< k1 k2) (cons elt l))
((string= k1 k2) (cons elt (cdr l)))
(else (cons h (loop (cdr l))))))))))
(loop l)))

Thanks,

Ian

.



Relevant Pages

  • Scotland western isles + coast
    ... Sharon and Tony and I did some fun and spectacular single-day loop road ... pretty sea views, road network has obvious cycle tours, and ... and get access to them by driving our car. ... for a holiday that combines pretty bicycling and pretty hiking (or serious ...
    (rec.bicycles.rides)
  • Re: (OT) For the trolley and train people
    ... of the lines but I'm pretty certain the driver moves to the other end. ... They have a power car at each end. ... run from one end of the tunnel to the other and back again have a single ... The terminal is located at the end of a loop connected to the route ...
    (alt.usage.english)
  • Re: Stupid Question-- Why not Open-loop?
    ... When my wife rides with me she things the system of my car and me is a closed-loop system. ... So when I watch what I'm doing while driving nails, the system of me and hammer is closed loop. ... Problems in maintaining stability in human-in-the-loop systems are of some concern in high-performance aircraft manufacture; you can have an aircraft that by itself has totally stable dynamics yet has enough delay from stick to airframe motion that it'll go unstable with any but a really good pilot in the hot seat. ...
    (sci.engr.control)
  • Re: Kia Picanto + red traffic lights
    ... traffic lights as they changed to red, so they stopped - all ok so far! ... There is some kind of 'loop' under the tarmac that detects ... cycle - clearly the car is too light to trigger the change... ... have to be very unlucky to be spotted by a police car, ...
    (uk.rec.driving)
  • sketch of union function problem
    ... from ANSI Common Lisp by Paul Graham.. ... Here, i try to loop through the second list, upon encountering an ... (dolist (obj y) ... (if (eql (car x) ...
    (comp.lang.lisp)