Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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.

share|improve this question
2  
Check out foldl/4, for "reduce". The !/0 you added only makes your version less general than what you can do with foldl/4. –  mat Apr 13 at 14:37
    
Thanks @mat, Was foldl 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 of string_concat, but now I've checked and it works both ways, so... I don't know why I did it :) –  wvxvw Apr 13 at 16:43
1  
6.0.2 is quite outdated, I recommend you upgrade to 7.1.36 or later. Soon, 7.2 will be released as a stable release within the 7.x development branch. foldl/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. –  mat Apr 16 at 11:36

1 Answer 1

up vote 4 down vote accepted

A quick browse through the SWI-Prolog manual finds atomics_to_string/3:

atomics_to_string(+List, +Separator, -String)

Creates a string just like atomics_to_string/2, but inserts Separator between each pair of inputs. For example:

?- atomics_to_string([gnu, "gnat", 1], ', ', A).
A = "gnu, gnat, 1"
share|improve this answer
    
It's funny, because I read that section of the manual, but didn't make a connection. Thanks for the effort! –  wvxvw Apr 10 at 7:50
    
Also, worth noting. This predicate only exists in the development version 7.X.X and high, (stable versions 6.X.X don't have it). –  wvxvw Apr 10 at 8:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.