module Text.Morse (
encodeMorse,
decodeMorse,
canEncodeToMorse,
isMorseChar
) where
import Data.Char
import Data.Map (Map)
import qualified Data.Map as M
morseCode :: Map Char String
morseCode =
M.fromList
[('a', ".-")
,('b', "-...")
,('c', "-.-.")
,('d', "-..")
,('e', ".")
,('f', "..-.")
,('g', "--.")
,('h', "....")
,('i', "..")
,('j', ".---")
,('k', "-.-")
,('l', ".-..")
,('m', "--")
,('n', "-.")
,('o', "---")
,('p', ".--.")
,('q', "--.-")
,('r', ".-.")
,('s', "...")
,('t', "-")
,('u', "..-")
,('v', "...-")
,('w', ".--")
,('x', "-..-")
,('y', "-.--")
,('z', "--..")
,('=', "-...-")
,('?', "..--..")
,('/', "-..-.")
,(',', "--..--")
,('.', ".-.-.-")
,(':', "---...")
,('\'', ".----.")
,('-', "-....-")
,('(', "-.--.")
,(')', "-.--.-")
,('0', "-----")
,('1', ".----")
,('2', "..---")
,('3', "...--")
,('4', "....-")
,('5', ".....")
,('6', "-....")
,('7', "--...")
,('8', "---..")
,('9', "----.")
,('@', ".--.-.")]
encodeMorse :: String -> String
encodeMorse =
concatMap (\x ->
case findMinMatch morseCode (\ k _ -> k == toLower x) of
Just (_, morse) -> morse ++ " "
Nothing ->
if isSpace x then "/" else "")
decodeMorse :: String -> String
decodeMorse str = decodeMorse' str ""
decodeMorse' :: String -> String -> String
decodeMorse' [] _ = []
decodeMorse' (x:xs) str
| x == '.'
= decodeMorse' xs (str ++ [x])
| x == '-'
= decodeMorse' xs (str ++ [x])
| x == ' '
= let char =
case findMinMatch morseCode (\ _ v -> v == str) of
Just (c, _) -> c
Nothing -> x
in char : decodeMorse' xs ""
| x == '/'
= ' ' : decodeMorse' xs ""
| otherwise
= decodeMorse' xs ""
canEncodeToMorse :: Char -> Bool
canEncodeToMorse char =
case findMinMatch morseCode (\ k _ -> k == char) of
Just _ -> True
Nothing -> False
isMorseChar :: Char -> Bool
isMorseChar char = char `elem` ".-/ "
findMinMatch :: Ord k => Map k a -> (k -> a -> Bool) -> Maybe (k, a)
findMinMatch map fun = match
where filterMap = M.filterWithKey fun map
match = if M.null filterMap
then Nothing
else Just $ M.findMin filterMap