Repeat a string: Difference between revisions
Content added Content deleted
(Added Erlang version) |
|||
Line 236: | Line 236: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
This example uses list comprehensions to accumulate N copies of X: |
|||
<lang erlang> |
<lang erlang> |
||
repeat(X,N) -> |
repeat(X,N) -> |
||
lists:flatten( |
lists:flatten(lists:duplicate(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> |
</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}}== |
=={{header|Factor}}== |