RosettaCodeData/Task/Singly-linked-list-Element-insertion/EchoLisp/singly-linked-list-element-insertion.l
2023-07-01 13:44:08 -04:00

11 lines
380 B
Common Lisp

(define (insert-after lst target item)
(when (null? lst) (error "cannot insert in" null))
(let [(sub-list (member target lst))]
(if sub-list (set-cdr! sub-list (cons item (cdr sub-list))) ; make chirurgy if found
(nconc lst item)))) ; else append item
(define L '(a b))
(insert-after L 'a 'c)
L (a c b)
(insert-after L 'x 'y)
L (a c b y)