/haskell/phone-number/phone.hs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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

Comments

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

slowthinker commented 24 January 2015 at 12:14 UTC

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.

mambocab commented 25 January 2015 at 05:45 UTC

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.

mambocab commented 25 January 2015 at 05:52 UTC

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 ;-)

slowthinker commented 25 January 2015 at 07:33 UTC

You're not logged in right now. Please login via GitHub to comment