|
From: | emacstheviking |
Subject: | Re: Impressed but confused, gluing a list together... |
Date: | Tue, 1 Oct 2013 22:04:50 +0100 |
Thanks.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........join([X], _, [X]).join([], _, []).Rule 1: given an empty list, return one, you can't glue nothing together.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: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.
join([], _, []).
join([X], _, [X]).
join([X|Xs], With, [X, With|Acc]) :- glue( Xs, With, Acc).
However... I am not sure I truly understand how I did it... if I explain my reasoning perhaps somebody can straighten me out?
Rule 2: If there is a single X in the list, return just that single X. That avoid "trailing" glue.
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.
[Prev in Thread] | Current Thread | [Next in Thread] |