Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prolog Database Question

Status
Not open for further replies.

g0blu322

Programmer
Joined
Oct 28, 2012
Messages
4
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?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top