User Tools


Function SUBSTITUTE, SUBSTITUTE-IF, SUBSTITUTE-IF-NOT, NSUBSTITUTE, NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT

Syntax

  • substitute newitem olditem sequence &key from-end test test-not start end count keyresult-sequence
  • substitute-if newitem predicate sequence &key from-end start end count keyresult-sequence
  • substitute-if-not newitem predicate sequence &key from-end start end count keyresult-sequence
  • nsubstitute newitem olditem sequence &key from-end test test-not start end count keyresult-sequence
  • nsubstitute-if newitem predicate sequence &key from-end start end count keyresult-sequence
  • nsubstitute-if-not newitem predicate sequence &key from-end start end count keyresult-sequence

Arguments and Values

Description

substitute, substitute-if, and substitute-if-not return a copy of sequence in which each element that satisfies the test has been replaced with newitem.

nsubstitute, nsubstitute-if, and nsubstitute-if-not are like substitute, substitute-if, and substitute-if-not respectively, but they may modify sequence.

If sequence is a vector, the result is a vector that has the same actual array element type as sequence.

If sequence is a list, the result is a list.

count, if supplied, limits the number of elements altered; if more than count elements satisfy the test, then of these elements only the leftmost or rightmost, depending on from-end, are replaced, as many as specified by count.

If count is supplied and negative, the behavior is as if zero had been supplied instead.

If count is nil, all matching items are affected.

Supplying a from-end of true matters only when the count is provided (and non-nil); in that case, only the rightmost count elements satisfying the test are removed (instead of the leftmost).

predicate, test, and test-not might be called more than once for each sequence element, and their side effects can happen in any order.

The result of all these functions is a sequence of the same type as sequence that has the same elements except that those in the subsequence bounded by start and end and satisfying the test have been replaced by newitem.

substitute, substitute-if, and substitute-if-not return a sequence which can share with sequence or may be identical to the input sequence if no elements need to be changed.

nsubstitute and nsubstitute-if are required to setf any car (if sequence is a list) or aref (if sequence is a vector) of sequence that is required to be replaced with newitem. If sequence is a list, none of the cdrs of the top-level list can be modified.

Examples

(substitute #\. #\Space "0 2 4 6")

"0.2.4.6"

(substitute 9 4 '(1 2 4 1 3 4 5))

(1 2 9 1 3 9 5)

(substitute 9 4 '(1 2 4 1 3 4 5) :count 1)

(1 2 9 1 3 4 5)

(substitute 9 4 '(1 2 4 1 3 4 5) :count 1 :from-end t)

(1 2 4 1 3 9 5)

(substitute 9 3 '(1 2 4 1 3 4 5) :test #'>)

(9 9 4 9 3 4 5)

(substitute-if 0 #'evenp '((1) (2) (3) (4)) :start 2 :key #'car)

((1) (2) (3) 0)

(substitute-if 9 #'oddp '(1 2 4 1 3 4 5))

(9 2 4 9 9 4 9)

(substitute-if 9 #'evenp '(1 2 4 1 3 4 5) :count 1 :from-end t)

(1 2 4 1 3 9 5)

(defparameter *some-things* (list 'a 'car 'b 'cdr 'c))

*SOME-THINGS*

(nsubstitute-if "function was here" #'fboundp *some-things* :count 1 :from-end t)

(A CAR B "function was here" C)

*some-things*

(A CAR B "function was here" C)

(defparameter *alpha-tester* (copy-seq "ab "))

"ab "

(nsubstitute-if-not #\z #'alpha-char-p *alpha-tester*)

"abz"

*alpha-tester*

"abz"

Side Effects

nsubstitute, nsubstitute-if, and nsubstitute-if-not modify sequence.

Affected By

None.

Exceptional Situations

Should be prepared to signal an error of type type-error if sequence is not a proper sequence.

See Also

{\secref\ConstantModification},

{\secref\TraversalRules}

Notes

If sequence is a vector, the result might or might not be simple, and might or might not be identical to sequence.

The :test-not argument is deprecated.

The functions substitute-if-not and nsubstitute-if-not are deprecated.

nsubstitute and nsubstitute-if can be used in for-effect-only positions in code.

Because the side-effecting variants (e.g. nsubstitute) potentially change the path that is being traversed, their effects in the presence of shared or circular structure may vary in surprising ways when compared to their non-side-effecting alternatives. To see this, consider the following side-effect behavior, which might be exhibited by some implementations:

(defun test-it (fn) (let ((x (cons 'b nil))) (rplacd x x) (funcall fn 'a 'b x :count 1))) (test-it #'substitute) → (A . #1=(B . #1#)) (test-it #'nsubstitute) → (A . #1#)

\issue{MAPPING-DESTRUCTIVE-INTERACTION:EXPLICITLY-VAGUE} \issue{TEST-NOT-IF-NOT:FLUSH-ALL} \issue{TEST-NOT-IF-NOT:FLUSH-ALL} \issue{SUBSEQ-OUT-OF-BOUNDS} \issue{RANGE-OF-START-AND-END-PARAMETERS:INTEGER-AND-INTEGER-NIL} \issue{RANGE-OF-COUNT-KEYWORD:NIL-OR-INTEGER} \issue{RANGE-OF-COUNT-KEYWORD:NIL-OR-INTEGER} \issue{REMF-DESTRUCTION-UNSPECIFIED:X3J13-MAR-89} \issue{CONSTANT-MODIFICATION:DISALLOW}