Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module PhoneNumber where
- import Prelude
- import Data.Char.Unicode (isDigit)
- import Data.Array (filter, head, tail, length)
- import Data.String (toCharArray, fromCharArray)
- import Data.Maybe (Maybe(..))
- clean :: String -> Maybe (Array Char)
- clean input = go where
- go
- | len == 10 = Just digits
- | len == 11 && head digits == Just '1' = tail digits
- | otherwise = Nothing
- digits = filter isDigit $ toCharArray input
- len = length digits
- check:: Array Char -> Maybe String
- check ['0', _] = Nothing
- check ['1', _] = Nothing
- check [ _ , _ , _ , '0', _] = Nothing
- check [ _ , _ , _ , '1', _] = Nothing
- check s = Just $ fromCharArray s
- phoneNumber :: String -> Maybe String
- phoneNumber input = clean input >>= check
Add Comment
Please, Sign In to add comment