concat list of strings. like atomic_list_concat/2

59 views
Skip to first unread message

Dan

unread,
Jan 27, 2019, 11:47:58 PM1/27/19
to SWI-Prolog
Hello,

I am blanking about recursively concatenating a list of strings.  

What throws me off is how to arrange the string_concat/3 to keep to the right order

any thoughts are much appreciated,

Dan

Boris Vassilev

unread,
Jan 28, 2019, 12:38:39 AM1/28/19
to SWI-Prolog
Hello,

Not very clear what you tried and what you expected and what you got instead. Do you mean that you have a list of strings, like ["a", "b", "c"], and you want to get the string "abc"? As in:

?- atomics_to_string(["a", "b", "c"], X).
X = "abc".

And you tried:

?- L = ["a", "b", "c"], L = [H|T], foldl(string_concat, T, H, X).
L = ["a", "b", "c"],
H = "a",
T = ["b", "c"],
X = "cba".

... and this is throwing you off?

If this was the case, you can consider reversing the list first:

?- L = ["a", "b", "c"], reverse(L, [H|T]), foldl(string_concat, T, H, X).
L = ["a", "b", "c"],
H = "c",
T = ["b", "a"],
X = "abc".

You can also write your own foldr/4 (Right Fold) that either reverses and does a left fold or does something more fancy.

But again I am making too many assumptions about your intentions and your problem.

--
You received this message because you are subscribed to the Google Groups "SWI-Prolog" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swi-prolog+...@googlegroups.com.
Visit this group at https://groups.google.com/group/swi-prolog.
For more options, visit https://groups.google.com/d/optout.

Dan

unread,
Jan 29, 2019, 3:56:47 AM1/29/19
to SWI-Prolog
Hi Boris,

Thank you. 

Yes, i was completely blanking on this -- also, i haven't used foldl before, so its great to see an example. 

thank you,

Daniel
Reply all
Reply to author
Forward
0 new messages