Guest User

Untitled

a guest
Jan 20th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. module PhoneNumber where
  2.  
  3. import Prelude
  4. import Data.Char.Unicode (isDigit)
  5. import Data.Array (filter, head, tail, length)
  6. import Data.String (toCharArray, fromCharArray)
  7. import Data.Maybe (Maybe(..))
  8.  
  9. clean :: String -> Maybe (Array Char)
  10. clean input = go where
  11. go
  12. | len == 10 = Just digits
  13. | len == 11 && head digits == Just '1' = tail digits
  14. | otherwise = Nothing
  15. digits = filter isDigit $ toCharArray input
  16. len = length digits
  17.  
  18. check:: Array Char -> Maybe String
  19. check ['0', _] = Nothing
  20. check ['1', _] = Nothing
  21. check [ _ , _ , _ , '0', _] = Nothing
  22. check [ _ , _ , _ , '1', _] = Nothing
  23. check s = Just $ fromCharArray s
  24.  
  25.  
  26. phoneNumber :: String -> Maybe String
  27. phoneNumber input = clean input >>= check
Add Comment
Please, Sign In to add comment