This is probably the most trivial implementation of a function that returns the length of a list in Prolog
count([], 0).
count([_|B], T) :- count(B, U), T is U + 1.
one thing about Prolog that I still cannot wrap my head around is the flexibility of using variables as parameters.
So for example I can run count([a, b, c], 3).
and get true
. I can also run count([a, b], X).
and get an answer X = 2.
. Oddly (at least for me) is that I can also run count(X, 3).
and get at least one result, which looks something like X = [_G4337877, _G4337880, _G4337883] ;
before the interpreter disappears into an infinite loop. I can even run something truly "flexible" like count(X, A).
and get X = [], A = 0 ; X = [_G4369400], A = 1.
, which is obviously incomplete but somehow really nice.
Therefore my multifaceted question. Can I somehow explain to Prolog not to look beyond first result when executing count(X, 3).
? Can I somehow make Prolog generate any number of solutions for count(X, A).
? Is there a limitation of what kind of solutions I can generate? What is it about this specific predicate, that prevents me from generating all solutions for all possible kinds of queries?