Avatar of mambocab
0
0
Genius
0
4

mambocab's solution

to Phone Number in the Haskell Track

Instructions
Test suite
Solution

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 Data.Char (isDigit)

normalizeInternational s
    | ((length s) == 10) = s
    | and [(length s == 11), (head s == '1')] = tail s
    | otherwise = "0000000000"

number s = normalizeInternational $ filter isDigit s

areaCode s = take 3 $ normalizeInternational s

prettyPrint s = "(" ++ areaCode ++ ") " ++ prefix ++ "-" ++ suffix
    where
        (areaCode, remainder) = splitAt 3 $ normalizeInternational s
        (prefix, suffix) = splitAt 3 remainder

What can you learn from this solution?

A huge amount can be learnt from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that I could read more about to develop my understanding?

Community comments

See what others have said about this solution
over 3 years ago
slowthinker says

You can find many articles on this topic. Once you get used to it, point-free style is usually easier to read (see examples in the links). So while not mandatory, you can use these exercises to get the hang of it.

Ask yourself: why should I remember what s stands for in order to understand areaCode? I find that areaCode = take 3 . normalizeInternational describes perfectly what is happening without variable names getting in the way ;-)

over 3 years ago
slowthinker says

You could eta reduce number and areaCode.
Why are you using and [expr1, expr2] instead of expr1 && expr2 in normalizeInternational?

over 3 years ago
mambocab says

Oh, you mean that I could write them in a points-free style?

If you have a couple minutes, could you explain the benefits, as you see them, of writing in that style? I can see its elegance, but not its utility.

over 3 years ago
mambocab says

You could eta reduce number and areaCode.

Sorry, what do you mean?

And of course, &&! I don't know how I managed to forget about that.