Repeat a string: Difference between revisions

Content added Content deleted
Line 1,293: Line 1,293:
Finally, note that strings and characters are not distinct datatypes in Maple; a character is just a string of length one.
Finally, note that strings and characters are not distinct datatypes in Maple; a character is just a string of length one.


=={{header|Mathematica}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>(* solution 1 *)
<lang Mathematica>(* solution 1 *)
rep[n_Integer,s_String]:=Apply[StringJoin,ConstantArray[s,{n}]]
rep[n_Integer,s_String]:=Apply[StringJoin,ConstantArray[s,{n}]]

(* solution 2 -- @@ is the infix form of Apply[] *)
(* solution 2 -- @@ is the infix form of Apply[] *)
rep[n_Integer,s_String]:=StringJoin@@Table[s,{n}]
rep[n_Integer,s_String]:=StringJoin@@Table[s,{n}]

(* solution 3 -- demonstrating another of the large number of looping constructs available *)
(* solution 3 -- demonstrating another of the large number of looping constructs available *)
rep[n_Integer,s_String]:=Nest[StringJoin[s, #] &,s,n-1]</lang>
rep[n_Integer,s_String]:=Nest[StringJoin[s, #] &,s,n-1]</lang>