Home > Archive > Prolog > February 2005 > swi-prolog calc age
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
swi-prolog calc age
|
|
|
| Hi,
I am teaching prolog to a high school class and today I showed them about
rules as follows.
boy(v).
boy(j).
boy(a).
girl(e).
girl(l).
the rule
possible_boyfriend(A,B) :-
girl(A),
boy(B).
Now one of the students then asked how we could calculate someone's age ...
if we added some facts like
birthday(anna,10/1/1990).
I wonder if people have any ideas how to create a predicate like
age(X,A) :-
A is now() - birthday(X,B). % where A is the person's age
kind regards & thanks in advance
Darren
| |
| uwe hunger 2005-02-26, 8:57 am |
| inxdr wrote:
> Hi,
> I am teaching prolog to a high school class and today I showed them about
> rules as follows.
>
> boy(v).
> boy(j).
> boy(a).
>
> girl(e).
> girl(l).
>
> the rule
> possible_boyfriend(A,B) :-
> girl(A),
> boy(B).
>
> Now one of the students then asked how we could calculate someone's age
> ... if we added some facts like
> birthday(anna,10/1/1990).
>
> I wonder if people have any ideas how to create a predicate like
> age(X,A) :-
> A is now() - birthday(X,B). % where A is the person's age
> kind regards & thanks in advance
> Darren
This was my solution:
/* has birthday this year */
birthday(birthday,5,2,1990).
/* has birthday today, the current system date*/
birthday(birthdaytoday,26,2,1990).
/* hasn't birthday this year */
birthday(nobirthday,3,7,1990).
/* no birth, in planning */
birthday(nosolution,1,1,3000).
age(N,A) :-
get_time(T),
convert_time(T,J,M,D,_,_,_,_),
birthday(N,D1,M1,J1),
compute(A,D,D1,M,M1,J,J1).
compute(A,D,D1,M,M1,J,J1) :-
J =:= J1,
M =< M1,
D =< D1,
A is 0.
compute(A,_,_,M,M1,J,J1) :-
J > J1,
M > M1,
A is J -J1.
compute(A,D,D1,M,M1,J,J1) :-
J > J1,
M =:= M1,
D < D1,
B is J -J1,
A is B - 1.
compute(A,D,D1,M,M1,J,J1) :-
J > J1,
M =:= M1,
D >= D1,
A is J -J1.
compute(A,_,_,M,M1,J,J1) :-
J > J1,
M < M1,
B is J -J1,
A is B -1.
Uwe
|
|
|
|
|