I have this old one (similar):
; Function: ALE_AppendBeforeNth
;
; Version 1.00 - 15/11/2005
;
; Description:
; returns a copy of the list with a new item added before
; the item in the nth position (NthPos)
;
; Arguments:
; NthPos = Integer - nth like
; NewItm = An atom or list
; In_Lst = A list
; InRLst = Original list reversed
;
; Return Values:
; A list with a new item added before the item in the nth position
;
; Examples:
; (setq alist '((0 . A) (1 . B) (2 . C) (3 . D)))
;
; (ALE_AppendBeforeNth 0 "NEW" alist (reverse alist))
; Returns: ("NEW" (0 . A) (1 . B) (2 . C) (3 . D))
;
; (ALE_AppendBeforeNth 1 "NEW" alist (reverse alist))
; Returns: ((0 . A) "NEW" (1 . B) (2 . C) (3 . D))
;
; (ALE_AppendBeforeNth 3 "NEW" alist (reverse alist))
; Returns: ((0 . A) (1 . B) (2 . C) (3 . D) "NEW")
;
; (ALE_AppendBeforeNth 4 "NEW" alist (reverse alist))
; Returns: ((0 . A) (1 . B) (2 . C) (3 . D))
;
; (ALE_AppendBeforeNth 1 '(9 . Z) alist (reverse alist))
; Returns: ((0 . A) (9 . Z) (1 . B) (2 . C) (3 . D))
;
; (ALE_AppendBeforeNth 1 nil alist (reverse alist))
; Returns: ((0 . A) nil (1 . B) (2 . C) (3 . D))
;
(defun ALE_AppendBeforeNth (NthPos NewItm In_Lst InRLst / LstLng NthItm)
(cond
( (null In_Lst) nil )
( (zerop NthPos) (cons NewItm In_Lst) )
( (null (setq NthItm (nth NthPos In_Lst))) In_Lst )
( (zerop (setq LstLng (- (length In_Lst) (1+ NthPos))))
(append In_Lst (list NewItm))
)
( T
(while
(/=
NthPos
(length (setq InRLst (cdr (member NthItm InRLst))))
)
)
(while
(/=
LstLng
(length (setq In_Lst (cdr (member NthItm In_Lst))))
)
)
(append (reverse InRLst) (list NewItm) (cons NthItm In_Lst))
)
)
)