Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.11 KB | None | 0 0
  1. module Main where
  2.  
  3. import Lib
  4. import Data.Char
  5. import Prelude
  6. import Text.Printf
  7.  
  8. main :: IO ()
  9. main = someFunc
  10.  
  11. qsort [] = []
  12. qsort (x:xs) = qsort smaller ++ [x] ++ qsort larger
  13. where
  14. smaller = [a | a <- xs, a <= x]
  15. larger = [b | b <- xs, b > x]
  16.  
  17. --qsort [3,5,1,4,2]
  18. seqn :: [IO a] -> IO [a]
  19. seqn [] = return []
  20. seqn (act:acts) = do x <- act
  21. xs <- seqn acts
  22. return (x:xs)
  23.  
  24. factorial n = product [1..n]
  25.  
  26. average ns = sum ns `div` length ns
  27.  
  28. add x y = x + y
  29.  
  30. -- Pattern matching
  31.  
  32. --(&&) :: Bool -> Bool -> Bool
  33. --True && True = True
  34. --_ && _ = False
  35.  
  36.  
  37. -- lambda expressions
  38. a = \ x -> x + x
  39.  
  40. addL :: Int -> (Int -> Int)
  41. addL = \ x -> \ y -> x + y
  42.  
  43. add2 = addL 2
  44.  
  45. -- function declaration is right associative
  46. -- function application is left associative
  47.  
  48. -- luhn algorithm
  49. luhnDouble :: Int -> Int
  50. luhnDouble x = y - (if y > 9 then 9 else 0) where y = x * 2
  51.  
  52. --luhn :: Int -> Int -> Int -> Int -> Bool
  53. --luhn a b c d = sum (map luhnDouble [a,c]) `mod` 10 == 0
  54.  
  55. -- guards
  56. --find :: Eq a => a -> [(a,b)] -> [b]
  57. --find k t = [v | (k', v) <- t, k == k']
  58.  
  59. -- zip
  60. pairs :: [a] -> [(a,a)]
  61. pairs xs = zip xs (tail xs)
  62.  
  63. sorted :: Ord a => [a] -> Bool
  64. sorted xs = and [x <= y | (x,y) <- pairs xs]
  65.  
  66. positions :: Eq a => a -> [a] -> [Int]
  67. positions x xs = [i | (x', i) <- zip xs [0..], x == x']
  68.  
  69. -- Caesar Cipher
  70. let2int :: Char -> Int
  71. let2int c = ord c - ord 'a'
  72.  
  73. int2let :: Int -> Char
  74. int2let n = chr (ord 'a' + n)
  75.  
  76. shift :: Int -> Char -> Char
  77. shift n c | isLower c = int2let ((let2int c + n) `mod` 26)
  78. | otherwise = c
  79.  
  80. --encode :: Int -> String -> String
  81. --encode n xs = [shift n x | x <- xs]
  82.  
  83. percent :: Int -> Int -> Float
  84. percent n m = (fromIntegral n / fromIntegral m) * 100
  85.  
  86. count :: Eq a => a -> [a] -> Int
  87. count x xs = sum [1 | x' <- xs, x == x']
  88.  
  89. lowers :: String -> Int
  90. lowers xs = length [x | x <- xs, isAsciiLower x]
  91.  
  92. freqs :: String -> [Float]
  93. freqs xs = [percent (count x xs) n | x <- ['a'..'z']]
  94. where
  95. n = lowers xs
  96.  
  97.  
  98. -------------
  99. -- Recursion
  100. -- Some of the implementations are commented because they are
  101. -- overriding the existing definitions in scope
  102. -------------
  103. fac :: Int -> Int
  104. fac 0 = 1
  105. fac n = n * fac(n-1)
  106.  
  107. --product :: Num a => [a] -> a
  108. --product [] = 1
  109. --product (n:ns) = n * product ns
  110.  
  111. --length :: [a] -> Int
  112. --length [] = 0
  113. --length (_:xs) = 1 + length xs
  114.  
  115. --reverse :: [a] -> [a]
  116. --reverse [] = []
  117. --reverse (x:xs) = reverse(xs) ++ [x]
  118.  
  119. -- commented because above qsort code is using system ++
  120. --(++) :: [a] -> [a] -> [a]
  121. --[] ++ ys = ys
  122. --(x:xs) ++ ys = x : (xs :: ys)
  123.  
  124. insert :: Ord a => a -> [a] -> [a]
  125. insert x [] = [x]
  126. insert x (y:ys) | x <= y = x : y : ys
  127. | otherwise = y : insert x ys
  128.  
  129. isort :: Ord a => [a] -> [a]
  130. isort [] = []
  131. isort (x:xs) = insert x (isort xs)
  132.  
  133. -- multiple arguments
  134. --zip :: [a] -> [b] -> [(a,b)]
  135. --zip [] _ = []
  136. --zip _ [] = []
  137. --zip (x:xs) (y:ys) = (x,y) : zip xs ys
  138.  
  139. --drop :: Int -> [a] -> [a]
  140. --drop 0 xs = xs
  141. --drop _ [] = []
  142. --drop n (_:xs) = drop (n-1) xs
  143.  
  144. fib :: Int -> Int
  145. fib 0 = 0
  146. fib 1 = 1
  147. fib n = fib (n-2) + fib(n-1)
  148.  
  149. -- mutual recursion
  150. --even :: Int -> Bool
  151. --even 0 = True
  152. --even n = odd (n-1)
  153. --
  154. --odd :: Int -> Bool
  155. --odd = False
  156. --odd n = even (n + 1)
  157.  
  158. evens :: [a] -> [a]
  159. evens [] = []
  160. evens (x:xs) = x : odds xs
  161.  
  162. odds :: [a] -> [a]
  163. odds [] = []
  164. odds (x:xs) = evens xs
  165.  
  166.  
  167. -------------------------
  168. -- Higher order functions
  169. -------------------------
  170. twice :: (a -> a) -> a -> a
  171. twice f x = f (f x)
  172.  
  173. --map :: (a -> b) -> [a] -> b
  174. --map f xs = [f x | x <- xs]
  175.  
  176. --filter :: (a -> Bool) -> [a] -> [a]
  177. --filter p xs = [x | x <- xs, p x]
  178. -- filter using recursion
  179. --filter p [] = []
  180. --filter p (x:xs) | p x = x : filter p xs
  181. -- | otherwise filter p xs
  182.  
  183. --foldr :: (a -> b -> b) -> b -> [a] -> b
  184. --foldr f v [] = v
  185. --foldr f v (x:xs) = f x (foldr f v xs)
  186.  
  187. --foldl :: (a -> b -> a) -> a -> [b] -> a
  188. --foldl f v [] = v
  189. --foldl f v (x:xs) = foldl f (f v x) xs
  190.  
  191. --sum :: Num a => [a] -> a
  192. --sum = sum' 0
  193. -- where
  194. -- sum' v [] = v
  195. -- sum' v (x:xs) = sum' (v+x) xs
  196.  
  197. --(.) :: (b -> c) -> (a -> b) -> (a -> c)
  198. --f . g = \x -> f (g x)
  199.  
  200. type Bit = Int
  201. bin2int :: [Bit] -> Int
  202. bin2int bits = sum [w*b | (w,b) <- zip weights bits]
  203. where weights = iterate (*2) 1
  204.  
  205. int2bin :: Int -> [Bit]
  206. int2bin 0 = []
  207. int2bin n = n `mod` 2 : int2bin(n `div` 2)
  208.  
  209. make8 :: [Bit] -> [Bit]
  210. make8 bits = take 8 (bits ++ repeat 0)
  211.  
  212. encode :: String -> [Bit]
  213. encode = concatMap (make8 . int2bin . ord)
  214.  
  215. chop8 :: [Bit] -> [[Bit]]
  216. chop8 [] = []
  217. chop8 bits = take 8 bits : chop8 (drop 8 bits)
  218.  
  219. decode :: [Bit] -> String
  220. decode = map (chr . bin2int) . chop8
  221.  
  222. ------------------------------
  223. -- Declaring types and classes
  224. ------------------------------
  225.  
  226. type Pair a = (a,a)
  227. type Assoc k v = [(k,v)]
  228.  
  229. find :: Eq k => k -> Assoc k v -> v
  230. find k t = head [v | (k', v) <- t, k == k']
  231.  
  232. data Move = North | South | East | West
  233.  
  234.  
  235. type Pos = (Int, Int)
  236.  
  237. move :: Move -> Pos -> Pos
  238. move North (x,y) = (x, y+1)
  239. move South (x,y) = (x, y-1)
  240. move East (x,y) = (x+1, y)
  241. move West (x,y) = (x-1, y)
  242.  
  243. data Shape = Circle Float | Rect Float Float
  244. area :: Shape -> Float
  245. area (Circle r) = pi * r^2
  246. area (Rect l b) = l * b
  247.  
  248. safediv :: Int -> Int -> Maybe Int
  249. safediv _ 0 = Nothing
  250. safediv m n = Just (m `div` n)
  251.  
  252. -- Natural Numbers
  253.  
  254. data Nat = Zero | Succ Nat
  255.  
  256. instance Show Nat where
  257. show Zero = "Zero"
  258. show (Succ m) = printf "Succ (%s)" (show m)
  259.  
  260. nat2int :: Nat -> Int
  261. nat2int Zero = 0
  262. nat2int (Succ n) = 1 + nat2int n
  263.  
  264. int2nat :: Int -> Nat
  265. int2nat 0 = Zero
  266. int2nat n = Succ (int2nat (n-1))
  267.  
  268. add' :: Nat -> Nat -> Nat
  269. add' Zero n = n
  270. add' (Succ m) n = Succ (add' m n)
  271.  
  272. addNat :: Nat -> Nat -> Nat
  273. addNat m n = int2nat (nat2int m + nat2int n)
  274.  
  275. -- List
  276.  
  277. data List' a = Nil | Cons a (List' a)
  278.  
  279. len :: List' a -> Int
  280. len Nil = 0
  281. len (Cons _ xs) = 1 + len xs
  282.  
  283. -- Tree
  284.  
  285. data Tree a = Leaf a | Node (Tree a) a (Tree a)
  286. t :: Tree Int
  287. t = Node (Node (Leaf 1) 3 (Leaf 4))
  288. 5
  289. (Node (Leaf 6) 7 (Leaf 9))
  290.  
  291. occurs :: Eq a => a -> Tree a -> Bool
  292. occurs x (Leaf y) = x == y
  293. occurs x (Node l y r) = (x == y) || occurs x l || occurs x r
  294.  
  295. flatten :: Tree a -> [a]
  296. flatten (Leaf x) = [x]
  297. flatten (Node l x r) = flatten l ++ [x] ++ flatten r
  298.  
  299. occurs' :: Ord a => a -> Tree a -> Bool
  300. occurs' x (Leaf y) = x == y
  301. occurs' x (Node l y r) | x == y = True
  302. | x < y = occurs x l
  303. | otherwise = occurs x r
  304.  
  305. -- class
  306.  
  307. class Bird a where
  308. eat, walk, fly :: () -> a
  309.  
  310. -- Tautology checker
  311.  
  312. data Prop = Const Bool
  313. | Var Char
  314. | Not Prop
  315. | And Prop Prop
  316. | Imply Prop Prop
  317.  
  318. p1 :: Prop
  319. p1 = And (Var 'A') (Not (Var 'A'))
  320.  
  321. p2 :: Prop
  322. p2 = Imply (And (Var 'A') (Var 'B')) (Var 'A')
  323.  
  324. p3 :: Prop
  325. p3 = Imply (Var 'A') (And (Var 'A') (Var 'B'))
  326.  
  327. p4 :: Prop
  328. p4 = Imply (And (Var 'A') (Imply (Var 'A') (Var 'B'))) (Var 'B')
  329.  
  330. type Subst = Assoc Char Bool
  331.  
  332. eval :: Subst -> Prop -> Bool
  333. eval _ (Const b) = b
  334. eval s (Var x) = find x s
  335. eval s (Not p) = not (eval s p)
  336. eval s (And p q) = eval s p && eval s q
  337. eval s (Imply p q) = eval s p <= eval s q
  338.  
  339. vars :: Prop -> [Char]
  340. vars (Const _) = []
  341. vars (Var x) = [x]
  342. vars (Not p) = vars p
  343. vars (And p q) = vars p ++ vars q
  344. vars (Imply p q) = vars p ++ vars q
  345.  
  346. bools :: Int -> [[Bool]]
  347. bools n = map (reverse . map conv . make n . int2bin) range
  348. where
  349. range = [0..(2^n)-1]
  350. make n bs = take n (bs ++ repeat 0)
  351. conv 0 = False
  352. conv 1 = True
  353.  
  354. rmdups :: Eq a => [a] -> [a]
  355. rmdups [] = []
  356. rmdups (x:xs) = x : filter (/= x) (rmdups xs)
  357.  
  358. substs :: Prop -> [Subst]
  359. substs p = map (zip vs) (bools (length vs))
  360. where vs = rmdups (vars p)
  361.  
  362. isTaut :: Prop -> Bool
  363. isTaut p = and [eval s p | s <- substs p]
  364.  
  365. -- Abstract Machine
  366.  
  367. data Expr = Val Int | Add Expr Expr
  368.  
  369. value :: Expr -> Int
  370. value (Val n) = n
  371. value (Add x y) = value x + value y
  372.  
  373. type Cont = [Op]
  374.  
  375. data Op = EVAL Expr | ADD Int
  376.  
  377. eval' :: Expr -> Cont -> Int
  378. eval' (Val n) c = exec c n
  379. eval' (Add x y) c = eval' x (EVAL y : c)
  380.  
  381. exec :: Cont -> Int -> Int
  382. exec [] n = n
  383. exec (EVAL y : c) n = eval' y (ADD n : c)
  384. exec (ADD n : c) m = exec c (n+m)
  385.  
  386. value :: Expr -> Int
  387. value e = eval e []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement