Reputation: 392
I'm new to Prolog and trying to figure out how it works. So could anyone help me with the following code:
whatisthis([]).
whatisthis([_, b| L]):- whatisthis(L).
So when would Prolog return 'True' if the query would have the following format:
?- whatisthis(X).
I experimented with some different inputs and the only X that returned 'True' was X = [].
Upvotes: 1
Views: 55
Reputation: 477220
This query is even that declarative that you can query what X
will be true:
?- whatisthis(X).
X = [] ;
X = [_G1242, b] ;
X = [_G1242, b, _G1248, b] ;
X = [_G1242, b, _G1248, b, _G1254, b] ;
X = [_G1242, b, _G1248, b, _G1254, b, _G1260, b] ;
X = [_G1242, b, _G1248, b, _G1254, b, _G1260, b, _G1266|...]
If you hit the semicolon ; it will emit the next result. The _Gxxxx
parts are unbound variables. You can write anything there.
So every X
that is a list of even length where every second element is a b
. So the following examples will succeed:
[]
[a, b]
[2, b]
[1, b, 4, b]
[a, b, 1, b]
[f(a,b), b, b(f,a), b]
[1, b, 4, b, 2, b, 5, b]
Upvotes: 2