user971840
user971840

Reputation: 75

Prolog Database Query

To make a dating database with facts in the form of:

person(name, age, sex, height, weight, education, salary)

Given the input of:

datable(alex, X).

datable_weight_limit(alex, 170, X). Where 170 is the threshold for weight

How can I return the names of potential matches with the rules:

  1. Only opposite sexes can be matched
  2. Males should be older than females
  3. Males should have a higher education than females?

I have these relations set up:

edu_less(hs, bachelor).
edu_less(bachelor, master).
edu_less(master, phd).

edu_lesser(A, B):-
    edu_less(A, X),
    edu_lesser(X, B).

I've tried:

datable(X, Y):-

    person(X, Agel, Sexl, Heightl, Weightl, Educationl, Salaryl),

    person(Namem, Agem, Sexm, Heightm, Weightm, Educationm, Salarym),

    Sexm \== Sexl.

datable(X, Y):-

    person(X, Agel, Sexl, Heightl, Weightl, Educationl, Salaryl),

    Sexl == female,

    findall( X, person(X, _, male, _, _, _, _), Y).

But I seem to have no luck. Is there a function I'm missing or something I'm implementing wrong?

Upvotes: 1

Views: 1287

Answers (1)

Mikita Belahlazau
Mikita Belahlazau

Reputation: 15434

I think you are almost correct. Try this:

datable(M, F):-
    person(M, AgeM, male, HeightM, WeightM, EducationM, SalaryM),
    person(F, AgeF, female, HeightF, WeightF, EducationF, SalaryF),
    AgeM >= AgeF,
    edu_lesser(EducationF, EducationM),
    datable_weight_limit(M, MaxWeight),
    MaxWeight >= WeightF. 

Here we suppose, that education is also numbers. If you need also function - datable(F,M):

datable(F,M) :-
     person(F, _, female, _, _, _, _),
     datable(M, F).

Upvotes: 2

Related Questions