Skip to main content

make-sequence

make-sequence Function

Syntax:

make-sequence result-type size &key initial-element → sequence

Arguments and Values:

result-type—a sequence type specifier .

size—a non-negative integer .

initial-element—an object. The default is implementation-dependent.

sequence—a proper sequence.

Description:

Returns a sequence of the type result-type and of length size, each of the elements of which has been initialized to initial-element.

If the result-type is a subtype of list, the result will be a list.

If the result-type is a subtype of vector, then if the implementation can determine the element type specified for the result-type, the element type of the resulting array is the result of upgrading that element type; or, if the implementation can determine that the element type is unspecified (or *), the element type of the resulting array is t; otherwise, an error is signaled.

Examples:

(make-sequence ’list 0)() 
(make-sequence ’string 26 :initial-element #\.)
".........................."
(make-sequence(vector double-float) 2
:initial-element 1d0)
→ #(1.0d0 1.0d0)
(make-sequence(vector \* 2) 3) should signal an error
(make-sequence(vector \* 4) 3) should signal an error

Affected By:

The implementation.

Exceptional Situations:

The consequences are unspecified if initial-element is not an object which can be stored in the resulting sequence.

An error of type type-error must be signaled if the result-type is neither a recognizable subtype of list, nor a recognizable subtype of vector.

An error of type type-error should be signaled if result-type specifies the number of elements and size is different from that number.

See Also:

make-array, make-list

Notes:

(make-sequence ’string 5) (make-string 5)

Expanded Reference: make-sequence

Creating a list of a given size

make-sequence creates a new sequence of the specified type and length. All elements are initialized to the :initial-element value.

(make-sequence 'list 5 :initial-element 0)
=> (0 0 0 0 0)
(make-sequence 'list 0)
=> NIL

Creating a string

(make-sequence 'string 10 :initial-element #\.)
=> ".........."
(make-sequence 'string 3 :initial-element #\x)
=> "xxx"

Creating a vector

(make-sequence 'vector 4 :initial-element nil)
=> #(NIL NIL NIL NIL)
(make-sequence '(vector integer) 3 :initial-element 42)
=> #(42 42 42)

Without :initial-element

If :initial-element is not provided, the initial values of elements are implementation-dependent. It is good practice to always supply an initial element.

(length (make-sequence 'list 3))
=> 3

Practical example: creating a sequence template

(make-sequence 'string 26
:initial-element #\-)
=> "--------------------------"