Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Lisp

Idiom #44 Insert element in list

Insert the element x at position i in the list s. Further elements must be shifted to the right.

Inserting the element x at a given position in the list s
(defun ins (lst x i)
 (if (zerop i) (cons x lst)
     (cons (car lst) (ins (cdr lst) x (- i 1)))))

(setf s (ins s x i))
s.insert (s.begin () + i, x);

New implementation...