Skip to content

Instantly share code, notes, and snippets.

@kutyel
Last active September 1, 2020 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kutyel/328fa9bed5ebb1eb3bf940df51b5e2c0 to your computer and use it in GitHub Desktop.
Save kutyel/328fa9bed5ebb1eb3bf940df51b5e2c0 to your computer and use it in GitHub Desktop.
Quick and dirty morse decoder in Haskell
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 " "
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 " "
{-# 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