Strip a set of characters from a string: Difference between revisions

Content added Content deleted
(→‎{{header|Picat}}: split into subsections)
Line 1,856: Line 1,856:


=={{header|Picat}}==
=={{header|Picat}}==
===List comprehension===
Two implementations:
<lang Picat>stripchars(String, Chars) = [C : C in String, not(membchk(C,Chars))].</lang>
* a function using list comprehension: stripchars/2
* a predicate using recursion: stripchars2/2


===Recursion===
<lang Picat>go =>
<lang Picat>stripchars2(String,Chars, Res) =>
S = "She was a soul stripper. She took my heart!",
println(stripchars(S, "aei")),
stripchars2(S, "aei", S2),
println(S2),
nl.

% List comprehension
stripchars(String, Chars) = [C : C in String, not(membchk(C,Chars))].

% Recursion
stripchars2(String,Chars, Res) =>
stripchars2(String, Chars, [], Res).
stripchars2(String, Chars, [], Res).


Line 1,880: Line 1,869:
stripchars2([H|T], Chars, Res1, [H|Res]) :-
stripchars2([H|T], Chars, Res1, [H|Res]) :-
stripchars2(T, Chars, Res1, Res).</lang>
stripchars2(T, Chars, Res1, Res).</lang>

===Test===
<lang Picat>go =>
S = "She was a soul stripper. She took my heart!",
println(stripchars(S, "aei")),
stripchars2(S, "aei", S2),
println(S2),
nl.</lang>



{{out}}
{{out}}
<pre>Sh ws soul strppr. Sh took my hrt!
<pre>Sh ws soul strppr. Sh took my hrt!
Sh ws soul strppr. Sh took my hrt!</pre>
Sh ws soul strppr. Sh took my hrt!</pre>



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