Repeat a string: Difference between revisions

(Added Erlang version)
Line 236:
 
=={{header|Erlang}}==
This example uses list comprehensions to accumulate N copies of X:
<lang erlang>
repeat(X,N) ->
lists:flatten([ X || _ <- lists:seqduplicate(1,N,X)]).
</lang>
This example uses lists:flatmap to do the same:
<lang erlang>
repeat(X,N) ->
lists:flatmap(fun (_) -> X end, lists:seq(1,N)).
</lang>
 
This will duplicate a string or character N times to produce a new string.
The first version also works for characters or any other values (though deeplists will all be flattened). The second version requires that X be a list, anything else will result in a bad argument exception.
 
=={{header|Factor}}==
Anonymous user