I can’t find a way to use an Erlang library function to create a list consisting of an element E repeated N times. Sure, it’s a cinch to write one,
repeat(E, N) ->
repeat(E, N, []).
repeat(_E, 0, L) ->
L;
repeat(E, N, L) ->
repeat(E, N-1, [E|L]).
but I expected this to be a one-liner using some lists module function or something. Is there a way?
Edit: Ok, there’s this of course,
lists:map(fun(_) -> E end, lists:seq(1, N))
but that creates an extra list that is immediately discarded. Seems inefficient to me, but maybe I’m not thinking Erlang-idiomatically.
Edit 2: Turns out it was a stupid question. I just somehow didn’t see the duplicate function in the lists module.
I think there is a function in erlang can do it: