MorseCode 0.0.2 → 0.0.3
raw patch · 2 files changed
+32/−25 lines, 2 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Text.Morse: fromMorseCode :: String -> String
- Text.Morse: toMorseCode :: String -> String
+ Text.Morse: canEncodeToMorse :: Char -> Bool
+ Text.Morse: decodeMorse :: String -> String
+ Text.Morse: encodeMorse :: String -> String
Files
- MorseCode.cabal +1/−1
- Text/Morse.hs +31/−24
MorseCode.cabal view
@@ -1,5 +1,5 @@ name: MorseCode-version: 0.0.2+version: 0.0.3 Cabal-Version: >= 1.6 license: GPL-3 license-file: LICENSE
Text/Morse.hs view
@@ -17,9 +17,10 @@ -- along with this program. If not, see <http://www.gnu.org/licenses/>. module Text.Morse (- toMorseCode,- fromMorseCode,- isMorseChar,+ encodeMorse,+ decodeMorse,+ canEncodeToMorse,+ isMorseChar ) where import Data.Char@@ -80,47 +81,53 @@ ,('@', ".--.-.")] -- | Convert String to Morse Code.--- And ignore invliad Morse character.-toMorseCode :: String -> String-toMorseCode =+-- And ignore invalid Morse character.+encodeMorse :: String -> String+encodeMorse = concatMap (\x -> case findMinMatch morseCode (\ k _ -> k == toLower x) of -- Add blank after Morse Code. Just (_, morse) -> morse ++ " " Nothing -> - -- Replace blank with '/'+ -- Replace space with '/' -- or ignore invalid character.- if x == ' ' then "/" else "")+ if isSpace x then "/" else "") -- | Get string from Morse Code.--- Throw error if find invalid Morse Code.-fromMorseCode :: String -> String-fromMorseCode str = fromMorseCode' str ""+-- Invalid Morse Code will skip.+decodeMorse :: String -> String+decodeMorse str = decodeMorse' str "" -fromMorseCode' :: String -> String -> String-fromMorseCode' [] _ = []-fromMorseCode' (x:xs) str +decodeMorse' :: String -> String -> String+decodeMorse' [] _ = []+decodeMorse' (x:xs) str | x == '.'- = fromMorseCode' xs (str ++ [x])+ = decodeMorse' xs (str ++ [x]) | x == '-'- = fromMorseCode' xs (str ++ [x])+ = decodeMorse' xs (str ++ [x]) | x == ' ' = let char = case findMinMatch morseCode (\ _ v -> v == str) of Just (c, _) -> c- Nothing -> error (str ++ " is not valid Morse Code.")- in char : fromMorseCode' xs ""+ -- Insert blank when got invalid Morse code.+ Nothing -> x + in char : decodeMorse' xs "" | x == '/' - = ' ' : fromMorseCode' xs ""- | otherwise - = error ("'" ++ [x] ++ "' is not valid Morse Code.")+ = ' ' : decodeMorse' xs ""+ -- Ignore invalid Morse character.+ | otherwise+ = decodeMorse' xs "" --- | Whether character is valid Morse Character.-isMorseChar :: Char -> Bool-isMorseChar char = +-- | Character can encode to Morse Code?+canEncodeToMorse :: Char -> Bool+canEncodeToMorse char = case findMinMatch morseCode (\ k _ -> k == char) of Just _ -> True Nothing -> False++-- | Is valid Morse character.?+isMorseChar :: Char -> Bool+isMorseChar char = char `elem` ".-/ " -- | Find min match one. findMinMatch :: Ord k => Map k a -> (k -> a -> Bool) -> Maybe (k, a)