Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,4 @@
|
|||
hailstone n
|
||||
| n == 1 = 1
|
||||
| even n = n `div` 2
|
||||
| odd n = 3*n + 1
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
data Environment = Environment { count :: Int, value :: Int }
|
||||
deriving Eq
|
||||
|
|
@ -0,0 +1 @@
|
|||
environments = [ Environment 0 n | n <- [1..12] ]
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
process (Environment c 1) = Environment c 1
|
||||
process (Environment c n) = Environment (c+1) (hailstone n)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
process = execState $ do
|
||||
n <- gets value
|
||||
c <- gets count
|
||||
when (n > 1) $ modify $ \env -> env { count = c + 1 }
|
||||
modify $ \env -> env { value = hailstone n }
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
fixedPoint f x
|
||||
| fx == x = [x]
|
||||
| otherwise = x : fixedPoint f fx
|
||||
where fx = f x
|
||||
|
||||
prettyPrint field = putStrLn . foldMap (format.field)
|
||||
where format n = (if n < 10 then " " else "") ++ show n ++ " "
|
||||
|
||||
main = do
|
||||
let result = fixedPoint (map process) environments
|
||||
mapM_ (prettyPrint value) result
|
||||
putStrLn (replicate 36 '-')
|
||||
prettyPrint count (last result)
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
main = do
|
||||
let result = map (fixedPoint process) environments
|
||||
mapM_ (prettyPrint value) result
|
||||
putStrLn (replicate 36 '-')
|
||||
putStrLn "Counts: "
|
||||
prettyPrint (count . last) result
|
||||
Loading…
Add table
Add a link
Reference in a new issue