I've been looking for a way to have a predicate that establishes that all elements of a list are substrings delimited by a given string. Analogues in other languages include: 'delimiter'.join(list)
in Python, (mapconcat function sequence delimiter)
in Emacs Lisp, array.join('delimiter')
in JavaScript, and I'm sure there are more of the kind. Below are three different variants I came up with, but I've a strong feeling that I'm reinventing the wheel here:
join
join(_, [X], X) :- !.
join(Sep, [X | Xs], S) :-
join(Sep, Xs, Sx),
string_concat(Sep, Sx, Sy),
string_concat(X, Sy, S).
Seems to be the simplest, but I don't like that it is not tail-recursive and that it has an obvious repetitive pattern.
interleave
interleave([X], _, X) :- !.
interleave([X | Xs], Glue, Result) :-
interleave(Xs, Glue, Previous),
format(atom(Result), "~p~p~p", [X, Glue, Previous]).
Still not tail-recursive, no repetition, but I'm not sure how bad is format
if compared to plain string_concat
.
mapconcat
reduce([], X, _, X) :- !.
reduce([X | Xs], Acc, Predicate, Result) :-
call(Predicate, Acc, X, Interim),
reduce(Xs, Interim, Predicate, Result).
reduce([X | Xs], Predicate, Result) :- reduce(Xs, X, Predicate, Result).
mapconcat_helper(Glue, X, Y, Z) :- format(atom(Z), '~p~p~p', [X, Glue, Y]).
mapconcat(List, Glue, Result) :-
reduce(List, mapconcat_helper(Glue), Result).
To my surprise, there isn't a reduce
-like predicate in the standard library, so I had to write my own. Finally, this won't explode the stack for longer lists.
So, which one, if any of these is best? Perhaps there are libraries that already do this better? I'm using SWI Prolog, and am doing this mostly for self-education, so I'm not yet concerned with portability etc. If it's SWI-specific, I think it'd still do.
foldl/4
, for "reduce". The!/0
you added only makes your version less general than what you can do withfoldl/4
. \$\endgroup\$ – mat Apr 13 '15 at 14:37foldl
added after 6.0.2? Because I'm sure I searched for fold, but didn't find it. I think I added!
due to instantiation mode ofstring_concat
, but now I've checked and it works both ways, so... I don't know why I did it :) \$\endgroup\$ – wvxvw Apr 13 '15 at 16:43foldl/4
is quite powerful and useful in many cases. Due to its completely relational nature, it can be used in more cases than may be apparent at first. \$\endgroup\$ – mat Apr 16 '15 at 11:36