Subst Once
| Function Syntax | (LM:SubstOnce <a> <b> <l>) |
| Current Version | 1.0 |
| Donate |
| Arguments | ||
|---|---|---|
| Symbol | Type | Description |
| a | List or Atom | Item to be substituted |
| b | List or Atom | Item to be replaced |
| l | List | List in which to make the substitution |
| Returns | ||
| Type | Description | |
| List | Resultant list following the substitution | |
Program Description
This subfunction will substitute the first occurrence of supplied item ('b') for a new item ('a') in a supplied list.
Recursive Version
Select all
;;---------------------=={ Subst Once }==---------------------;; ;; ;; ;; Substitutes the first occurrence of item 'b' for item 'a' ;; ;;------------------------------------------------------------;; ;; Author: Lee Mac, Copyright © 2011 - www.lee-mac.com ;; ;;------------------------------------------------------------;; ;; Arguments: ;; ;; a - item to be substituted ;; ;; b - item to be replaced ;; ;; l - list in which to make the substitution ;; ;;------------------------------------------------------------;; ;; Returns: Resultant list following the substitution ;; ;;------------------------------------------------------------;; (defun LM:SubstOnce ( a b l ) (if l (if (equal b (car l)) (cons a (cdr l)) (cons (car l) (LM:SubstOnce a b (cdr l))) ) ) )
Iterative Version
Select all
;;---------------------=={ Subst Once }==---------------------;; ;; ;; ;; Substitutes the first occurrence of item 'b' for item 'a' ;; ;;------------------------------------------------------------;; ;; Author: Lee Mac, Copyright © 2011 - www.lee-mac.com ;; ;;------------------------------------------------------------;; ;; Arguments: ;; ;; a - item to be substituted ;; ;; b - item to be replaced ;; ;; l - list in which to make the substitution ;; ;;------------------------------------------------------------;; ;; Returns: Resultant list following the substitution ;; ;;------------------------------------------------------------;; (defun LM:SubstOnce ( a b l ) (mapcar '(lambda ( x ) (if (equal b x) (setq x a a nil b nil)) x) l) )
Example Function Call
_$ (LM:SubstOnce 10 3 '(1 2 3 4 5 1 2 3 4 5)) (1 2 10 4 5 1 2 3 4 5)
