0
votes

I'm new to prolog and have been working on a problem related to what I believe is concatenation functionality in prolog. After reading the documentation for predicates that work on lists and strings, I am left with more questions than answers.

The goal I'm trying to achieve is to create one single product from distributing a string across all the different elements in a list. I've used append/2 and maplist/2 and some other predicates but have always gotten a response of false. Example of what I'm trying to achieve:

?- cellprod("B",["C","D","E"],X).
X = ["BC", "BD", "BE"].

Current code:

cellprod(A,[B|B1],C):- append(A,B),
                       cellprod(A,B1,C).

What predicates should I be looking at for a string to list conversion to distribute and end with a newly formed list? Or is there a concatenation predicate I am missing? Much appreciated!

1

1 Answers

0
votes

Maybe you are required to learn about recursion, so this answer could be totally useless... anyway... we can solve easily in this way:

?- S="B",L=["C","D","E"],findall(T,(member(E,L),string_concat(S,E,T)),X).
S = "B",
L = ["C", "D", "E"],
X = ["BC", "BD", "BE"].

or this:

?- S="B",L=["C","D","E"],maplist(string_concat(S),L,X).
S = "B",
L = ["C", "D", "E"],
X = ["BC", "BD", "BE"].