Skip to content

Instantly share code, notes, and snippets.

@kutyel
Last active Sep 1, 2020
Embed
What would you like to do?
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