×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Prolog Database Question

Prolog Database Question

Prolog Database Question

(OP)
Hi,

So I'm trying to finish up a project for a class. It's a "dating database". It includes 5 males, and 5 females. I have to match them up using the following rules:
(a) Only opposite sex can be matched
(b) Male candidate should have a higher age than Female candidate
(c) Male candidate should have a higher or equivalent education, compared with Female candidate
The following search rules of this database are assumed and should be implemented in your database:
(d) datable search: to find only the opposite-sex candidates who satisfy the aforementioned matching rules
(e) datable_weight search: to find only the opposite-sex candidates who satisfy the aforementioned matching rules and have a less weight than a specified threshold.

Perform the following test cases:

datable(alex, X).
datable(frank, X).
datable_weight(betty,170,X).
datable_weight(sussan,170,X).

In the above cases, 170 refers to the weight threshold.

****Here's my code:*****
%% The database %%

% values meaning person(name,age,sex,height,weight,edu,sa…
person(alex, 20,m,160,150,hs,30).
person(betty, 20,f,158,148,hs,28).
person(charles, 30,m,165,155,bach,35).
person(david, 35,m,170,160,bach,40).
person(edward, 40,m,175,165,master,50)…
person(frank, 45,m,180,180,phd,60).
person(keley, 25,f,163,153,hs,33).
person(victoria,30,f,168,158,bach,38).
person(timbeley,35,f,173,163,master,48…
person(sussan, 40,f,178,168,phd,58).

size_database(N) :- N is 10.

%% end of the database %%


%% list functions %%

append([X|Y],Z,[X|W]) :- append(Y,Z,W).
append([],X,X).

member(X,[L|_]) :- X == L. % swi prolog is dumb...
member(X,[_|R]) :- member(X,R).

cons(X,R,[X|R]).

%% end of list fucntions %%


%% gender functions %%

% m ascii value is 109
% f ascii value is 102

male(Name) :- person(Name,B,Sex,D,E,F,G), atom_char(Sex,Num), Num = 109.
female(Name) :- person(Name,B,Sex,D,E,F,G), atom_char(Sex,Num), Num = 102.

%% end of gender functions %%

% true if the first person has a higher age
higherAge(Name1,Name2) :- person(Name1,B_AGE,C,D,E,F,G),
person(Name2,Y_AGE,X,W,V,U,T), B_AGE > Y_AGE.


%% education functions %%

% assigning values to educations
eduNum(hs,N) :- N is 0.
eduNum(bach,N) :- N is 1.
eduNum(master,N) :- N is 2.
eduNum(phd,N) :- N is 3.

% true if Edu1 is greater or equal to Edu2
greaterEdu(Edu1,Edu2) :- eduNum(Edu1,En1), eduNum(Edu2,En2),
En1 >= En2.

% true if the first person has a higher education
higherEdu(Name1,Name2) :- person(Name1,B,C,D,E,F_EDU,G),
person(Name2,Y,X,W,V,U_EDU,T),greate…

%% end of education functions %%


datable(Name1,Name2) :- male(Name1), female(Name2),
higherEdu(Name1,Name2), higherAge(Name1,Name2).
datable(Name1,Name2) :- female(Name1), male(Name2),
higherEdu(Name2,Name1), higherAge(Name2,Name1).

datable_weight(Name1,Weight,Name2) :- datable(Name1,Name2),
person(Name2,Y,X,W,V_Weight,U,T),
Weight >= V_Weight.

% doesn't work...
datableList(Name,List) :- datableList(Name,List,0).

datableList(Name,List,Index) :- datable(Name,X),
not(member(X,List)), append(List,X,List),
size_database(Size), Index =< Size,
Index is Index + 1,
datableList(Name,List,Index).


When I run it, it always just picks the first opposite-sex member from the list, instead of using the rules. Any suggestions or help?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close