Hi! I started yesterday programming in lisp and now I've got my first problem... Suppose that I have a list l1 and a list l2: how can I replace the nth element in list l1 with list l2? (The resulting list should be flat) Maybe it should be better modify the structure of l1 than create a brand new list with the features desired...
Thanks!!! piercarlo
P.S.: I use GCL 2.2.2
Two answers:
(a) (defun nsplice (l1 n l2) ;; this is destructive version
(when (< -1 n (length l1))
(let* ((prior (when (plusp n)
(nthcdr (1- n) l1)))
(target (or (cdr prior)
(nthcdr n l1)))
(remainder (cdr target)))
(when prior
(rplacd prior l2))
(rplacd (last l2) remainder)
(if prior l1 l2))))
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 418 |
Nodes: | 16 (2 / 14) |
Uptime: | 47:08:43 |
Calls: | 8,810 |
Calls today: | 6 |
Files: | 13,307 |
Messages: | 5,972,846 |