0
votes

This is my first time using Prolog

I have employees

employee(eID,firstname,lastname,month,year) 

example :

employee(1,liz,white,4,2000).
employee(2,ted,johnson,5,1998).

I want to make a predicate senior(X,Y) that will return true if the first employee is older in the company.

I have this:

senior(X,Y) : -
  employee(X,firstname,lastname,month,year),
  employee(Y,firstname,lastname,month,year),
  X.year < Y.year.

but this always return false. I can't understand the reason.

How can I make this predicate work?

2
That is no way to run a company, and is illegal in Europe. You must not discriminate by age, except if there is a valid reason (e.g. the law limits working hours for people below a curtain age).ctrl-alt-delor
lol, btw this is the date that the employee hired !Thelouras
If you mean date employee was hired, then it is less dodgy. However I still do not see what value it is to compare people, like this. One person may have been in the company a few weeks, but be much better at a particular job than someone that was there 10 years.ctrl-alt-delor

2 Answers

1
votes

In Prolog, variables start with either an underscore or an upper case letter. E.g. firstname is an atom, i.e. a constant, but FirstName is a variable. But, in your specific question, you don't care about the employee names. Thus, you can replace those arguments by the anonymous variable:

senior(X,Y) : -
  employee(X, _, _, Xmonth, Xyear),
  employee(Y, _, _, Ymonth, Yyear),
  ...

Can you now complete the code by writing the necessary comparisons using the Xmonth, Xyear, Ymonth, and Yyear variables?

1
votes

Is it mandatory that you do this with one rule? You could use one rule for comparing employees who were hired in different years, and a second rule for comparing employees who were hired in the same year. To expand on this, let's say you have employees listed this way:

employee(eid,year,month,day)

and, of course, a list of employees. You could use the following three rules:

% For employees that were hired in different years.
senior(Eid1,Eid2) :-
    employee(Eid1,X,_,_),
    employee(Eid2,Y,_,_),
    X<Y.

% For employees that were hired in the same year, different month.
senior(Eid1,Eid2) :-
    employee(Eid1,Year,X,_);
    employee(Eid2,Year,Y,_);    % Notice how one common variable "Year" is used
    X<Y.

% For employees that were hired in the same year, same month, different day,
% the rule is "expanded" from the previous one.
senior(Eid1,Eid2) :-
    employee(Eid1,Year,Month,X);
    employee(Eid2,Year,Month,Y);
    X<Y.

Make sure you don't forget and replace "Year" and/or "Month" with underscores, because then somebody hired on 2010-01-01 (ISO 8601) would be shown as senior to someone hired on 2005-12-12.

Then again, perhaps you should catalogue all dates in ISO 8601:2004. No matter how big your employee list, you could write a small script to convert

employee(eID,firstname,lastname,month,year)

to

employee(eID,firstname,lastname,yyyymm)