Guest User

Untitled

a guest
Jul 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. readInt :: String -> Int
  2. readInt = read
  3.  
  4. splitBy :: Char -> String -> [String]
  5. splitBy _ "" = []
  6. splitBy ch s = let (l, s') = break (== ch) s
  7. in l : case s' of
  8. [] -> []
  9. (_:s'') -> splitBy ch s''
  10.  
  11. hasDelimiter :: String -> Bool
  12. hasDelimiter text = length text > 2 && 2 == (length . takeWhile (==True) $ zipWith (==) "//" text)
  13.  
  14. delimiter :: String -> Char
  15. delimiter text
  16. | hasDelimiter text = text !! 2
  17. | otherwise = '\n'
  18.  
  19.  
  20. calc :: String -> Int
  21. calc text = sum . map readInt . numbers . body $ text
  22. where numbers = splitBy (delimiter text)
  23. body text = if hasDelimiter text then drop 3 text else text
Add Comment
Please, Sign In to add comment