summaryrefslogtreecommitdiff
path: root/46-50
diff options
context:
space:
mode:
Diffstat (limited to '46-50')
-rw-r--r--46-50/46.hs25
-rw-r--r--46-50/47.hs31
-rw-r--r--46-50/48.hs33
-rw-r--r--46-50/49.hs4
-rw-r--r--46-50/50.hs12
5 files changed, 105 insertions, 0 deletions
diff --git a/46-50/46.hs b/46-50/46.hs
new file mode 100644
index 0000000..30602e0
--- /dev/null
+++ b/46-50/46.hs
@@ -0,0 +1,25 @@
+and', or', nand', nor', xor', impl', equ' :: Bool -> Bool -> Bool
+not' :: Bool -> Bool
+
+not' True = False
+not' False = True
+
+and' True True = True
+and' _ _ = False
+
+or' False False = False
+or' _ _ = True
+
+xor' True False = True
+xor' False True = True
+xor' _ _ = False
+
+equ' a b = (not' a) `xor'` b
+impl' a b = (not' a) `or'` b
+
+nand' a = not' . and' a
+nor' a = not' . or' a
+
+table :: (Bool -> Bool -> Bool) -> IO ()
+table f = mapM_ putStrLn [show a ++ " " ++ show b ++ " " ++ (show $ f a b)
+ | let bin = [True, False], a <- bin, b <- bin]
diff --git a/46-50/47.hs b/46-50/47.hs
new file mode 100644
index 0000000..f672f4a
--- /dev/null
+++ b/46-50/47.hs
@@ -0,0 +1,31 @@
+and', or', nand', nor', xor', impl', equ' :: Bool -> Bool -> Bool
+not' :: Bool -> Bool
+
+not' True = False
+not' False = True
+
+and' True True = True
+and' _ _ = False
+
+or' False False = False
+or' _ _ = True
+
+xor' True False = True
+xor' False True = True
+xor' _ _ = False
+
+equ' a b = (not' a) `xor'` b
+impl' a b = (not' a) `or'` b
+
+nand' a = not' . and' a
+nor' a = not' . or' a
+
+table2 :: (Bool -> Bool -> Bool) -> IO ()
+table2 f = mapM_ putStrLn [show a ++ " " ++ show b ++ " " ++ (show $ f a b)
+ | let bin = [True, False], a <- bin, b <- bin]
+
+infixl 4 `or'`
+infixl 5 `xor'`
+infixl 6 `and'`
+infixl 7 `equ'`
+-- use not for negation, it has fixity 9 by default
diff --git a/46-50/48.hs b/46-50/48.hs
new file mode 100644
index 0000000..5f86179
--- /dev/null
+++ b/46-50/48.hs
@@ -0,0 +1,33 @@
+import Control.Monad
+
+and', or', nand', nor', xor', impl', equ' :: Bool -> Bool -> Bool
+not' :: Bool -> Bool
+
+not' True = False
+not' False = True
+
+and' True True = True
+and' _ _ = False
+
+or' False False = False
+or' _ _ = True
+
+xor' True False = True
+xor' False True = True
+xor' _ _ = False
+
+equ' a b = (not' a) `xor'` b
+impl' a b = (not' a) `or'` b
+
+nand' a = not' . and' a
+nor' a = not' . or' a
+
+tablen :: Int -> ([Bool] -> Bool) -> IO ()
+tablen n f = mapM_ putStrLn [(unwords $ map show args) ++ " " ++ (show $ f args)
+ | args <- replicateM n [True, False]]
+
+infixl 4 `or'`
+infixl 5 `xor'`
+infixl 6 `and'`
+infixl 7 `equ'`
+--infixl 3 `equ'`
diff --git a/46-50/49.hs b/46-50/49.hs
new file mode 100644
index 0000000..afe25ad
--- /dev/null
+++ b/46-50/49.hs
@@ -0,0 +1,4 @@
+gray :: Int -> [String]
+
+gray 0 = [""]
+gray n = (map ('0':) last) ++ (map ('1':) $ reverse last) where last = gray (n - 1)
diff --git a/46-50/50.hs b/46-50/50.hs
new file mode 100644
index 0000000..0e9c33b
--- /dev/null
+++ b/46-50/50.hs
@@ -0,0 +1,12 @@
+import Data.List (sortBy, insertBy)
+import Data.Ord (comparing)
+
+huffman :: [(Char, Int)] -> [(Char, String)]
+
+huffman l = build $ map (\(a, b) -> ([(a, "")], b)) $ sortBy (comparing snd) l
+ where build [x] = fst x
+ build (x:y:xs) =
+ build $ insertBy
+ (comparing snd)
+ ((add x '0') ++ (add y '1'), snd x + snd y) xs
+ where add e c = (map (\(a, b) -> (a, c:b)) $ fst e)