Advertisement
joker546645

Haskell #solo

Dec 16th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.List
  2. import System.IO
  3. import Data.Array
  4.  
  5. collatz n
  6.      | mod n 2 == 0 = div n 2
  7.      | otherwise =  3 * n + 1
  8.  
  9. collatzList n
  10.         | n < 1 = error "Cannot have negative number"
  11.         | n == 1 = [n]
  12.         | otherwise = let sql = n:collatzList (collatz n)
  13.         in sql
  14.  
  15. search :: [Int] -> Int -> Int
  16. search (x:xs) y = if x == y then 1 else 1 + search xs y
  17.  
  18. length' :: (Num b) => [a] -> b  
  19. length' [] = 0  
  20. length' (_:xs) = 1 + length' xs
  21.  
  22. maximum' :: (Ord a) => [a] -> a  
  23. maximum' [] = error "maximum of empty list"  
  24. maximum' [x] = x  
  25. maximum' (x:xs)  
  26.     | x > maxTail = x  
  27.     | otherwise = maxTail  
  28.     where maxTail = maximum' xs
  29.  
  30. main :: IO ()
  31. main = do
  32. n <- getLine
  33. print (search (map length' (map collatzList [1..(read n :: Int)])) (maximum' (map length' (map collatzList [1..(read n :: Int)]))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement