RosettaCodeData/Task/Hailstone-sequence/Haskell/hailstone-sequence-2.hs

27 lines
597 B
Haskell
Raw Permalink Normal View History

2013-04-10 21:29:02 -07:00
import Data.Ord (comparing)
2017-09-23 10:01:46 +02:00
import Data.List (maximumBy, intercalate)
2013-04-10 21:29:02 -07:00
hailstone :: Int -> [Int]
2017-09-23 10:01:46 +02:00
hailstone 1 = [1]
hailstone n
| even n = n : hailstone (n `div` 2)
| otherwise = n : hailstone (n * 3 + 1)
2013-04-10 21:29:02 -07:00
2017-09-23 10:01:46 +02:00
withResult :: (Int -> Int) -> Int -> (Int, Int)
2013-04-10 21:29:02 -07:00
withResult f x = (f x, x)
2017-09-23 10:01:46 +02:00
h27 :: [Int]
h27 = hailstone 27
2013-04-10 21:29:02 -07:00
main :: IO ()
2017-09-23 10:01:46 +02:00
main =
mapM_
putStrLn
[ (show . length) h27
, "hailstone 27: " ++
intercalate " ... " (show <$> [take 4 h27, drop (length h27 - 4) h27])
, show $
maximumBy (comparing fst) $
withResult (length . hailstone) <$> [1 .. 100000]
]