String prepend: Difference between revisions

Content added Content deleted
(Added XPL0 example.)
Line 1,258: Line 1,258:
<b>NB:</b> s = prepend(s,"Hello ") gives typecheck: s is {"Hello ",87'W',111'o',114'r',108'l',100'd'}, of length 6, rather than the "Hello World" of length 11 you probably wanted.<br>
<b>NB:</b> s = prepend(s,"Hello ") gives typecheck: s is {"Hello ",87'W',111'o',114'r',108'l',100'd'}, of length 6, rather than the "Hello World" of length 11 you probably wanted.<br>
&nbsp;&nbsp; &nbsp; &nbsp; - and likewise s = prepend("Hello ",s) is not only the wrong way round but dies with typecheck: s is {"World",72'H',101'e',108'l',108'l',111'o',32' '} (length 7).
&nbsp;&nbsp; &nbsp; &nbsp; - and likewise s = prepend("Hello ",s) is not only the wrong way round but dies with typecheck: s is {"World",72'H',101'e',108'l',108'l',111'o',32' '} (length 7).

=={{header|Picat}}==
As usual there are a couple ways of doing this. The most common is probable to use string concatenation (++), but append/3 might be useful if backtracking is needed.
<lang Picat>go =>
S = "123456789",
println(S),
S := "A" ++ S,
println(S),

% append
append("B",S,T),
S := T,
println(S),

% insert at position
S := insert(S,1,'C'), % note: must be a char to keep it a proper string
println(S),
% insert many characters
S := insert_all(S,1,"DE"),
println(S),
nl.</lang>

{{out}}
<pre>123456789
A123456789
BA123456789
CBA123456789
DECBA123456789</pre>



=={{header|PicoLisp}}==
=={{header|PicoLisp}}==