dooooooduo

COMP1100 excercises

Apr 27th, 2016
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Lab 8 Binary Tree
  2. data Binary_Tree a = Null | Node {element :: a, left_tree, right_tree :: Binary_Tree a}
  3.    deriving(Show, Eq)
  4.  
  5. size_of_tree :: Integral b => Binary_Tree a -> b
  6. size_of_tree tree = case tree of
  7.    Null                          -> 0
  8.    (Node _ tree_left tree_right) -> 1 + size_of_tree tree_left + size_of_tree tree_right
  9.    
  10. depth_of_tree :: Integral b => Binary_Tree a -> b
  11. depth_of_tree tree = case tree of
  12.    Null                          -> 0
  13.    (Node _ tree_left tree_right) -> 1 + max (depth_of_tree tree_left) (depth_of_tree tree_right)
  14.  
  15. flatten_tree :: Binary_Tree a -> [a]
  16. flatten_tree tree = case tree of
  17.    Null                                -> []
  18.    (Node element tree_left tree_right) -> flatten_tree tree_left ++ [element] ++ flatten_tree tree_right
  19.    
  20. leaves_of_tree :: Binary_Tree a -> [a]
  21. leaves_of_tree tree = case tree of
  22.    Null                                -> []
  23.    (Node element Null      Null)       -> [element]
  24.    (Node element tree_left tree_right) -> leaves_of_tree tree_left ++ leaves_of_tree tree_right
  25.    
  26.  
  27. -- Lab 8 Binary Tree higher
  28. data Binary_Tree a = Null | Node {element :: a, left_tree, right_tree :: Binary_Tree a}
  29.    deriving(Show, Eq)
  30.  
  31. size_of_tree :: Integral b => Binary_Tree a -> b
  32. size_of_tree tree = case tree of
  33.    Null                          -> 0
  34.    (Node _ tree_left tree_right) -> 1 + size_of_tree tree_left + size_of_tree tree_right
  35.    
  36. depth_of_tree :: Integral b => Binary_Tree a -> b
  37. depth_of_tree tree = case tree of
  38.    Null                          -> 0
  39.    (Node _ tree_left tree_right) -> 1 + max (depth_of_tree tree_left) (depth_of_tree tree_right)
  40.  
  41. flatten_tree :: Binary_Tree a -> [a]
  42. flatten_tree tree = case tree of
  43.    Null                                -> []
  44.    (Node element tree_left tree_right) -> flatten_tree tree_left ++ [element] ++ flatten_tree tree_right
  45.    
  46. leaves_of_tree :: Binary_Tree a -> [a]
  47. leaves_of_tree tree = case tree of
  48.    Null                                -> []
  49.    (Node element Null      Null)       -> [element]
  50.    (Node element tree_left tree_right) -> leaves_of_tree tree_left ++ leaves_of_tree tree_right
  51.  
  52. map_function_over_binary_tree :: (a -> b) -> Binary_Tree a -> Binary_Tree b
  53. map_function_over_binary_tree f tree = case tree of
  54.    Null -> Null
  55.    (Node elem l r) -> Node (f elem) (map_function_over_binary_tree f l) (map_function_over_binary_tree f r)
  56.  
  57. -- Lab 8 Binary Search Tree
  58. import Data.List
  59.  
  60. data Binary_Tree a = Null | Node {element :: a, left_tree, right_tree :: Binary_Tree a}
  61.    deriving(Show, Eq)
  62. type BS_Tree a = Binary_Tree a
  63.  
  64.  
  65. is_valid_binary_search_tree :: Ord a => BS_Tree a -> Bool
  66. is_valid_binary_search_tree tree = flattened_tree == sort  flattened_tree
  67.    where
  68.       flattened_tree = flatten_in_order tree
  69.  
  70. flatten_in_order :: Ord a => BS_Tree a -> [a]
  71. flatten_in_order tree = case tree of
  72.    Null -> []
  73.    (Node e l r) -> flatten_in_order l ++ [e] ++ flatten_in_order r
  74.  
  75. minimum_element :: Ord a => BS_Tree a -> a
  76. minimum_element tree@(Node e _ _) = minimum_element_sub tree e
  77.    where
  78.       minimum_element_sub :: Ord a => BS_Tree a -> a -> a
  79.       minimum_element_sub tree' min_elem = case tree' of
  80.          Null -> min_elem
  81.          (Node e' l _)
  82.            | e' < min_elem   -> minimum_element_sub l e'
  83.            | otherwise       -> minimum_element_sub l min_elem
  84.            
  85. maximum_element :: Ord a => BS_Tree a -> a
  86. maximum_element tree@(Node e _ _) = maximum_element_sub tree e
  87.   where
  88.      maximum_element_sub :: Ord a => BS_Tree a -> a -> a
  89.      maximum_element_sub tree' max_elem = case tree' of
  90.         Null -> max_elem
  91.         (Node e' _ r)
  92.             | e' > max_elem   -> maximum_element_sub r e'
  93.             | otherwise       -> maximum_element_sub r max_elem
  94.  
  95. contains_element :: Ord a => a -> BS_Tree a -> Bool
  96. contains_element elem tree = case tree of
  97.    Null -> False
  98.    (Node e l r) -> case compare elem e of
  99.       EQ -> True
  100.       LT -> contains_element elem l
  101.       GT -> contains_element elem r
  102.  
  103. insert_element_to_tree :: Ord a => a -> BS_Tree a -> BS_Tree a
  104. insert_element_to_tree element tree = case tree of
  105.    Null           -> Node element Null Null
  106.    (Node e l r)   -> case compare element e of
  107.       EQ -> Node e l r
  108.       LT -> Node e (insert_element_to_tree element l) r
  109.       GT -> Node e l (insert_element_to_tree element r)
  110.  
  111.  
  112. -- Lab 8 Rose Tree
  113. data Rose_Tree a = Rose_Node a [Rose_Tree a]
  114.    deriving (Show, Eq)
  115.    
  116. size_of_tree :: Integral b => Rose_Tree a -> b
  117. size_of_tree (Rose_Node _ nodes) = 1 + sum (map size_of_tree nodes)
  118.  
  119. depth_of_tree :: Integral b => Rose_Tree a -> b
  120. depth_of_tree (Rose_Node _ nodes) = 1 + maximum (0 : (map depth_of_tree nodes))
  121.  
  122. leaves_of_tree :: Rose_Tree a -> [a]
  123. leaves_of_tree (Rose_Node e nodes) = case nodes of
  124.    [] -> [e]
  125.    _  -> concatMap leaves_of_tree nodes
  126.  
  127. map_function_over_rose_tree :: (a -> b) -> Rose_Tree a -> Rose_Tree b
  128. map_function_over_rose_tree f (Rose_Node e nodes) = Rose_Node (f e) (map (\x -> map_function_over_rose_tree f x) nodes)
  129.  
  130.  
  131. -- Lab 8 Arithmetic Tree Expressions
  132. data Expression a = Number a | Node (Expression a) Operand (Expression a)
  133.    deriving (Show, Eq)
  134. data Operand = Plus | Minus | Times | Divided_By | Power
  135.    deriving (Show, Eq)
  136.    
  137.    
  138. eval :: (Floating a, Eq a) => Expression a -> Expression a
  139. eval expr = Number (eval_sub expr)
  140.    where
  141.       eval_sub :: (Floating a, Eq a) => Expression a -> a
  142.       eval_sub expr = case expr of
  143.          Number x -> x
  144.          (Node expr_l op expr_r) -> case op of
  145.             Plus        -> (eval_sub expr_l) + (eval_sub expr_r)
  146.             Minus       -> (eval_sub expr_l) - (eval_sub expr_r)
  147.             Times       -> (eval_sub expr_l) * (eval_sub expr_r)
  148.             Divided_By
  149.                | d == 0    -> error "Zero division!"
  150.                | otherwise -> eval_sub expr_l / d
  151.                where
  152.                   d = eval_sub expr_r
  153.             Power       -> (eval_sub expr_l) ** (eval_sub expr_r)
  154.  
  155.  
  156. -- Lab 9 Higher order prime test
  157. is_prime :: Integer -> Bool
  158. is_prime num = num > 1 && (num == 2 || null (filter (\x -> num `mod` x == 0) [2 .. num - 1]))
  159.  
  160. prime_factors :: Integer -> [Integer]
  161. prime_factors num
  162.    | null factor || num < 2   = []
  163.    | otherwise                = factor ++ prime_factors (num `div` (head factor))
  164.    where
  165.       factor = take 1 (filter (\x -> is_prime x && num `mod` x == 0) [2 .. num])
  166.  
  167.  
  168. -- Lab 9 Higher order functions in use
  169. import Data.Char
  170.  
  171. convert_to_lower :: String -> String
  172. convert_to_lower str = map toLower str
  173.  
  174. remove_non_alphanum :: String -> String
  175. remove_non_alphanum str = filter (`elem` (['a'..'z'] ++ ['A'..'Z'] ++ ['0' .. '9'])) str
  176.  
  177. dot_product :: [Integer] -> [Integer] -> Integer
  178. dot_product l1 l2 = sum (zipWith (*) l1 l2)
  179.  
  180. is_Square :: Integral a => a -> Bool
  181. is_Square i = floor ((sqrt (fromIntegral i) :: Float)) ^ (2 :: Integer) == i
  182.  
  183. sum_of_squares_up_to :: Integer -> Integer
  184. sum_of_squares_up_to limit = sum (filter is_Square [1 .. limit])
  185.  
  186. largest_square_smaller_than :: Integer -> Integer
  187. largest_square_smaller_than limit = last (filter (\x -> is_Square x && x < limit) [1 .. limit - 1])
  188.  
  189.  
  190. -- Lab 9 Higher order functions polymorphic
  191. apply_function_over_list' :: (a -> b) -> [a] -> [b]
  192. apply_function_over_list' f list = case list of
  193.    []       -> []
  194.    x : xs   -> f x : apply_function_over_list' f xs
  195.  
  196. select_where_true' :: (a -> Bool) -> [a] -> [a]
  197. select_where_true' f list = case list of
  198.   [] -> []
  199.   x : xs
  200.      | f x       -> x : select_where_true' f xs
  201.       | otherwise -> select_where_true' f xs
  202.      
  203. combine_lists_with_binary_operation :: (a -> b -> c) -> [a] -> [b] -> [c]
  204. combine_lists_with_binary_operation f l1 l2 = case (l1, l2) of
  205.   (x : xs, y : ys)  -> f x y : combine_lists_with_binary_operation f xs ys
  206.   _                 -> []
  207.  
  208. combine_elements_into_tuples :: [a] -> [b] -> [(a, b)]
  209. combine_elements_into_tuples l1 l2 = case (l1, l2) of
  210.   (x : xs, y : ys)  -> (x, y) : combine_elements_into_tuples xs ys
  211.   _                 -> []
  212.  
  213. combine_elements_into_tuples' :: [a] -> [b] -> [(a, b)]
  214. combine_elements_into_tuples' l1 l2 = combine_lists_with_binary_operation (,) l1 l2
  215.  
  216.  
  217. -- Lab 9 Higher order functions
  218. apply_function :: (Integer -> Integer) -> Integer -> Integer
  219. apply_function f num = f num
  220.  
  221. apply_function_over_list :: (Integer -> Integer) -> [Integer] -> [Integer]
  222. apply_function_over_list f list = case list of
  223.   []       -> []
  224.   x : xs   -> f x : apply_function_over_list f xs
  225.  
  226. select_where_true :: (Double -> Bool) -> [Double] -> [Double]
  227. select_where_true f list = case list of
  228.   [] -> []
  229.   x : xs
  230.      | f x       -> x : select_where_true f xs
  231.      | otherwise -> select_where_true f xs
Advertisement
Add Comment
Please, Sign In to add comment