# agbell's solution

## to Phone Number in the Haskell Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Clean up user-entered phone numbers so that they can be sent SMS messages.

The North American Numbering Plan (NANP) is a telephone numbering system used by many countries in North America like the United States, Canada or Bermuda. All NANP-countries share the same international country code: `1`.

NANP numbers are ten-digit numbers consisting of a three-digit Numbering Plan Area code, commonly known as area code, followed by a seven-digit local number. The first three digits of the local number represent the exchange code, followed by the unique four-digit number which is the subscriber number.

The format is usually represented as

``````(NXX)-NXX-XXXX
``````

where `N` is any digit from 2 through 9 and `X` is any digit from 0 through 9.

Your task is to clean up differently formatted telephone numbers by removing punctuation and the country code (1) if present.

For example, the inputs

• `+1 (613)-995-0253`
• `613-995-0253`
• `1 613 995 0253`
• `613.995.0253`

should all produce the output

`6139950253`

Note: As this exercise only deals with telephone numbers used in NANP-countries, only 1 is considered a valid country code.

## Getting Started

For installation and learning resources, refer to the exercism help page.

## Running the tests

To run the test suite, execute the following command:

``````stack test
``````

#### If you get an error message like this...

``````No .cabal file found in directory
``````

You are probably running an old stack version and need to upgrade it.

#### Otherwise, if you get an error message like this...

``````No compiler found, expected minor version match with...
Try running "stack setup" to install the correct GHC...
``````

Just do as it says and it will download and install the correct compiler version:

``````stack setup
``````

## Running GHCi

If you want to play with your solution in GHCi, just run the command:

``````stack ghci
``````

## Feedback, Issues, Pull Requests

The exercism/haskell repository on GitHub is the home for all of the Haskell exercises.

If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue. We'll do our best to help you!

## Source

Event Manager by JumpstartLab http://tutorials.jumpstartlab.com/projects/eventmanager.html

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### Tests.hs

``````{-# LANGUAGE RecordWildCards #-}

import Data.Foldable     (for_)
import Test.Hspec        (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)

import Phone (number)

main :: IO ()
main = hspecWith defaultConfig {configFastFail = True} specs

specs :: Spec
specs = describe "number" \$ for_ cases test
where
test Case{..} = it description \$ number input `shouldBe` expected

data Case = Case { description ::       String
, input       ::       String
, expected    :: Maybe String
}

cases :: [Case]
cases =
[ Case { description = "cleans the number"
, input       = "(223) 456-7890"
, expected    = Just "2234567890"
}
, Case { description = "cleans numbers with dots"
, input       = "223.456.7890"
, expected    = Just "2234567890"
}
, Case { description = "cleans numbers with multiple spaces"
, input       = "223 456   7890   "
, expected    = Just "2234567890"
}
, Case { description = "invalid when 9 digits"
, input       = "123456789"
, expected    = Nothing
}
, Case { description = "invalid when 11 digits does not start with a 1"
, input       = "22234567890"
, expected    = Nothing
}
, Case { description = "valid when 11 digits and starting with 1"
, input       = "12234567890"
, expected    = Just "2234567890"
}
, Case { description = "valid when 11 digits and starting with 1 even with punctuation"
, input       = "+1 (223) 456-7890"
, expected    = Just "2234567890"
}
, Case { description = "invalid when more than 11 digits"
, input       = "321234567890"
, expected    = Nothing
}
, Case { description = "invalid with letters"
, input       = "123-abc-7890"
, expected    = Nothing
}
, Case { description = "invalid with punctuations"
, input       = "123-@:!-7890"
, expected    = Nothing
}
, Case { description = "invalid if area code starts with 0"
, input       = "(023) 456-7890"
, expected    = Nothing
}
, Case { description = "invalid if area code starts with 1"
, input       = "(123) 456-7890"
, expected    = Nothing
}
, Case { description = "invalid if exchange code starts with 0"
, input       = "(223) 056-7890"
, expected    = Nothing
}
, Case { description = "invalid if exchange code starts with 1"
, input       = "(223) 156-7890"
, expected    = Nothing
}
]``````
``````module Phone (areaCode, number, prettyPrint)
where

import Text.ParserCombinators.Parsec
import Text.Printf
import Data.Char

data Phone = Phone { area :: String, num1 :: String, num2 :: String }

areaCode :: String -> String
areaCode s = area \$ readExpr s

number :: String -> String
number s = normalPrint \$ readExpr s

prettyPrint :: String -> String
prettyPrint s = pretty \$ readExpr s
where
pretty (Phone a n1 n2) = printf "(%s) %s-%s" a n1 n2

normalPrint :: Phone -> String
normalPrint (Phone a n1 n2) = a ++ n1 ++ n2

readExpr input = either (const \$ Phone "000" "000" "0000") id (parse parse1 "" input)
where
parse1 = try (nonNumber >> char '1' >> parsePhone) <|> parsePhone
parsePhone = do
nonNumber
a <- count 3 digit
nonNumber
n1 <- count 3 digit
nonNumber
n2 <- count 4 digit
nonNumber
eof
return \$ Phone a n1 n2
nonNumber = skipMany \$ satisfy (not . isDigit)``````