diff options
author | Julian T <julian@jtle.dk> | 2021-11-25 08:47:19 +0100 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2021-11-25 08:47:19 +0100 |
commit | 890ad2bcee172ab2a4cbb319145f5b42ba38619a (patch) | |
tree | c7cc445410379cb5ee4b37b18f2d45a56e680b28 /sem7/pp/larger.hs | |
parent | 7f57150038a90f634dd27b25bd9bba05c461c22a (diff) |
Add notes and assignment solution
Diffstat (limited to 'sem7/pp/larger.hs')
-rw-r--r-- | sem7/pp/larger.hs | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/sem7/pp/larger.hs b/sem7/pp/larger.hs new file mode 100644 index 0000000..5618faf --- /dev/null +++ b/sem7/pp/larger.hs @@ -0,0 +1,29 @@ +import Data.Maybe + +data LTree = LLeaf String | LNode LTree LTree +data LTreeRoot = LTreeRoot LTree Float + +treemerge :: LTreeRoot -> LTreeRoot -> LTreeRoot +treemerge (LTreeRoot ta la) (LTreeRoot tb lb) = LTreeRoot (LNode ta tb) (la + lb) + +data Bit = Zero | One + +instance Show Bit where + show Zero = "0" + show One = "1" + +createBitList :: String -> Maybe [Bit] +createBitList [] = Just [] +createBitList (c:rest) | c == '0' = createBitList rest >>= Just . (:) Zero + | c == '1' = createBitList rest >>= Just . (:) One + | otherwise = Nothing + +maketable :: LTree -> [([Bit], String)] +maketable = recur [] + where recur prefix (LLeaf val) = [(reverse prefix, val)] + recur prefix (LNode left right) = + recur (Zero : prefix) left ++ + recur (One : prefix) right + + +testTree = LNode (LLeaf "Hej") (LNode (LLeaf "Med") (LLeaf "Dig")) |