Common Lisp equivalent of Haskell's replicate?
Asked Answered
M

2

5

replicate is a function that takes an integer and a sequence and returns the sequence repeated n times.

E.g. replicate 3 ["a"] returns ["a", "a", "a"]

Does Common Lisp have an equivalent function, or do I have to write one?

Menstruum answered 1/4, 2011 at 19:15 Comment(0)
S
5

(make-sequence 'list n :initial-element element)

HyperSpec

Stowaway answered 1/4, 2011 at 19:25 Comment(1)
Thanks! I know a lot of these helpful functions exist, just not their names.Menstruum
K
7

Use make-list

(make-list 3 :initial-element 'a)

It evaluates to

(A A A)
Karb answered 1/4, 2011 at 19:21 Comment(1)
maybe (make-list 3 :initial-element '(a))?Synapsis
S
5

(make-sequence 'list n :initial-element element)

HyperSpec

Stowaway answered 1/4, 2011 at 19:25 Comment(1)
Thanks! I know a lot of these helpful functions exist, just not their names.Menstruum

© 2022 - 2024 — McMap. All rights reserved.