Key phone in Haskell Here is my solution for an exercise, which requires translation of strings to key presses on a phone keypad (for example, to get 'b' the digit '2' needs to be pressed twice).

It's taken from the book Haskell from first principles, and originally appeared in 1HaskellADay. I'd love to get feedback.

module Phone where
import Data.Char
import Data.String.Utils
import Data.List

data DaPhone = DaPhone [String]
convo :: [String]
convo =
          ["Wanna play 20 questions",
           "Ya",
           "U 1st haha",
           "Lol ok. Have u ever tasted alcohol lol",
           "Lol ya",
           "Wow ur cool haha. Ur turn",
           "Ok. Do u think I am pretty Lol",
           "Lol ya",
           "Haha thanks just making sure rofl ur turn"]

-- validButtons = "1234567890*#"
type Digit = Char
-- Valid presses: 1 and up
type Presses = Int

reverseTaps :: DaPhone -&gt; Char -&gt; [(Digit, Presses)]
reverseTaps (DaPhone keys) c = if isUpper c then ('*', 1) : looks else looks
    where looks = look keys (toLower c)
-- assuming the default phone definition
-- 'a' -&gt; [('2', 1)]
-- 'A' -&gt; [('*', 1), ('2', 1)]

look :: [String] -&gt; Char -&gt; [(Digit, Presses)]
look keys c = look' keys c 0

look' :: [String] -&gt; Char -&gt; Int -&gt; [(Digit, Presses)]
look' [] _ _ = []
look' (x:xs) c n = if ind /= (-1) then [(head $ show n, ind + 1)] else look' xs c (n + 1)
where ind = maybe (-1) id $ elemIndex c x

cellPhonesDead :: DaPhone -&gt; String -&gt; [(Digit, Presses)]
cellPhonesDead = (concat .) . map . reverseTaps

-- count total presses
fingerTaps :: [(Digit, Presses)] -&gt; Presses
fingerTaps = sum . map snd

mostPopularLetter :: String -&gt; Char
mostPopularLetter = head . longest . group . sort

coolestLtr :: [String] -&gt; Char
coolestLtr = mostPopularLetter . filter isAlpha . concat

coolestWord :: [String] -&gt; String
coolestWord = head . longest . group . sort . words . join " "

longest :: [[a]] -&gt; [a]
longest = maximumBy (\x y -&gt; compare (length x) (length y))

phone :: DaPhone
phone = DaPhone keymap

keymap :: [String]
keymap =

                [
                    " 0",
                    "1",
                    "abc2",
                    "def3",
                    "ghi4",
                    "jkl5",
                    "mno6",
                    "pqrs7",
                    "tuv8",
                    "wxyz9"
                ]
In real code, I would consider this to be too much naming for this little code. Most of this would probably be used only once and could thus be inlined. Since the exercise required implementations for all these, it's okay.

(concat .) . map is foldMap.

\x y -&gt; compare (length x) (length y) is comparing length.

Did you try compiling this? ind /= Nothing means that ind + 1 shouldn't work, and elemIndex takes the Char before the String.

look keys c = case asum $ zipWith (fmap . (,)) [0..] $ map (elemIndex c) keys of
  Nothing -&gt; []
  Just (n, ind) -&gt; [(head $ show n, ind + 1)]