CALCULATING DAY NAME FROM DATE IN PROLOG 
Author Message
 CALCULATING DAY NAME FROM DATE IN PROLOG

Does anyone know how to do this?

Thanks.



Tue, 01 Jun 2004 04:21:55 GMT  
 CALCULATING DAY NAME FROM DATE IN PROLOG

Quote:
> Does anyone know how to do this?

> Thanks.

FIGURED IT OUT:

day(_month,_day,_year,D):-
  A is (14-_month)//12,
  Y is _year-A,
  M is round(_month+(12*A)-2),
  D is (_day+Y+(Y//4)-(Y//100)+(Y//400)+(round(31*M)//12)) mod 7.

Later!



Tue, 01 Jun 2004 16:24:10 GMT  
 CALCULATING DAY NAME FROM DATE IN PROLOG
EVEN BETTER!!!

=====================================================================
day(_month,_day,_year,D):-
  A is (14-_month)//12,
  Y is _year-A,
  M is round(_month+(12*A)-2),
  D is (_day+Y+(Y//4)-(Y//100)+(Y//400)+(round(31*M)//12)) mod 7.

dayname(_daynum,_dayname):-
  %nth1(?Index, ?List, ?Elem)
  _daywords = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
  _daynums = [0,1,2,3,4,5,6],
  nth1(_numindex,_daynums,_daynum),
  nth1(_numindex,_daywords,_dayname).
===================================================================
Gets the day number, then, uses it to derive the actual day name.
That way, I can derive either!

Not bad, for my second PROLOG program ever, eh?


Quote:
> Does anyone know how to do this?

> Thanks.



Tue, 01 Jun 2004 17:07:15 GMT  
 CALCULATING DAY NAME FROM DATE IN PROLOG
I would use:

dayname(Num,Name) :-

member([Num,Name],[[0,'Monday'],[1,'Tuesday'],[2,'Wednesday'],[3,'Thursday']
,[4,'Friday'],[5,'Saturday'],[6,'Sunday']]).

or even store the expansion as a set of facts:

dayname(0,'Monday').
dayname(1,'Tuesday').
...
dayname(6,'Sunday').

A matter of taste of course.



Quote:
> EVEN BETTER!!!

> =====================================================================
> day(_month,_day,_year,D):-
>   A is (14-_month)//12,
>   Y is _year-A,
>   M is round(_month+(12*A)-2),
>   D is (_day+Y+(Y//4)-(Y//100)+(Y//400)+(round(31*M)//12)) mod 7.

> dayname(_daynum,_dayname):-
>   %nth1(?Index, ?List, ?Elem)
>   _daywords =

['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
Quote:
>   _daynums = [0,1,2,3,4,5,6],
>   nth1(_numindex,_daynums,_daynum),
>   nth1(_numindex,_daywords,_dayname).
> ===================================================================
> Gets the day number, then, uses it to derive the actual day name.
> That way, I can derive either!

> Not bad, for my second PROLOG program ever, eh?




Quote:
> > Does anyone know how to do this?

> > Thanks.



Mon, 07 Jun 2004 17:20:33 GMT  
 CALCULATING DAY NAME FROM DATE IN PROLOG


Quote:
> I would use:

> dayname(Num,Name) :-

member([Num,Name],[[0,'Monday'],[1,'Tuesday'],[2,'Wednesday'],[3,'Thursday']

Quote:
> ,[4,'Friday'],[5,'Saturday'],[6,'Sunday']]).

> or even store the expansion as a set of facts:

> dayname(0,'Monday').
> dayname(1,'Tuesday').
> ...
> dayname(6,'Sunday').

> A matter of taste of course.

It's not just a matter of taste,  dayname/2 as a set of facts will be much
more efficient when looking up a Name from a Number, because Prolog indexes
on the first argument.

Also, it's a bad idea to use lists for tuples. Since [0,'Monday'] is
really '.'(0,'.'('Monday',[])) there's a lot of unnecessary baggage
there, literally :).

It's much better to use N-ary terms for an N-ary tuples e.g.
foo(0, 'Monday). Save lists for sets, bags, sequences and the like.

--
Regards

John Fletcher



Mon, 07 Jun 2004 18:03:40 GMT  
 CALCULATING DAY NAME FROM DATE IN PROLOG

Quote:


> > I would use:

> > dayname(Num,Name) :-

> member([Num,Name],[[0,'Monday'],[1,'Tuesday'],
> > [2,'Wednesday'],[3,'Thursday'],[4,'Friday'],
> >[5,'Saturday'],[6,'Sunday']]).

> > or even store the expansion as a set of facts:

> > dayname(0,'Monday').
> > dayname(1,'Tuesday').
> > ...
> > dayname(6,'Sunday').

> > A matter of taste of course.

> It's not just a matter of taste,  dayname/2 as a set of
> facts will be much more efficient when looking up a Name
> from a Number, because Prolog indexes on the first argument.

> Also, it's a bad idea to use lists for tuples. Since
> [0,'Monday'] is really '.'(0,'.'('Monday',[])) there's a
> lot of unnecessary baggage there, literally :).

> It's much better to use N-ary terms for an N-ary tuples e.g.
> foo(0, 'Monday). Save lists for sets, bags, sequences and
> the like.

Good advice if the day number algorithm computed ISO weekday
indexes. It doesn't -- weekday 0 is Sunday.

Which leaves us wondering when the yanks are going to catch
up with the rest of the international community? :-)

Daniel



Wed, 09 Jun 2004 11:45:32 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Calculating date -14 days

2. Prolog code to convert date to day-of-week wanted

3. date after adding/subtracting days from date

4. Day function calculates wrong

5. The Best to Calculate The Day Of Week

6. Calculating Year, Month and Day of Life

7. calculating the julian day

8. -re-how calculate easter date

9. how calculate easter date

10. calculate Date

11. Calculating with dates

12. how are dates calculated in Clarion

 

 
Powered by phpBB® Forum Software