Hello and thank you for taking the time to read this question. I have the following problem:
Given a list that returns the number of numbers greater than X Example: greater(4,[1,2,3,4,5,6],N) Result. N= 2
may(0,[],0):-!, fail.
may(N,[N],1).
may(N,[H|Q],X):-H>N,may(Q,N,X),X is X+1.
The problem is that PROLOG only returns False but not the value of X .
I hope you can explain to me what I'm doing wrong, thank you in advance for your help.
First error when you call may(Q,N,X) in the third line rule, then you are trying to step on X with X is X+1. Such assignment in Prolog is not allowed since "Variables" with already assigned values cannot be modified. Second returns false because when may(N,[H|Q],X):-H>N,may(Q,N,X),X is X+1. compare 1>4 returns false and since it cannot enter the other rules (since one is with an empty list and the other if it only has one element) throw false and backtracking returns false. In prolog what is used and for me it is very useful to understand how it works is to write trace. at the prompt to see how the program is running and with nodebug. trace mode is exited. Your program would be the following:
The use of the underscore is so that the Singleton warning is not given. Its function is different, but in order not to confuse you, I will only tell you the aforementioned, as you will see I created a variable X1 in the second line to be able to save the number of values that met the condition of greater, and then in the one below I call when they are smaller than N because otherwise, when it encounters a smaller one, backtracking will start and it will cut recursion, throwing false. I hope you understand and anything you answer me.