Remove Nth

Function Syntax (LM:RemoveNth <n> <l>)
Current Version 1.0
Arguments
Symbol Type Description
n Integer Zero based index of item to Remove
l List List from which to remove the item
Returns
Type Description
List List with item at nth index removed

Program Description

This subfunction will return the supplied list with the item at index n removed.

Recursive Version

Select all
;;----------------------=={ Remove Nth }==--------------------;;
;;                                                            ;;
;;  Removes the item at the nth index in a supplied list      ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  n - index of item to remove (zero based)                  ;;
;;  l - list from which item is to be removed                 ;;
;;------------------------------------------------------------;;
;;  Returns:  List with item at index n removed               ;;
;;------------------------------------------------------------;;

(defun LM:RemoveNth ( n l )
    (if (and l (< 0 n))
        (cons (car l) (LM:RemoveNth (1- n) (cdr l)))
        (cdr l)
    )
)

Iterative Version

Select all
;;----------------------=={ Remove Nth }==--------------------;;
;;                                                            ;;
;;  Removes the item at the nth index in a supplied list      ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  n - index of item to remove (zero based)                  ;;
;;  l - list from which item is to be removed                 ;;
;;------------------------------------------------------------;;
;;  Returns:  List with item at index n removed               ;;
;;------------------------------------------------------------;;

(defun LM:RemoveNth ( n l / i )
    (setq i -1)
    (vl-remove-if '(lambda ( x ) (= (setq i (1+ i)) n)) l)
)

Example Function Call

_$ (LM:RemoveNth 3 '("A" "B" "C" "D" "E" "F"))
("A" "B" "C" "E" "F")

textsize

increase · reset · decrease

Designed & Created by Lee Mac © 2010