-- Collatz sequence. See rosettacode.org/wiki/Hailstone_sequence#Haskell import Data.List (maximumBy) import Data.Ord (comparing) main :: IO () main = putStrLn $ "Collatz sequence for 27: " ++ (show.hailstone $ 27) ++ "\n" ++ "The number " ++ (show longestChain) ++" has the longest hailstone sequence" ++" for any number less then 100000. " ++"The sequence has length " ++ (show.length.hailstone $ longestChain) hailstone :: Integer -> [Integer] hailstone = takeWhile (/=1) . (iterate collatz) where collatz n = if even n then n `div` 2 else 3*n+1 longestChain :: Integer longestChain = fst $ maximumBy (comparing snd) $ map pairUp [1..100000] where pairUp i = (i, (length.hailstone) i) -- ------------For GNU Emacs ------------ -- Local Variables: -- compile-command: "ghc -Wall -O2 hail.hs" -- End: