Last active
September 1, 2020 14:25
-
-
Save kutyel/328fa9bed5ebb1eb3bf940df51b5e2c0 to your computer and use it in GitHub Desktop.
Quick and dirty morse decoder in Haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Codewars.Kata.DecodeMorse (decodeMorse) where | |
import Codewars.Kata.DecodeMorse.Preload (morseCodes) | |
import Data.List.Split (splitOn) | |
import Data.Map.Strict ((!)) | |
decodeMorse :: String -> String | |
decodeMorse = unwords . filter (not . null) . map ((>>= (morseCodes!)) . words) . splitOn " " |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Data.List.Split (splitOn) | |
import qualified Data.Map.Strict as M | |
test :: String | |
test = ".... . -.-- .--- ..- -.. ." | |
morseCodes :: M.Map String String -- This is given to us by the exercise | |
morseCodes = M.fromList [("....", "H"), (".", "E"), ("-.--", "Y"), (".---", "J"), ("..-", "U"), ("-..", "D")] | |
decodeMorse :: String -> String | |
decodeMorse = maybe "" unwords . traverse (traverse (`M.lookup` (head <$> morseCodes))) . fmap words . splitOn " " |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
module Codewars.Kata.DecodeMorse (decodeMorse) where | |
import Codewars.Kata.DecodeMorse.Preload (morseCodes) | |
import Data.Map.Strict ((!)) | |
import Data.Text hiding (concatMap, unwords) | |
import Prelude hiding (words) | |
decodeMorse :: String -> String | |
decodeMorse = unwords . fmap (concatMap (morseCodes !) . fmap unpack . words) . splitOn " " . strip . pack |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment