Inserting separators

A function is required to return a copy of a given list with separators added in between adjacent items, for example:

(SEPARATED-LIST '(1 -1 1 -1 1) 0)

=> (1 0 -1 0 1 0 -1 0 1)

This solution is incorrect due to a spurious separator at the end:

(DEFUN SEPARATED-LIST (ITEMS SEPARATOR)
  (LOOP FOR I IN ITEMS
        COLLECT I
        COLLECT SEPARATOR))
Filed under  //   iteration   sequences  

About

A growing compendium of erroneous Common Lisp code snippets by Ismail Ikram.

Lisp novices might try to correct these as a learning exercise. Need some help or just feel like nitpicking? Do post a comment and let me know.

Twitter