| Did you know ... | Search Documentation: |
| Average of solutions - calling a Prolog goal |
This example is a bit harder. The predicate average/3 is defined to take the template average(+Var, :Goal, -Average) , where Goal binds Var and will unify Average with average of the (integer) results.
PlQuery takes the name of a predicate and the
goal-argument vector as arguments. From this information it deduces the
arity and glocates the predicate. The method PlQuery::next_solution()
yields
true if there was a solution and false
otherwise (other values are possible if the flag PL_Q_EXT_STATUS
was specified in the constructor. If the goal yields a Prolog exception,
you can handle this and convert the Prolog exception to a C++ exception
using‘PlWrap<int>(...)`. A return to Prolog does
an implicit “cut” (PL_cut_query()); this can also be
done explicitly by the
PlQuery::cut() method.
PREDICATE(average, 3) /* average(+Templ, :Goal, -Average) */
{ long sum = 0;
long n = 0;
PlQuery q("call", PlTermv(A2));
while( PlWrap<int>(q.next_solution()) )
{ sum += A1.as_long();
n++;
}
return A3.unify_float(double(sum) / double(n));
}
?- [user]. |: p(1). |: p(10). |: p(20). |: % user://1 compiled 0.00 sec, 3 clauses true. ?- average(X, p(X), Average). Average = 10.333333333333334.