prolog-setof

Prolog Recursion skipping same results

谁都会走 提交于 2019-11-26 18:38:10
问题 My code runs but the problem is it shows the same results more than once. Here's my code: disease(hiv,[sore_throat,headache,fever,rash]). disease(pregnancy,[fatigue,vomiting,light_headedness,increased_waistline]). disease(flu,[fatigue,fever,tiredness,nasal_discharge]). diagnose([], []). diagnose(Name, [H|T]) :- disease(The_Disease, Symptoms), member(H, Symptoms), write(Name), write(' has/is '), writeln(The_Disease), diagnose(Name, T). member(X,[X|_]). member(X,[_|T]):- member(X,T). Result

Collect all “minimum” solutions from a predicate

谁说胖子不能爱 提交于 2019-11-26 11:35:10
问题 Given the following facts in a database: foo(a, 3). foo(b, 2). foo(c, 4). foo(d, 3). foo(e, 2). foo(f, 6). foo(g, 3). foo(h, 2). I want to collect all first arguments that have the smallest second argument, plus the value of the second argument. First try: find_min_1(Min, As) :- setof(B-A, foo(A, B), [Min-_|_]), findall(A, foo(A, Min), As). ?- find_min_1(Min, As). Min = 2, As = [b, e, h]. Instead of setof/3 , I could use aggregate/3 : find_min_2(Min, As) :- aggregate(min(B), A^foo(A, B), Min)