This is my solution to Ex. 6 on p. 97 of Paul Graham's "ANSI Common
Lisp"
<QUOTE>
Define iterative and recursive versions of a function that takes an
object and a list, and returns a new list in which the object appears
between each pair of elements in the original list:
(intersprerse '- '(a b c d))
(A - B - C - C)
<\QUOTE>
(I'll just ask about the iterative solution I developed.)
;;;;Ex. 6
(defun intersperse (element list)
(let ((return-list (list (car list))))
(do ((rest-input-list (cdr list) (cdr rest-input-list))
(rest-return-list return-list (cddr rest-return-list)))
((not (consp rest-input-list)) return-list)
(setf (cdr rest-return-list)
(list element (car rest-input-list))))))
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 403 |
Nodes: | 16 (2 / 14) |
Uptime: | 43:25:34 |
Calls: | 8,407 |
Calls today: | 2 |
Files: | 13,171 |
Messages: | 5,905,019 |