DEV Community

Discussion on: Daily Challenge #47 - Alphabets

Collapse
 
craigmc08 profile image
Craig McIlwrath
import Data.Maybe (catMaybes)
import Data.List (find)
import Data.Functor (fmap)
import Data.Char (toLower) 

alpha = ['a'..'z']

isAlpha :: Char -> Bool
isAlpha = (`elem` alpha)

(>.<) :: (a -> b -> d) -> (c -> b) -> (a -> c -> d)
a >.< b = flip $ flip a . b

toNumber :: Char -> Maybe Int
toNumber = fmap snd . (flip find) alphaNum . ((==) >.< fst)
  where alphaNum = zip alpha [1..]

encodeMsg :: String -> String
encodeMsg = unwords . map show . catMaybes . map toNumber . filter isAlpha . map toLower

I wanted to try to write this function completely using point-free style. It led to me having to write that >.< operator, which you can see from the type definition exactly what it does. It was a good mental exercise in types for me, a Haskell beginner.

Collapse
 
curtisfenner profile image
Curtis Fenner

You don't need your filter isAlpha and isAlpha functions, since toNumber already returns None when the character isn't a letter, which chops off a nice bit of the solution!

You can also use findIndex from Data.List instead of find-with-zip (though that solution is cool! 😋

toNumber = (fmap (+1)) . (flip findIndex alpha) . (==)