I'm attempting to determine whether a Prolog list has an odd or even number of elements. I realize there are likely better solutions using length/2
and I would be willing to mark a better solution than this one the right answer, I would just like to know what I am doing wrong in this example.
I have code detailed as:
oddSize([]) :-
false.
oddSize([_]).
oddSize([_,_|T]) :-
oddSize(T).
The output I receive when I attempt to test this code is:
1 ?- oddSize([]).
false.
2 ?- oddSize([1]).
true ;
false.
3 ?- oddSize([1,2]).
false.
4 ?- oddSize([1,2,3]).
true ;
false.
It seems to be detecting which of the lists have an odd number of elements, but why do I get the extra result of false
?