Haskell and Morse code

haskellMorse

http://apfelmus.nfshost.com/articles/fun-with-morse-code.html

First, we use an association list.

text = "-.-. --.- @ -.. . @ .--- .... .---- --- --- -.. @ -.-"
dict :: [(String, Char)]
dict =
   [(".-"   ,'a'),
    ("-..." ,'b'),
    ("-.-." ,'c'),
    ("-.."  ,'d'),
    ("."    ,'e'),
    ("..-." ,'f'),
    ("--."  ,'g'),
    ("...." ,'h'),
    (".."   ,'i'),
    (".---" ,'j'),
    ("-.-"  ,'k'),
    (".-.." ,'l'),
    ("--"   ,'m'),
    ("-."   ,'n'),
    ("---"  ,'o'),
    (".--." ,'p'),
    ("--.-" ,'q'),
    (".-."  ,'r'),
    ("..."  ,'s'),
    ("-"    ,'t'),
    ("..-"  ,'u'),
    ("...-" ,'v'),
    (".--"  ,'w'),
    ("-..-" ,'x'),
    ("-.--" ,'y'),
    ("--.." ,'z'),
    ("-----",'0'),
    (".----",'1'),
    ("..---",'2'),
    ("...--",'3'),
    ("....-",'4'),
    (".....",'5'),
    ("-....",'6'),
    ("--...",'7'),
    ("---..",'8'),
    ("----.",'9'),
    ("@"    ,' ')] 

decodeLetter :: String -> Char
decodeLetter code = maybe ' ' id $ lookup code dict
decode = map decodeLetter . words

main = do
    print $ decode text

The function words breaks a string up into a list of words, which were delimited by
white space.

-- https://www.haskell.org/onlinereport/standard-prelude.html
words            :: String -> [String]
words s          =  case dropWhile Char.isSpace s of
                      "" -> []
                      s' -> w : words s''
                            where (w, s'') = break Char.isSpace s'

Using a string “@” for a word space is not elegant, and there should be a better way.

The output will be:

"cq de jh1ood k"