Skip to content

Instantly share code, notes, and snippets.

@relrod
Created March 19, 2014 07:56
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 relrod/9637272 to your computer and use it in GitHub Desktop.
Save relrod/9637272 to your computer and use it in GitHub Desktop.
-----------------------------------------------------------------------------
-- |
-- Module : MorseTree
-- Copyright : (c) 2014 Ricky Elrod
-- License : BSD-style
-- Maintainer : Ricky Elrod <ricky@elrod.me>
-- Stability : experimental
-- Portability : Portable
--
-- TODO: Probably should use Data.Tree instead, for better library support.
-- TODO: toMorseChar is partial right now :(
--
-- Represents morse code as a tree structure
-- Idea taken from: http://www.learnmorsecode.com/pix/learn.gif
--
-- >>> map (flip toMorseChar morseTree) [[Dah, Dit, Dit, Dit], [Dit, Dit]
-- ['H', 'I']
-----------------------------------------------------------------------------
data Tree a = Empty | Branch a (Tree a) (Tree a) deriving (Show)
data Elem a = Start | MorseChar a
data Morse = Dit | Dah deriving (Show)
instance Show a => Show (Elem a) where
show Start = "<start>"
show (MorseChar x) = show x
morseTree :: Tree (Elem Char)
morseTree =
Branch Start
(Branch (MorseChar 'T')
(Branch (MorseChar 'M')
(Branch (MorseChar 'O')
(Branch (MorseChar '-')
(Branch (MorseChar '0') Empty Empty)
(Branch (MorseChar '9') Empty Empty)
)
(Branch (MorseChar '.')
Empty
(Branch (MorseChar '8') Empty Empty)
)
)
(Branch (MorseChar 'G')
(Branch (MorseChar 'Q') Empty Empty)
(Branch (MorseChar 'Z')
Empty
(Branch (MorseChar '7') Empty Empty)
)
)
)
(Branch (MorseChar 'N')
(Branch (MorseChar 'K')
(Branch (MorseChar 'Y') Empty Empty)
(Branch (MorseChar 'C') Empty Empty)
)
(Branch (MorseChar 'D')
(Branch (MorseChar 'X') Empty Empty)
(Branch (MorseChar 'B')
Empty
(Branch (MorseChar '6') Empty Empty)
)
)
)
)
(Branch (MorseChar 'E')
(Branch (MorseChar 'A')
(Branch (MorseChar 'W')
(Branch (MorseChar 'J')
(Branch (MorseChar '1') Empty Empty)
Empty
)
(Branch (MorseChar 'P') Empty Empty)
)
(Branch (MorseChar 'R')
Empty
(Branch (MorseChar 'L') Empty Empty)
)
)
(Branch (MorseChar 'I')
(Branch (MorseChar 'U')
(Branch (MorseChar '_')
(Branch (MorseChar '2') Empty Empty)
Empty
)
(Branch (MorseChar 'F') Empty Empty)
)
(Branch (MorseChar 'S')
(Branch (MorseChar 'V')
(Branch (MorseChar '3') Empty Empty)
Empty
)
(Branch (MorseChar 'H')
(Branch (MorseChar '4') Empty Empty)
(Branch (MorseChar '5') Empty Empty)
)
)
)
)
toMorseChar :: [Morse] -> Tree (Elem Char) -> (Elem Char)
toMorseChar (Dit:m) (Branch _ _ r) = toMorseChar m r
toMorseChar (Dah:m) (Branch _ l _) = toMorseChar m l
toMorseChar [] (Branch x _ _) = x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment