list - Having trouble with predicate in Prolog -
i trying right predicate in prolog accepts item, list, , number, , checks see if item in list number of times. example
count(7,[3,7],x).
would return x=1
.
count(7,[3,7],1).
would return true
this have far
count_occur(a,[0|b],d). count_occur(a,[a|c],d) :- count_occur(a,c,d1), d d1+1. count_occur(a,[b|c],d) :- count_occur(a,c,d).
i new prolog , struggling understand programming paradigm.
what trying see if first item in list matches passed-in value (a), if increment d , check again against remainder of list. how in lisp or language anyway. use help, been @ while , isn't clicking me.
i don't have prolog right test it, try that:
count_occur(a, [], 0). count_occur(a, [a|t], d):- count_occur(a, t, d1), d d1 + 1. count_occur(a, [b|t], d):- \= b, count_occur(a, t, d).
the idea if list empty, there 0 occurrences of each element. rest same yours think correct.
the difference have added a \= b
, should mean a \neq b
. think otherwise accept a == b
, might lead count_occur(3, [3], 0).
being true. should check that.
i hope helps!
Comments
Post a Comment