I was asked a question:
Define a predicate ordered by / 1, which checks the list of integers in ascending order. For example, a goal ordered([1,3,7,11])must be successful, just like a goal ordered([1,3,3,7]), while a goal ordered([1,7,3,9])must fail.
So far I have this:
ordered([]).
ordered([N, M|Ns]):-
append(M, Ns, Tail),
ordered(Tail),
N =< M.
But it does not work on every list.
I realized that the reason she fails is because she reaches a finite number in the list, and then tries to compare that number with an empty list. Obviously, this fails because you cannot compare an integer with an empty list. Even if you could, and say, return it 0for an empty list, it will still return false, since the number will be greater than 0, no less.
... ? , .
, :
ordered([]).
ordered([N]):-
N >= 0.
ordered([N, M|Ns]):-
append(M, Ns, Tail),
ordered(Tail),
N =< M.
ordered([1]), .
- ordered([N, M|Ns]) ?