packages feed

MorseCode 0.0.1 → 0.0.2

raw patch · 2 files changed

+43/−18 lines, 2 filesPVP: major bump suggested

API removals or changes: PVP suggests a major version bump

API changes (from Hackage documentation)

- Text.Morse: morseCode :: Map Char String
+ Text.Morse: fromMorseCode :: String -> String
+ Text.Morse: isMorseChar :: Char -> Bool

Files

MorseCode.cabal view
@@ -1,5 +1,5 @@ name:			MorseCode-version:		0.0.1+version:		0.0.2 Cabal-Version:	>= 1.6 license:		GPL-3 license-file:	LICENSE
Text/Morse.hs view
@@ -17,8 +17,9 @@ -- along with this program.  If not, see <http://www.gnu.org/licenses/>.  module Text.Morse (-    morseCode,                                          -    toMorseCode+    toMorseCode,+    fromMorseCode,+    isMorseChar,     ) where  import Data.Char@@ -26,7 +27,7 @@  import qualified Data.Map as M --- | Morse code convert map.+-- | Morse Code convert map. morseCode :: Map Char String morseCode =   M.fromList@@ -76,26 +77,50 @@   ,('7', "--...")   ,('8', "---..")   ,('9', "----.")-  ,('Ä', ".-.-")-  ,('Æ', ".-.-")-  ,('Á', ".--.-")-  ,('Å', ".--.-")-  ,('ß', ".../...")-  ,('É', "..-..")-  ,('Ñ', "--.--")-  ,('Ö', "---.")-  ,('Ø', "---.")-  ,('Ü', "..--")   ,('@', ".--.-.")] --- | Convert String to morse code.--- If character not in 'morseCode', don't convert. +-- | Convert String to Morse Code.+-- And ignore invliad Morse character. toMorseCode :: String -> String toMorseCode =   concatMap (\x ->                   case findMinMatch morseCode (\ k _ -> k == toLower x) of-                   Just (_, morse) -> morse-                   Nothing -> [x])+                   -- Add blank after Morse Code.+                   Just (_, morse) -> morse ++ " "+                   Nothing -> +                       -- Replace blank with '/'+                       -- or ignore invalid character.+                       if x == ' ' then "/" else "")++-- | Get string from Morse Code.+-- Throw error if find invalid Morse Code.+fromMorseCode :: String -> String+fromMorseCode str = fromMorseCode' str ""++fromMorseCode' :: String -> String -> String+fromMorseCode' [] _ = []+fromMorseCode' (x:xs) str +    | x == '.'+        = fromMorseCode' xs (str ++ [x])+    | x == '-'+        = fromMorseCode' 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 ""+    | x == '/' +        = ' ' : fromMorseCode' xs ""+    | otherwise +        = error ("'" ++ [x] ++ "' is not valid Morse Code.")++-- | Whether character is valid Morse Character.+isMorseChar :: Char -> Bool+isMorseChar char = +  case findMinMatch morseCode (\ k _ -> k == char) of+    Just _  -> True+    Nothing -> False  -- | Find min match one. findMinMatch :: Ord k => Map k a -> (k -> a -> Bool) -> Maybe (k, a)