Thread: [pure-lang-svn] SF.net SVN: pure-lang: [339] pure/trunk/examples/libor
Status: Beta
Brought to you by:
agraef
From: <ye...@us...> - 2008-06-30 01:41:53
|
Revision: 339 http://pure-lang.svn.sourceforge.net/pure-lang/?rev=339&view=rev Author: yes Date: 2008-06-29 18:42:01 -0700 (Sun, 29 Jun 2008) Log Message: ----------- fixing permissions Removed Paths: ------------- pure/trunk/examples/libor/myutils.pure pure/trunk/examples/libor/queens.pure Deleted: pure/trunk/examples/libor/myutils.pure =================================================================== --- pure/trunk/examples/libor/myutils.pure 2008-06-30 01:08:27 UTC (rev 338) +++ pure/trunk/examples/libor/myutils.pure 2008-06-30 01:42:01 UTC (rev 339) @@ -1,31 +0,0 @@ -// Dr Libor Spacek, 21th May 2008 - -//General mathematical iterators over one and two indices -MathIter1 op i1 i2 f = foldl1 op (map f (i1..i2)); -MathIter2 op i1 i2 j1 j2 f = - foldl1 op (map (uncurry f) [x,y; x = i1..i2; y = j1..j2]); -//Examples on how to use the mathematical iterators -Sigma i1 i2 f = MathIter1 (+) i1 i2 f; -Pi i1 i2 f = MathIter1 (*) i1 i2 f; -Factorial n = Pi 1L n id; -//Binomial using (k, n-k) symmetry and bignum division -Binomial n k = (Pi (k+1L) n id) div (Pi 2L (n-k) id) if n-k < k; - = (Pi (n-k+1L) n id) div (Pi 2L k id); - -// Euclid's recursive greatest common factor algorithm for ints and bignums -Gcf x 0 | Gcf x 0L = x; -Gcf x y = Gcf y (x mod y); - -// take the head of a list and put it at the end -rotate (h:t) = reverse (h:(reverse t)); -// protate = rotate n items from the front: use when n is positive: 0<=n<=#n -protate 0 l = l; -protate n::int l = cat [(drop n l),(take n l)]; -// rotate n items, generalisation of "rotate the bits instruction" -// example: head (nrotate (-33) (0..23)); -// what time is 33 hrs before midnight? 15 hrs. -// The clock was moved -33 mod 24 = -9 hours from midnight (0) -nrotate n::int l = protate nm l when ll = #l; nm = ll + (n mod ll) end if n<0; - = protate nm l when nm = n mod #l end; - - Deleted: pure/trunk/examples/libor/queens.pure =================================================================== --- pure/trunk/examples/libor/queens.pure 2008-06-30 01:08:27 UTC (rev 338) +++ pure/trunk/examples/libor/queens.pure 2008-06-30 01:42:01 UTC (rev 339) @@ -1,96 +0,0 @@ -/* Several Solutions to the Queens Problem Dr Libor Spacek, 21th May 2008 - - (allqueens n) returns all solutions but is slow - (queens n) and (tailqueens n) return one different solution each - (thequeens n) does no search and is very fast even for large boards - -Examples: - - >allqueens 8; // returns all 92 solutions, as a list of lists - >queens 8; // gives solution number 52 in the allqueens' list, - >tailqueens 8; // gives solution no. 89, which is a reflection of no. 52 - >map succ (thequeens 8); // gives solution no. 56 */ - -// increment and decrement general utility -succ x::int = 1+x; pred x::int = x-1; - -// row j in current column not attacked by any queens in preceding columns? -safe _ _ [] = 1; -safe id::int j::int (j2::int:l) = // id is the column positions difference - if (j==j2) || (id==j2-j) || (id==j-j2) then 0 else safe (1+id) j l; - -allqueens n::int = list (searchall n n []) // returns all possible solutions - with - searchall n::int 0 p = p; - searchall n::int i::int p = - tuple [searchall n (i-1) (j:p); j = 1..n; safe 1 j p] - end; - -// the solution is only the rows permutation, without the ordered columns (1..n) -// full 2D board coordinates can be reconstructed with zip (1..n) (queens n); -nullary failed; -queens n::int = list (search n n n []) - with - search _ 0 _ p = (); // last i, solved - search _ _ 0 _ = failed; // failed, run out of alternative js - search n::int i::int j::int p = - if (failed === solution) then search n i (j-1) p else j,solution - when solution = search n (i-1) n (j:p); end if safe 1 j p; - = search n i (j-1) p // also try another j when unsafe - end; -// this concise backtracking tailrecursive version throws a single solution -tailqueens n::int = catch id (srch n n n []) - with srch _ 0 _ p = throw p; - srch _ _ 0 _ = failed; - srch n::int i::int j::int p = if safe 1 j p then - ( if failed === (srch n (i-1) n (j:p)) then srch n i (j-1) p else () ) - else srch n i (j-1) p - end; - -/* -thequeens encodes my no search solution, which is to my knowledge the simplest -known algorithm for this problem. -There always exists one fundamental centre-symmetrical solution of this form, -representing an orbit of just 4 reflected solutions, instead of the usual 8. -These few lines of code are self-contained (not calling any square checking). -The solutions had been tested exhaustively for board sizes 0 to 5000 and also -individually for board size 50000x50000. - -Row numbering in 'thequeens' is changed for simplicity to 'C style' 0..n-1 -Solution using 2D board coordinates (1..n)x(1..n) can be easily reconstructed -with: (fullboard (thequeens n)). -*/ - -fullboard simple = zip (1..(#simple)) (map succ simple); - -nullary nosolution; // returned for n=2 and n=3 when there are no solutions - -thequeens n::int = case n of - 1 = [0]; // trivial solution to one square board - 2 | 3 = nosolution; - n::int = map (newsquare n) (0..(n-1)) // rule for even sized boards n>3 - with newsquare n::int x::int - = (start+2*x) mod n if x < halfn; // right start square is crucial - = (start2+2*(x-halfn)) mod n // centre reflections fill the 2nd half - end - when - halfn::int = n div 2; // local variable halfn - start::int = if (n mod 3) then (halfn-1) else 1;//(n mod 3) is special - start2::int = n-((start + 2*(halfn-1)) mod n)-1 // start reflections - end if (n mod 2) == 0; // even sized boards finished - = 0:(map succ (thequeens (n-1))) // corner start 0: solves odd size boards! -end; // end of case and thequeens - - -// The rest are test utilities for the queens problem: -// checks one queens solution either in 0..7 encoding or in 1..8 encoding. -// returns 1 for a correct result, including "nosolution" for sizes 2 and 3. -// returns 0 if a queen attack exists anywhere in the presented 'solution': -checkqs [] = 1; -checkqs (s::int:l) = if safe 1 s l then checkqs l else 0; -checkqs (nosolution) = 1; - -// conducts an exhaustive test of solutions for boards of all listed sizes. -// examples of use: >queenstest (1..1000); >queenstest (5000,4999..4990); -queenstest [] = 1; -queenstest (h:l) = if checkqs (thequeens h) then queenstest l else 0; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ye...@us...> - 2008-06-30 01:44:33
|
Revision: 340 http://pure-lang.svn.sourceforge.net/pure-lang/?rev=340&view=rev Author: yes Date: 2008-06-29 18:44:39 -0700 (Sun, 29 Jun 2008) Log Message: ----------- created subdir 'libor' in examples with two files: myutils.pure and queens.pure Added Paths: ----------- pure/trunk/examples/libor/myutils.pure pure/trunk/examples/libor/queens.pure Added: pure/trunk/examples/libor/myutils.pure =================================================================== --- pure/trunk/examples/libor/myutils.pure (rev 0) +++ pure/trunk/examples/libor/myutils.pure 2008-06-30 01:44:39 UTC (rev 340) @@ -0,0 +1,31 @@ +// Dr Libor Spacek, 21th May 2008 + +//General mathematical iterators over one and two indices +MathIter1 op i1 i2 f = foldl1 op (map f (i1..i2)); +MathIter2 op i1 i2 j1 j2 f = + foldl1 op (map (uncurry f) [x,y; x = i1..i2; y = j1..j2]); +//Examples on how to use the mathematical iterators +Sigma i1 i2 f = MathIter1 (+) i1 i2 f; +Pi i1 i2 f = MathIter1 (*) i1 i2 f; +Factorial n = Pi 1L n id; +//Binomial using (k, n-k) symmetry and bignum division +Binomial n k = (Pi (k+1L) n id) div (Pi 2L (n-k) id) if n-k < k; + = (Pi (n-k+1L) n id) div (Pi 2L k id); + +// Euclid's recursive greatest common factor algorithm for ints and bignums +Gcf x 0 | Gcf x 0L = x; +Gcf x y = Gcf y (x mod y); + +// take the head of a list and put it at the end +rotate (h:t) = reverse (h:(reverse t)); +// protate = rotate n items from the front: use when n is positive: 0<=n<=#n +protate 0 l = l; +protate n::int l = cat [(drop n l),(take n l)]; +// rotate n items, generalisation of "rotate the bits instruction" +// example: head (nrotate (-33) (0..23)); +// what time is 33 hrs before midnight? 15 hrs. +// The clock was moved -33 mod 24 = -9 hours from midnight (0) +nrotate n::int l = protate nm l when ll = #l; nm = ll + (n mod ll) end if n<0; + = protate nm l when nm = n mod #l end; + + Added: pure/trunk/examples/libor/queens.pure =================================================================== --- pure/trunk/examples/libor/queens.pure (rev 0) +++ pure/trunk/examples/libor/queens.pure 2008-06-30 01:44:39 UTC (rev 340) @@ -0,0 +1,96 @@ +/* Several Solutions to the Queens Problem Dr Libor Spacek, 21th May 2008 + + (allqueens n) returns all solutions but is slow + (queens n) and (tailqueens n) return one different solution each + (thequeens n) does no search and is very fast even for large boards + +Examples: + + >allqueens 8; // returns all 92 solutions, as a list of lists + >queens 8; // gives solution number 52 in the allqueens' list, + >tailqueens 8; // gives solution no. 89, which is a reflection of no. 52 + >map succ (thequeens 8); // gives solution no. 56 */ + +// increment and decrement general utility +succ x::int = 1+x; pred x::int = x-1; + +// row j in current column not attacked by any queens in preceding columns? +safe _ _ [] = 1; +safe id::int j::int (j2::int:l) = // id is the column positions difference + if (j==j2) || (id==j2-j) || (id==j-j2) then 0 else safe (1+id) j l; + +allqueens n::int = list (searchall n n []) // returns all possible solutions + with + searchall n::int 0 p = p; + searchall n::int i::int p = + tuple [searchall n (i-1) (j:p); j = 1..n; safe 1 j p] + end; + +// the solution is only the rows permutation, without the ordered columns (1..n) +// full 2D board coordinates can be reconstructed with zip (1..n) (queens n); +nullary failed; +queens n::int = list (search n n n []) + with + search _ 0 _ p = (); // last i, solved + search _ _ 0 _ = failed; // failed, run out of alternative js + search n::int i::int j::int p = + if (failed === solution) then search n i (j-1) p else j,solution + when solution = search n (i-1) n (j:p); end if safe 1 j p; + = search n i (j-1) p // also try another j when unsafe + end; +// this concise backtracking tailrecursive version throws a single solution +tailqueens n::int = catch id (srch n n n []) + with srch _ 0 _ p = throw p; + srch _ _ 0 _ = failed; + srch n::int i::int j::int p = if safe 1 j p then + ( if failed === (srch n (i-1) n (j:p)) then srch n i (j-1) p else () ) + else srch n i (j-1) p + end; + +/* +thequeens encodes my no search solution, which is to my knowledge the simplest +known algorithm for this problem. +There always exists one fundamental centre-symmetrical solution of this form, +representing an orbit of just 4 reflected solutions, instead of the usual 8. +These few lines of code are self-contained (not calling any square checking). +The solutions had been tested exhaustively for board sizes 0 to 5000 and also +individually for board size 50000x50000. + +Row numbering in 'thequeens' is changed for simplicity to 'C style' 0..n-1 +Solution using 2D board coordinates (1..n)x(1..n) can be easily reconstructed +with: (fullboard (thequeens n)). +*/ + +fullboard simple = zip (1..(#simple)) (map succ simple); + +nullary nosolution; // returned for n=2 and n=3 when there are no solutions + +thequeens n::int = case n of + 1 = [0]; // trivial solution to one square board + 2 | 3 = nosolution; + n::int = map (newsquare n) (0..(n-1)) // rule for even sized boards n>3 + with newsquare n::int x::int + = (start+2*x) mod n if x < halfn; // right start square is crucial + = (start2+2*(x-halfn)) mod n // centre reflections fill the 2nd half + end + when + halfn::int = n div 2; // local variable halfn + start::int = if (n mod 3) then (halfn-1) else 1;//(n mod 3) is special + start2::int = n-((start + 2*(halfn-1)) mod n)-1 // start reflections + end if (n mod 2) == 0; // even sized boards finished + = 0:(map succ (thequeens (n-1))) // corner start 0: solves odd size boards! +end; // end of case and thequeens + + +// The rest are test utilities for the queens problem: +// checks one queens solution either in 0..7 encoding or in 1..8 encoding. +// returns 1 for a correct result, including "nosolution" for sizes 2 and 3. +// returns 0 if a queen attack exists anywhere in the presented 'solution': +checkqs [] = 1; +checkqs (s::int:l) = if safe 1 s l then checkqs l else 0; +checkqs (nosolution) = 1; + +// conducts an exhaustive test of solutions for boards of all listed sizes. +// examples of use: >queenstest (1..1000); >queenstest (5000,4999..4990); +queenstest [] = 1; +queenstest (h:l) = if checkqs (thequeens h) then queenstest l else 0; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ye...@us...> - 2008-07-03 11:04:03
|
Revision: 378 http://pure-lang.svn.sourceforge.net/pure-lang/?rev=378&view=rev Author: yes Date: 2008-07-03 04:04:10 -0700 (Thu, 03 Jul 2008) Log Message: ----------- fixed some minor typos in comments, added Mayan Calendar curiosity, see added file examples/libor/date.pure Modified Paths: -------------- pure/trunk/examples/libor/myutils.pure pure/trunk/examples/libor/queens.pure Added Paths: ----------- pure/trunk/examples/libor/date.pure Added: pure/trunk/examples/libor/date.pure =================================================================== --- pure/trunk/examples/libor/date.pure (rev 0) +++ pure/trunk/examples/libor/date.pure 2008-07-03 11:04:10 UTC (rev 378) @@ -0,0 +1,57 @@ +// Mayan Calendar - Copyright (c) 2008 by Libor Spacek +// Usage: pure -x date.pure [-h]] + +using system; +extern int time(); + +puts "Mayan Calendar, Copyright (c) 2008 by Libor Spacek"; + +def posixepoch = (12:17:16:7:5); // Mayan long count date of the posix epoch +def epochday = mayan2days posixepoch; // Mayan day of the posix epoch +def endofdays = 15695; // posix days at the end of the cycle (13th baktun) +def secsinday = 86400; // number of seconds in a day +def cycledays = mayan2days (13:0:0:0:0); + +// time now in posix seconds converted to whole days +posixdays = tm div secsinday when tm = time end; + +// not used yet but could be: addmayan posixepoch (days2mayan posixdays) +addmayan (baktun1::int:katun1::int:tun1::int:vinal1::int:kin1::int) + (baktun2::int:katun2::int:tun2::int:vinal2::int:kin2::int) = + baktun:(katun mod 20):(tun mod 20):(vinal mod 18):(kin mod 20) + when + kin = kin1+kin2; vinal = vinal1+vinal2+(kin div 20); + tun = tun1+tun2+(vinal div 18); katun = katun1+katun2+(tun div 20); + baktun = baktun1+baktun2+(katun div 20) + end; + +days2mayan d::int = baktun:(katun mod 20):(tun mod 20):(vinal mod 18):(d mod 20) + when + vinal =d div 20; tun =vinal div 18; katun =tun div 20; baktun =katun div 20 + end; + +mayan2days (baktun::int:katun::int:tun::int:vinal::int:kin::int) = + 20*(18*(20*(20*baktun+katun)+tun)+vinal)+kin; + +// simple calculations to print +daytoday = epochday + posixdays; +mayantoday = days2mayan daytoday; +daysleft = endofdays - posixdays; +mayanleft = days2mayan daysleft; +percentcomplete = 100.0 * daytoday / cycledays; + +usage = puts "Usage: pure -x date.pure [anyarg]" $ + puts "\tanyarg for help"; + +case argc of + 1 = void (printf "Mayan long count today: %s = day %d of this cycle\n" + ((str mayantoday), (mayan2days mayantoday))) $ + void (printf "Mayan count down today: %s = %d days left till the end\n" + ((str mayanleft), daysleft)) $ + void (printf "The Cycle is %f%% complete!\n" percentcomplete); + 2 = void (puts "Mayan long count digits (and their range of values):") $ + void (puts "Baktun(0-12):Katun(0-19):Tun(0-19):Vinal(0-17):Kin(0-19)")$ + puts "Baktun=144000days:Katun=7200days:Tun=360days:Vinal=20days:Kin=1day"$ + usage; + n = usage otherwise +end; Modified: pure/trunk/examples/libor/myutils.pure =================================================================== --- pure/trunk/examples/libor/myutils.pure 2008-07-03 05:19:00 UTC (rev 377) +++ pure/trunk/examples/libor/myutils.pure 2008-07-03 11:04:10 UTC (rev 378) @@ -57,6 +57,7 @@ d = h div 24 end; -// an arbitrary binary operator applied to two days:hours:minutes:seconds +// an arbitrary binary operator applied to two (days,hours,minutes,seconds) opdhms op (d1::int,h1::int,m1::int,s1)(d2::int,h2::int,m2::int,s2) = - secs2dhms (op (dhms2secs (d1,h1,m1,s1)) (dhms2secs (d2,h2,m2,s2))); \ No newline at end of file + secs2dhms (op (dhms2secs (d1,h1,m1,s1)) (dhms2secs (d2,h2,m2,s2))); + \ No newline at end of file Modified: pure/trunk/examples/libor/queens.pure =================================================================== --- pure/trunk/examples/libor/queens.pure 2008-07-03 05:19:00 UTC (rev 377) +++ pure/trunk/examples/libor/queens.pure 2008-07-03 11:04:10 UTC (rev 378) @@ -77,8 +77,8 @@ halfn::int = n div 2; // local variable halfn start::int = if (n mod 3) then (halfn-1) else 1;//(n mod 3) is special start2::int = n-((start + 2*(halfn-1)) mod n)-1 // start reflections - end if (n mod 2) == 0; // even sized boards finished - = 0:(map succ (thequeens (n-1))) // corner start 0: solves odd size boards! + end if (n mod 2) == 0; // even sized boards finished + = 0:(map succ (thequeens (n-1))) // corner start 0: solves odd size boards! end; // end of case and thequeens This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ye...@us...> - 2008-07-04 00:07:44
|
Revision: 379 http://pure-lang.svn.sourceforge.net/pure-lang/?rev=379&view=rev Author: yes Date: 2008-07-03 17:07:53 -0700 (Thu, 03 Jul 2008) Log Message: ----------- Moved time calculation utilities from myutils.pure to date.pure and added up-to-the-second time functionality to date.pure Modified Paths: -------------- pure/trunk/examples/libor/date.pure pure/trunk/examples/libor/myutils.pure Modified: pure/trunk/examples/libor/date.pure =================================================================== --- pure/trunk/examples/libor/date.pure 2008-07-03 11:04:10 UTC (rev 378) +++ pure/trunk/examples/libor/date.pure 2008-07-04 00:07:53 UTC (rev 379) @@ -1,6 +1,11 @@ -// Mayan Calendar - Copyright (c) 2008 by Libor Spacek -// Usage: pure -x date.pure [-h]] +/* Mayan Calendar - Copyright (c) 2008 by Libor Spacek + Usage: pure -x date.pure [-h] + + Discrepancies with your local clock may occur when C library's time(); + returns Posix time based on UTC (Universel Temps Coordonné) or TAI + (Temps Atomique Internacional) rather than local daylight saving time */ + using system; extern int time(); @@ -11,11 +16,32 @@ def endofdays = 15695; // posix days at the end of the cycle (13th baktun) def secsinday = 86400; // number of seconds in a day def cycledays = mayan2days (13:0:0:0:0); +def year = 365.242374; +def cycleyears = cycledays / year; // time now in posix seconds converted to whole days -posixdays = tm div secsinday when tm = time end; +posixsecsnow = time; // call posixsecsnow to refresh the current time +posixdays = posixsecsnow div secsinday; +secsnow = posixsecsnow mod secsinday; + +// time calculations on the usual days:hours:minutes:seconds format +dhms2secs (d::int:h::int:m::int:s) = 60.0*(60*(24*d+h)+m)+s; +// secs are usually double and can be int or bigint but d,h,m are always ints +secs2dhms secs = + d:(h mod 24):(m mod 60):(secs-60.0*m) + when + m::int = int (secs / 60); + h::int = m div 60; + d::int = h div 24 + end; + +// an arbitrary binary operator applied to two (days,hours,minutes,seconds) +opdhms op (d1::int:h1::int:m1::int:s1)(d2::int:h2::int:m2::int:s2) = + secs2dhms (op (dhms2secs (d1:h1:m1:s1)) (dhms2secs (d2:h2:m2:s2))); + +// Now follows the Mayan Calendar -// not used yet but could be: addmayan posixepoch (days2mayan posixdays) +// not used yet but could be, as in: addmayan posixepoch (days2mayan posixdays) addmayan (baktun1::int:katun1::int:tun1::int:vinal1::int:kin1::int) (baktun2::int:katun2::int:tun2::int:vinal2::int:kin2::int) = baktun:(katun mod 20):(tun mod 20):(vinal mod 18):(kin mod 20) @@ -38,17 +64,20 @@ mayantoday = days2mayan daytoday; daysleft = endofdays - posixdays; mayanleft = days2mayan daysleft; -percentcomplete = 100.0 * daytoday / cycledays; +percentcomplete = 100.0*(epochday+posixsecsnow/secsinday)/cycledays; usage = puts "Usage: pure -x date.pure [anyarg]" $ puts "\tanyarg for help"; case argc of - 1 = void (printf "Mayan long count today: %s = day %d of this cycle\n" - ((str mayantoday), (mayan2days mayantoday))) $ - void (printf "Mayan count down today: %s = %d days left till the end\n" - ((str mayanleft), daysleft)) $ - void (printf "The Cycle is %f%% complete!\n" percentcomplete); + 1 = + void (printf "Posix time now: %s\n" (str (secs2dhms posixsecsnow))) $ + void (printf "Mayan long count date: %s = day %d\n" + ((str mayantoday), (mayan2days mayantoday))) $ + void (printf "Mayan countdown today: %s = %d days till the cycle ends\n" + ((str mayanleft), daysleft)) $ + void (printf "The Mayan cycle of over %d years " (int cycleyears)) $ + void (printf "is now %11.8f%% complete!\n" percentcomplete); 2 = void (puts "Mayan long count digits (and their range of values):") $ void (puts "Baktun(0-12):Katun(0-19):Tun(0-19):Vinal(0-17):Kin(0-19)")$ puts "Baktun=144000days:Katun=7200days:Tun=360days:Vinal=20days:Kin=1day"$ Modified: pure/trunk/examples/libor/myutils.pure =================================================================== --- pure/trunk/examples/libor/myutils.pure 2008-07-03 11:04:10 UTC (rev 378) +++ pure/trunk/examples/libor/myutils.pure 2008-07-04 00:07:53 UTC (rev 379) @@ -36,28 +36,3 @@ // what time is 33 hrs before midnight? Answer: 15 hrs. nrotate n::int l = protate nm l when ll = #l; nm = ll + (n mod ll) end if n<0; = protate nm l when nm = n mod #l end; - -//(3) Time Calculations - seconds can be int or double or bigint. d,h,m are ints -dhms2secs (d::int,h::int,m::int,s::int) | -dhms2secs (d::int,h::int,m::int,s::double) | -dhms2secs (d::int,h::int,m::int,s::bigint) = 60*(60*(24*d+h)+m)+s; - -secs2dhms secs::int | secs2dhms secs::bigint = - (int d),(int (h mod 24)),(int (m mod 60)),(int (secs mod 60)) - when - m = secs div 60; - h = m div 60; - d = h div 24 - end; -secs2dhms secs::double = - (int d),(int (h mod 24)),(int (m mod 60)),(int (isecs mod 60))+(secs-isecs) - when isecs = (bigint secs); - m = isecs div 60; - h = m div 60; - d = h div 24 - end; - -// an arbitrary binary operator applied to two (days,hours,minutes,seconds) -opdhms op (d1::int,h1::int,m1::int,s1)(d2::int,h2::int,m2::int,s2) = - secs2dhms (op (dhms2secs (d1,h1,m1,s1)) (dhms2secs (d2,h2,m2,s2))); - \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ye...@us...> - 2008-07-07 00:32:34
|
Revision: 408 http://pure-lang.svn.sourceforge.net/pure-lang/?rev=408&view=rev Author: yes Date: 2008-07-06 17:32:43 -0700 (Sun, 06 Jul 2008) Log Message: ----------- use init in date.pure, improve some indentation in myutils.pure Modified Paths: -------------- pure/trunk/examples/libor/date.pure pure/trunk/examples/libor/myutils.pure Modified: pure/trunk/examples/libor/date.pure =================================================================== --- pure/trunk/examples/libor/date.pure 2008-07-07 00:14:35 UTC (rev 407) +++ pure/trunk/examples/libor/date.pure 2008-07-07 00:32:43 UTC (rev 408) @@ -35,7 +35,7 @@ secsnow = time mod secsinday; // int seconds since midnight // strip the inconvenient \n off strings given by ctime, gmtime -stripnl s::string = reverse (tail (reverse s)); +stripnl s::string = init s; // either mayan or julian posix epoch (and posix time) as a mjday number mjday epoch::int secs::int |mjday epoch::int secs::bigint= epoch+secs/secsinday; Modified: pure/trunk/examples/libor/myutils.pure =================================================================== --- pure/trunk/examples/libor/myutils.pure 2008-07-07 00:14:35 UTC (rev 407) +++ pure/trunk/examples/libor/myutils.pure 2008-07-07 00:32:43 UTC (rev 408) @@ -1,7 +1,5 @@ -/* General Utilities - Copyright (c) 2008 by Libor Spacek */ +/* General Utilities Copyright (c) 2008 by Libor Spacek */ -//(1) Mathematics //General mathematical iterators over one and two indices MathIter1 op i1 i2 f = foldl1 op (map f (i1..i2)); @@ -10,14 +8,12 @@ //Examples on how to use the mathematical iterators Sigma i1 i2 f = MathIter1 (+) i1 i2 f; - Pi i1 i2 f = MathIter1 (*) i1 i2 f; - Factorial n = Pi 1L n id; //Binomial using (k, n-k) symmetry and bignum division Binomial n k = (Pi (k+1L) n id) div (Pi 2L (n-k) id) if n-k < k; - = (Pi (n-k+1L) n id) div (Pi 2L k id); + = (Pi (n-k+1L) n id) div (Pi 2L k id); // Euclid's recursive greatest common factor algorithm for ints and bignums Gcf x 0 | Gcf x 0L = x; @@ -34,5 +30,7 @@ // rotate n items, cf. "rotate n bits instruction" (n can now also be negative) // example applied to clocks: >head (nrotate (-33) (0..23)); // what time is 33 hrs before midnight? Answer: 15 hrs. -nrotate n::int l = protate nm l when ll = #l; nm = ll + (n mod ll) end if n<0; - = protate nm l when nm = n mod #l end; +nrotate n::int l + = protate nm l when ll = #l; nm = ll + (n mod ll) end if n<0; + = protate nm l when nm = n mod #l end; + \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ye...@us...> - 2008-07-09 09:28:28
|
Revision: 427 http://pure-lang.svn.sourceforge.net/pure-lang/?rev=427&view=rev Author: yes Date: 2008-07-09 02:28:37 -0700 (Wed, 09 Jul 2008) Log Message: ----------- fixed 'timex', removed succ and pred from queens.pure, as they are now in prelude.pure Modified Paths: -------------- pure/trunk/examples/libor/date.pure pure/trunk/examples/libor/queens.pure Modified: pure/trunk/examples/libor/date.pure =================================================================== --- pure/trunk/examples/libor/date.pure 2008-07-09 04:06:57 UTC (rev 426) +++ pure/trunk/examples/libor/date.pure 2008-07-09 09:28:37 UTC (rev 427) @@ -27,8 +27,8 @@ /******************************************************************************/ // first a couple of functions generally useful in Pure: -// timer function: (timex (id (foo arg))) returns cputime, value of (foo arg) -timex fn = (clock-t)/CLOCKS_PER_SEC $ res when t = clock; res = fn end; +// Albert Graef's timer function: returns cputime, value of (f onearg) +timex f x = (clock-t0)/CLOCKS_PER_SEC, res when t0 = clock; res = f x end; /* extended mod operator to work on doubles, so that int, bigint and double times can be conveniently used */ Modified: pure/trunk/examples/libor/queens.pure =================================================================== --- pure/trunk/examples/libor/queens.pure 2008-07-09 04:06:57 UTC (rev 426) +++ pure/trunk/examples/libor/queens.pure 2008-07-09 09:28:37 UTC (rev 427) @@ -11,9 +11,6 @@ >tailqueens 8; // gives solution no. 89, which is a reflection of no. 52 >map succ (thequeens 8); // gives solution no. 56 */ -// increment and decrement general utility -succ x::int = 1+x; pred x::int = x-1; - // row j in current column not attacked by any queens in preceding columns? safe _ _ [] = 1; safe id::int j::int (j2::int:l) = // id is the column positions difference This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |