Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Data.List
- import Data.Function
- -- predefine
- not' :: Bool -> Bool
- not' True = False
- not' False = True
- and',or',nor',nand',xor',impl',equ' :: Bool -> Bool -> Bool
- and' True True = True
- and' _ _ = False
- or' False False = False
- or' _ _ = True
- nand' a b = not' (and' a b)
- nor' a b = not' (or' a b)
- xor' a b
- | a==b =False
- | a/=b =True
- equ' a b = not' (xor' a b)
- ----- not understand
- impl' a b = or' (not' a) b
- -- problem 46
- table :: (Bool -> Bool ->t) -> [(Bool, Bool, t)]
- table f = [(a, b, f a b)| a <- [True, False], b <- [True, False]]
- -- problem 47
- ------ the same w/ above
- -- problem 48
- ------ not understand
- ------ tablen f = [(a, b, c, f [a, b, c])| a <- [True, False], b <- [True, False], c <- [True, False]]
- -- problem 49
- gray :: Num a => a -> [[Char]]
- gray 0 = [[]]
- gray n = (map ("0"++) $ gray (n-1)) ++ (map ("1"++) $ reverse $ gray (n-1))
- -- problem 50
- ------ type and data structure for I/O and huffman trees
- type FList = [(Char, Int)]
- type CList = [(Char, String)]
- data HTree = Leaf {symbol :: Char, wt :: Int} | Branch {wt :: Int, l :: HTree, r :: HTree} deriving (Eq)
- instance Ord HTree
- where compare = compare `on` wt
- ------ functions
- htmerge :: HTree -> HTree -> HTree
- htmerge x y = Branch (wt x + wt y) x y
- flist2ht :: FList -> HTree
- flist2ht = buildht . sort . map (uncurry Leaf)
- where
- buildht :: [HTree] -> HTree
- buildht [] = error "Empty Tree"
- buildht (t:[]) = t
- buildht (x:y:ts) = buildht $ insert (htmerge x y) ts
- ht2clist :: HTree -> CList
- ht2clist = htextract ""
- where
- htextract :: String -> HTree -> CList
- htextract s (Leaf sy _) = [(sy,s)]
- htextract s (Branch _ lt rt) = htextract (s ++ "0") lt ++ htextract (s ++ "1") rt
- huffman = sortBy (compare `on` fst) . ht2clist . flist2ht
Advertisement
Add Comment
Please, Sign In to add comment