Problems with lists -Prolog-

0

I have a problem with an exercise of , which is to construct a predicate that given an atom a as the first argument, a second atom s as the second argument, is unified to a third parameter L , which contains a set of numbers corresponding to the positions where the atom a appears in atom s . considering 0 as the first position.

As an example I have this:

buscaInstanciasAtom(asa, aSaasasasa, L) should result in L = [3,5,7] .

With this in mind, I did the following:

buscaInstanciasAtom(A, S, L):- sub_string(S,Posicion,_,_,A), L=Posicion.

But instead of being L=[3,5,7] , it results in L=3,L=5,L=7 , and I do not know how to insert in a single list, help please.

    
asked by CARLV 17.07.2018 в 23:06
source

1 answer

0

Use predicate sub_atom/5 instead of sub_string/5 . sub_string/5 is functionally equivalent to sub_atom/5 for chains, but you are working with atoms.

Starting with your solution:

buscaInstanciasAtom(A, S, L):- sub_atom(S,L,_,_,A).

You can use the findall/3 predicate to find all the answers of the sub_atom/5 predicate in a deterministic way.

buscaInstanciasAtom(A, S, L):- findall(P, sub_atom(S,P,_,_,A), L).

So that L is a list made up of all P values that satisfy the target sub_atom(S,P,_,_,A) .

Testing predicate searchesAtomAunts / 3 in Tau Prolog

    
answered by 18.07.2018 / 20:30
source