Challenge: Write a routine that inserts an arbitrary item between every item in a list. If the list contains only one item it should return the list unaltered.
Example:
(plaque '* '(tooth tooth tooth))
Should return (tooth * tooth * tooth)
(plaque '* '(tooth))
Should return (tooth)
My stabs ...
;; iterative using foreach
(defun plaque ( x lst / result )
(cons (car lst)
(foreach item (reverse (cdr lst))
(setq result
(cons x (cons item result))
)
)
)
)
;; using mapcar
(defun plaque ( x lst )
(cons (car lst)
(apply 'append
(mapcar
'(lambda (item) (list x item))
(cdr lst)
)
)
)
)
;; recursive
(defun plaque ( x lst / tail )
(if lst
(if (setq tail (cdr lst))
(cons
(car lst)
(cons x (plaque x tail))
)
lst
)
)
)