Question

I have a fact set as so...

fav_fruit(male, young, apple).
fav_fruit(female, young, bannana).
fav_fruit(male, old, bannana).
fav_fruit(female, old, apple).
fav_fruit(female, young, apple).

I'm need find out if there is any group of (Gender,Age) where there is more the one favorite fruit (the answer for the facts above would be (female,young.)

What I've been trying to figure out is how to use the aggregate or findall functions in prolog to return some type of list like....

female, young = 2 (apple, bannana)
male, young = 1 (apple)
male, old = 1 (bannana)
female, old = 1 (apple)

...that way I could just check the total for each member and test whether it it greater then 1.

Any ideas would be greatly appreciated.

Was it helpful?

Solution

How about

fav_fruit_class(Gender-Age, List):-
  findall(Gender-Age, fav_fruit(Gender, Age, _), LGenderAge),
  sort(LGenderAge, SGenderAge),
  member(Gender-Age, SGenderAge),
  findall(Fruit, fav_fruit(Gender, Age, Fruit), List).

The first findall+sort gets a list of classes (Gender/Age). The second findall gets the list of favorite fruits for each class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top