Count how many times a node is repeated in prolog

0

connection (d, h, 4). connection (h, f, 9). connection (i, f, 11). connection (f, g, 10). connection (f, a, 8). connection (a, b, 7).

hasArray (X): -connection (X, , ).

I want to know for example this code tells me that f is repeated 2 times I want to know how to look for it. Thanks for your help.

    
asked by Kevin Adriel GuTierrez 12.06.2018 в 06:02
source

1 answer

0

You can use findall / 3 to find all the repetitions of, what you Call nodes, and create a list with them. Then just count the number of items on that list. For example:

conexion(d,h,4). 
conexion(h,f,9). 
conexion(i,f,11). 
conexion(f,g,10). 
conexion(f,a,8). 
conexion(a,b,7).


tieneArista(X) :- findall(X, conexion(X,_,_), L), length(L,N), writeln(N).

Execution:

?- tieneArista(f).
2
true.

?- tieneArista(h).
1
true.
    
answered by 12.06.2018 в 19:29