Skip to main content

fill

fill Function

Syntax:

fill sequence item &key start end → sequence

Arguments and Values:

sequence—a proper sequence.

item—a sequence.

start, end—bounding index designators of sequence. The defaults for start and end are 0 and nil, respectively.

Description:

Replaces the elements of sequence bounded by start and end with item.

Examples:

(fill (list 0 1 2 3 4 5) ’(444)) → ((444) (444) (444) (444) (444) (444))
(fill (copy-seq "01234") #\e :start 3) → "012ee"
(setq x (vector ’a ’b ’c ’d ’e)) → #(A B C D E)
(fill x ’z :start 1 :end 3) → #(A Z Z D E)
x → #(A Z Z D E)
(fill x ’p) → #(P P P P P)
x → #(P P P P P)

Side Effects:

Sequence is destructively modified.

Exceptional Situations:

Should be prepared to signal an error of type type-error if sequence is not a proper sequence. Should signal an error of type type-error if start is not a non-negative integer . Should signal an error of type type-error if end is not a non-negative integer or nil.

See Also:

replace, nsubstitute

Notes:

(fill sequence item) ≡(nsubstitute-if item (constantly t) sequence)

make-sequence

Expanded Reference: fill​

Filling an entire sequence​

fill destructively replaces every element in the sequence with the given item and returns the modified sequence.

(fill (list 1 2 3 4 5) 0)
=> (0 0 0 0 0)
(fill (copy-seq "hello") #\*)
=> "*****"

Filling a subrange with :start and :end​

Use :start and :end to fill only a portion of the sequence.

(let ((v (vector 'a 'b 'c 'd 'e)))
(fill v 'z :start 1 :end 3)
v)
=> #(A Z Z D E)

Filling part of a string​

(let ((str (copy-seq "abcdef")))
(fill str #\. :start 3))
=> "abc..."

The item can be any object​

The fill item does not need to match the original element types (for general sequences). Every position gets the same identical object.

(fill (list nil nil nil) '(x y))
=> (#1=(X Y) #1# #1#)

fill modifies the sequence in place​

fill is destructive -- it modifies and returns the original sequence, not a copy.

(let ((lst (list 1 2 3)))
(fill lst 0)
lst)
=> (0 0 0)