-- Copyright 2003 Mike MacHenry -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by -- the Free Software Foundation; either version 2 of the License, or -- (at your option) any later version. -- This program is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- GNU General Public License for more details. -- You should have received a copy of the GNU General Public License -- along with this program; if not, write to the Free Software -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -- The license is at . -- sneaker.hs v0.0.1 -- dskippy 2003/02/04 -- Mike MacHenry ----------------------------------------------------------------------- -- This is my version of the sneaker program written in Haskell. I -- wrote it in Haskell simply to learn Haskell because it is so cool -- and to contribute to the ever growing collection of sneaker programs -- (see http://www.netgeek.ws) There are a few difference about this -- sneaker program from the rest (most due to it's Haskellian nature) -- -- One important thing to note is that it improves on a previously -- unnoticed bug in existing sneaker programs. Psuedo-random number -- generators are ment to be seeded in a program to give it determinism -- but then applied over and over again (often using a next operation) -- They are not intened to be reseeded over and over again. It has been -- show that the function f(n) = seed n; rand; does not exhibit random -- behavior where as f(n) = rand; does. As a result I wrote this sneaker -- program fixing it in a very Haskellian way. I get the current date -- and seed the random number generator with the year. Then I get -- generate an infinite list of pairs of sneakers and index it with the -- number of days since the begining of the year. This gives a more -- random distribution of the sneakers to be worn on each day ---------------------------------------------------------------------- -- TODO: make work for any given date, not just current. import Random import Time data Color = Blue | Red | Green | Khaki | White | Black | Orange | Yellow deriving (Enum, Show, Bounded) data Pair = Pair Color Color instance Random Color where random = randomR (minBound,maxBound) randomR (lo,hi) gen = (toEnum val, gen') where (val,gen') = randomR (fromEnum lo, fromEnum hi) gen instance Show Pair where show (Pair left right) = "Left: " ++ show left ++ "\nRight: " ++ show right today :: IO CalendarTime today = getClockTime >>= toCalendarTime randomPairs :: RandomGen g => g -> [Pair] randomPairs gen = zipWith Pair (randoms lgen) (randoms rgen) where (lgen, rgen) = split gen main :: IO () main = do day <- today putStrLn (calendarTimeToString day) print (randomPairs (mkStdGen (ctYear day)) !! ctYDay day)