Repeat a string: Difference between revisions

Content added Content deleted
(Added Maple implementation)
(Showed some different (but less efficient) solutions)
Line 558: Line 558:


=={{header|Maple}}==
=={{header|Maple}}==
There are many ways to do this in Maple. First, the "right" (most efficient) way is to use the supplied procedures for this purpose.
<lang Maple>
<lang Maple>
> use StringTools in
> use StringTools in
Line 567: Line 568:
"xxxxxxxxxxxxxxxxxxxx"
"xxxxxxxxxxxxxxxxxxxx"
</lang>
</lang>
These next two are essentially the same, but are less efficient (though still linear) because they create a sequence of 10 strings before concatenating them (with the built-in procedure cat) to form the result.
<lang Maple>
> cat( "abc" $ 10 );
"abcabcabcabcabcabcabcabcabcabc"

> cat( seq( "abc", i = 1 .. 10 ) );
"abcabcabcabcabcabcabcabcabcabc"
</lang>
You ''can'' build up a string in a loop, but this is highly inefficient (quadratic); don't do this.
<lang Maple>
> s := "":
> to 10 do s := cat( s, "abc" ) end: s;
"abcabcabcabcabcabcabcabcabcabc"
</lang>
If you need to build up a string incrementally, use a StringBuffer object, which keeps things linear.

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}}==