2

I am given a database and I am querying the database with a predicate

findmin(A,B,course(X,Y)):- course(X,Y),X >= A,Y =< B.

I have my database like,

course(a1,b1).
course(a2,b2).
course(a3,b3).
...

Now instead of using standard findall/3predicate, I want to use my own findall,

finda(X,findmin(A,B,X),L)

If i use a recursion that will always take me to the beginning of database, I'm not getting how to use findmin recursively to give me distinct occurrences in database.

1 Answers1

0

One way to achieve this is to use a failure-driven loop with side-effects. In the case of a findall implementation, this can broadly be achieved like this:

finda(X, Goal, Xs) :-
    % execute the goal to produce the binding for X
    Goal,
    % assert the result to the database (the 'side-effect')
    assert_to_db(..., Goal, ...)
    % deliberately fail, forcing Goal to be re-evaluated
    fail. 
finda(_X, _Goal, Xs) :-
    % retrieve the result from the cache and clear it
    retrieve(..., Xs, ...).

For a full implementation, see this response on StackOverflow.

Community
  • 1
  • 1