Advertisement
osipyonok

Huiaskel additional 1-7

Feb 9th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Control.Monad
  2. import qualified Data.ByteString.Char8 as B
  3. import Data.Maybe
  4. import Data.List
  5. import Control.Arrow
  6.  
  7. readInt :: B.ByteString -> Int
  8. readInt = fst . fromJust . B.readInt
  9.  
  10. main :: IO ()
  11. main = do
  12.   [len] <- liftM (map readInt . B.words) B.getLine
  13.   lst   <- liftM (map readInt . take len . B.words) B.getLine :: IO [Int]
  14.   putStrLn "Input: "
  15.   print lst
  16.   putStrLn "Result: "
  17.   print (count_distinct lst)
  18.  
  19. count_distinct_impl []    _   ans = ans
  20. count_distinct_impl (h:t) cur ans | h == cur  = count_distinct_impl t h ans
  21.                          | otherwise = count_distinct_impl t h (ans + 1)
  22.  
  23. count_distinct []  = 0
  24. count_distinct lst = count_distinct_impl (tail sorted) (head sorted) 1
  25.         where sorted = sort lst
  26.  
  27. --5
  28. --3 -1 2 4 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement