Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9206845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:15:41+00:00 2026-06-18T00:15:41+00:00

When I run a query, it can return zero, one or more than one

  • 0

When I run a query, it can return zero, one or more than one results. How can I “save” these results so that I can compare them later, or how can I compare them on the fly?

For example, I have these facts:

father(john, mark).
father(john, michael).
age(michael, 10).
age(mark, 12).

Now if I run a query like this

?- father(john, X).

it will return mark and michael, but how can I compare the age of mark and michael? In fact, how can I even know that the query will return more than one results?

The idea is that I want to get the eldest son of a father

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:15:42+00:00Added an answer on June 18, 2026 at 12:15 am

    What you’re running into here is the fact that on backtracking, Prolog releases the variable bindings it found when it produced the previous result. This feature causes a lot of consternation in the early days, so know that you’re not alone, we were all there once.

    The first thing you should do is try to let go of the need to tell Prolog how to get the result, and instead focus on telling Prolog how to distinguish the result you want logically. This is logic programming, after all. 🙂 When you say “how can I compare the age of mark and michael?” you’re hiding the real question behind the assumption that once you know how to hold onto things you can find the answer yourself. But you don’t want to find the answer yourself, you want Prolog to find it!

    Let’s take an example. Say you want to find out who is the youngest child of whom. You can do this logically:

    ?- father(Father, Child), 
       age(Child, YoungestAge), 
       \+ (father(Father, Child2), 
           age(Child2, Younger), 
           Younger < YoungestAge).
    Father = john,
    Child = michael,
    YoungestAge = 10.
    

    This would be inefficient with a large database, unfortunately, since Prolog will have to search every age/2 fact to find all the children of a particular parent. Presumably Prolog will index these predicates and that may be enough to save us depending on how you use it, but it doesn’t look like a strategy you can apply universally. But you can’t beat the logical reading of this query: supposing I have a father Father of child Child, and supposing that Child’s age is YoungestAge, there is no Child2 of this same Father whose age is Younger than YoungestAge.

    Frequently, you do need all the solutions, and for that there are three predicates: setof/3, bagof/3 and findall/3. They all have basically the same API with slightly different semantics. For instance, if you want all the children for a parent, you can use setof to get them:

    ?- setof(Child, father(john, Child), Children).
    Children = [mark, michael].
    

    You’ll need a bigger fact database to see the effect of the differences between the two, but that’s a different question. In short, setof/3 will get you a sorted list of unique answers whereas bagof/3 will get you an unsorted list with all the answers. findall/3 does the same thing, with a difference in the way it treats variables. In my experience, setof/3 and findall/3 tend to be used a lot more than bagof/3.

    If the work you’re doing necessitates it, or if efficiency demands it, you can use these meta-predicates to find all the possible solutions and walk through the list doing whatever processing you need to do.

    As for the question “how can I even know that the query will return more than one result?” the answer is basically you can generate them all and see how many you had (findall(..., Answers), length(Answers, Count)) or you can use once/1 to ensure that you get a single solution. once is great for making non-deterministic predicates deterministic; the effect is basically the same as putting a cut right after the clause. For instance:

    ?- father(john, X).
    X = mark ;
    X = michael.
    
    ?- once(father(john, X)).
    X = mark.
    
    ?- father(john, X), !.
    X = mark.
    

    In general it is recommended that you use once/1 over explicit cuts, if possible.

    Let me know if this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a requirement to run a query against a database that will return
How can I make a sql SELECT query run forever in oracle 10g? It
I can run the following query in PHPMyAdmin, but for some reason I get
In my repository implementation I can run the following query using a lambda expression:
I have two tables from which I'm trying to run a query to return
I run a query that returns the first word only from a particular field,
I'm currently having to run a query like the below for a one off
When I run this query in my spring project it just return an error.
I am trying to query a database to return some matching records and can't
For example, if I run one query in model: public function list_users() { $q

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.