subst
 
 
 

Searches a list for an old item and returns a copy of the list with a new item substituted in place of every occurrence of the old item

(subst newitem olditem lst)

Arguments

newitem

An atom or list.

olditem

An atom or list.

lst

A list.

Return Values

A list, with newitem replacing all occurrences of olditem. If olditem is not found in lst, subst returns lst unchanged.

Examples

Command: (setq sample '(a b (c d) b))

(A B (C D) B)

Command: (subst 'qq 'b sample)

(A QQ (C D) QQ)

Command: (subst 'qq 'z sample)

(A B (C D) B)

Command: (subst 'qq '(c d) sample)

(A B QQ B)

Command: (subst '(qq rr) '(c d) sample)

(A B (QQ RR) B)

Command: (subst '(qq rr) 'z sample)

(A B (C D) B)

When used in conjunction with assoc, subst provides a convenient means of replacing the value associated with one key in an association list, as demonstrated by the following function calls.

Set variable who to an association list:

Command: (setq who '((first john) (mid q) (last public)))

((FIRST JOHN) (MID Q) (LAST PUBLIC))

The following sets old to (FIRST JOHN) and new to (FIRST J):

Command: (setq old (assoc 'first who) new '(first j))

(FIRST J)

Finally, replace the value of the first item in the association list:

Command: (subst new old who)

((FIRST J) (MID Q) (LAST PUBLIC))