I am trying to get the maximum number from a given list 'L' and assign it to a variable, so I used to write the following function:
max(L,X):-
[H|Q]=L,
(X<H -> X=H),
length(Q,QLEN),
(QLEN>0 -> max(Q,X)),
true.
However after compiling the code and prompting max([1,2,3],X)
within SWI-Prolog, I get the following error:
ERROR: Arguments are not sufficiently instantiated ERROR: In: ERROR:
[9] _1064<1 ERROR: [8] max([1,2|...],_1092) at c:/users/kais/desktop/tp3.pl:24 ERROR: [7]
Why I'm getting such error?
X
does not have a value when you try to compare it withH
. That whole line makes no sense in Prolog. You need some other parameter for the max value so far. - Tomas BymaxList/3
is where yourX
gets instantiated, ie at the end. - Tomas By