Remove Once

Function Syntax (LM:RemoveOnce <x> <l>)
Current Version 1.0
Arguments
Symbol Type Description
x List or Atom Item to be removed
l List List from which to remove the first occurrence of item
Returns
Type Description
List Resultant list following the removal operation

Program Description

This subfunction will remove the first occurrence of an item from a list.

Recursive Version

Select all
;;--------------------=={ Remove Once }==---------------------;;
;;                                                            ;;
;;  Removes the first occurrence of an item from a list       ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  x - item to be removed                                    ;;
;;  l - list from which to remove first occurrence of item    ;;
;;------------------------------------------------------------;;
;;  Returns:  Resultant list following the removal            ;;
;;------------------------------------------------------------;;

(defun LM:RemoveOnce ( x l )
    (if l
        (if (equal x (car l))
            (cdr l)
            (cons (car l) (LM:RemoveOnce x (cdr l)))
        )
    )
)

Iterative Version

Select all
;;--------------------=={ Remove Once }==---------------------;;
;;                                                            ;;
;;  Removes the first occurrence of an item from a list       ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  x - item to be removed                                    ;;
;;  l - list from which to remove first occurrence of item    ;;
;;------------------------------------------------------------;;
;;  Returns:  Resultant list following the removal            ;;
;;------------------------------------------------------------;;

(defun LM:RemoveOnce ( x l / f )
    (setq f equal)
    (vl-remove-if '(lambda ( a ) (if (f a x) (setq f (lambda ( a b ) nil)))) l)
)

Example Function Call

_$ (LM:RemoveOnce 3 '(1 2 3 4 5 1 2 3 4 5))
(1 2 4 5 1 2 3 4 5)

textsize

increase · reset · decrease

Designed & Created by Lee Mac © 2010