0
votes

I have a problem on Prolog related to list of lists. The problem is about implementing a Prolog predicate: to_do(ListOfLists,_,Result). Where ListOfLists is a list of lists of character, either a,b,c, or d. Result must substitute a to ai, b to bi, c to x, and d to y.

For example,

?- to_do([[b,c],[a,d]],_,Result).
   Result = [[bi,x],[ai,y]].

?- to_do([[a,a,b],[b,c,d],[c,c,a]],_,Result).
   Result = [[ai,ai,bi],[bi,x,y],[x,x,ai]].

Note that Result must also be a list of lists.

Thanks a lot!

1
and what you have tried?RBA

1 Answers

0
votes

I believe this is solution to your problem:

substitute(a, ai).
substitute(b, bi).
substitute(c, x).
substitute(d, y).
substitute(In, Out) :- maplist(substitute, In, Out).

to_do(X, _, Y) :- substitute(X, Y).