Skip to content

Instantly share code, notes, and snippets.

@gatlin
Last active January 3, 2016 06:49
Show Gist options
  • Save gatlin/8425554 to your computer and use it in GitHub Desktop.
Save gatlin/8425554 to your computer and use it in GitHub Desktop.
According to this Time article [1], people who sleep between 6.5 and 7.5 hours a night live the longest. The following program computes optimal amounts of time to sleep for both naps and nighttime. The variable circadian was chosen to be 21.83 because for n = 8 it gives almost exactly 24 hours and for n = 6 it gives 7 hours and 14 minutes. This …
{-
- Usage: ./nap [INT]
- [INT] is some positive integer (eg 1, 2, etc)
-}
import System.Environment (getArgs)
import Control.Applicative ((<$>), (<*>), pure)
circadian = 21.83
naptime :: Double -> Double
naptime x = (circadian ** x) * (12 ** (1-x))
main :: IO ()
main = argv 0 >>= putStrLn . fmttime . naptime . read
where argv :: Int -> IO String
argv n = (!!) <$> getArgs <*> pure n
fmttime :: Double -> String
fmttime t = pr ++ (show ms) ++ " minutes"
where t' = fromIntegral . floor $ t
hs = t' `div` 60
ms = t' - (hs * 60)
pr = case hs of
0 -> ""
1 -> show hs ++ " hour and "
_ -> show hs ++ " hours and "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment