Ignore the deliberate error "glue" / "join"...the real code says "join". Sorry.
On 1 October 2013 22:03, emacstheviking <[email protected]> wrote: > I am dong some gluing of strings together and I wanted an intercalate > capability like many languagues offer, e.g. "join" in Javascript or > implode() with PHP and I came up with this: > > join([], _, []). > join([X], _, [X]). > join([X|Xs], With, [X, With|Acc]) :- glue( Xs, With, Acc). > > The great news is that I figured it out first time and it seems to do what > I need, I also have a flatten() predicate that will produce the final > output string. > > However... I am not sure I truly understand how I did it... if I explain > my reasoning perhaps somebody can straighten me out? > > Rule 1: given an empty list, return one, you can't glue nothing together. > join([], _, []). > > > Rule 2: If there is a single X in the list, return just that single X. > That avoid "trailing" glue. > join([X], _, [X]). > > Rule 3: Um...... I used the force I guess. > join([X|Xs], With, [X, With|Acc]) :- glue( Xs, With, Acc). > > Having seen many other things like it I kind of sensed it would do the > right thing but if somebody could supply me a clear explanation I'd be > grateful. I kind of know what's going on but I can't express it to my own > satisfation which is quite annoying right now. > > I "intuited" that the rule head should glue the current value of the head > of the list with the concatenation value *With* plus whatever the > accumulator has so far and that it should succeed if the same can be done > with the remainder of the list but somehow I feel that I just saw E.T. ride > off on the back of a unicorn reading a Marvel comic........ > > Thanks. > >
_______________________________________________ Users-prolog mailing list [email protected] https://lists.gnu.org/mailman/listinfo/users-prolog
