I'm pretty new to prolog and I'm trying to write a predicate such that I can tell if a list is the list of size one or not. Currently I have this:
one([H | T]) :- H \= [] ,T == [].
There is problems with this, at least my logic is that if the H
is not empty and the tail has nothing, then it must be the case that there is something in the head and thus has a size of one. Else it does not.
Some insight on solving this problem would be much appreciated thank you.
length([A], L).
will give 'L = 1', but OP's code fails onone([A])
. The reason is that it fails onone([[]]).
Correct code could be justone([H | T]) :- T==[].
, but of course that's equal toone([_])
. – Sergii Dymchenko[[]]
. – Shon