Ticket #2484: randil.hs

File randil.hs, 1.1 KB (added by knarF, 7 years ago)
Line 
1#!/usr/local/bin/runhaskell
2
3import System.Locale
4import System.Random
5import System.Time
6
7main :: IO ()
8main = do
9  clockTime <- getClockTime
10  comicNumber <- getStdRandom (randomR (0,comics clockTime))
11  putStrLn (command comicNumber)
12
13command :: Int -> String
14command n = "open http://www.dilbert.com/fast/" ++ formatCalendarTime defaultTimeLocale (iso8601DateFormat Nothing) (toUTCTime (nthComicClockTime n))
15
16nthComicClockTime :: Int -> ClockTime
17nthComicClockTime n = addToClockTime (TimeDiff 0 0 n 0 0 0 0) firstComicClockTime
18
19firstComicClockTime :: ClockTime
20firstComicClockTime = toClockTime (CalendarTime {ctYear = 1996, ctMonth = January, ctDay = 1, ctHour = 0, ctMin = 0, ctSec = 0, ctPicosec = 0, ctWDay = undefined, ctYDay = undefined, ctTZName = undefined, ctTZ = 0, ctIsDST = undefined})
21
22comics :: ClockTime -> Int
23comics clockTime = comics' 1
24 where
25  comics' n = if nthComicClockTime d > clockTime then comics'' n d else comics' d
26   where
27    d = 2 * n
28  comics'' a z = if a + 1 == z
29    then z
30    else if nthComicClockTime(m) > clockTime
31      then comics'' a m
32      else comics'' m z
33   where
34    m = (a + z) `div` 2