Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,4 @@
hailstone n
| n == 1 = 1
| even n = n `div` 2
| odd n = 3*n + 1

View file

@ -0,0 +1,2 @@
data Environment = Environment { count :: Int, value :: Int }
deriving Eq

View file

@ -0,0 +1 @@
environments = [ Environment 0 n | n <- [1..12] ]

View file

@ -0,0 +1,2 @@
process (Environment c 1) = Environment c 1
process (Environment c n) = Environment (c+1) (hailstone n)

View file

@ -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 }

View file

@ -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)

View file

@ -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