Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Task 1
  2. repeat1 :: Int -> a -> [a]
  3. repeat1 0 x = []
  4. repeat1 n x = x : repeat1 (n - 1) x
  5.  
  6. repeatAll :: Int -> [a] -> [a]
  7. repeatAll 0 list = []
  8. repeatAll n [] = []
  9. repeatAll n (x : xs) = (repeat1 n x) ++ (repeatAll n xs)
  10.  
  11. -- Task 2
  12. removeNth :: Int -> [a] -> [a]
  13. removeNth n list = removeNth' n 1 list
  14.  
  15. removeNth' :: Int -> Int -> [a] -> [a]
  16. removeNth' n cur_pos [] = []
  17. removeNth' n cur_pos (x : xs)   | (mod cur_pos n) == 0 = removeNth' n (cur_pos + 1) xs
  18.                                 | otherwise = x : (removeNth' n (cur_pos + 1) xs)
  19.                                
  20. -- Task 3
  21. getSubList :: Int -> Int -> [a] -> [a]
  22. getSubList n k [] = []
  23. getSubList 0 0 (x : xs) = []
  24. getSubList 0 k (x : xs) | k < 0 = error "bad bound"
  25.                         | otherwise = x : (getSubList 0 (k - 1) xs)
  26. getSubList n k (x : xs) = getSubList (n - 1) (k - 1) xs
  27.  
  28. -- Task 4
  29. shift :: Int -> [a] -> [a]
  30. shift n list    | n > 0 = reverse (shift (-n) (reverse list))
  31.                 | n == 0 = list
  32.                 | (-n) > length(list) = shift (-(mod (-n) (length list))) list
  33.                 | otherwise = (getLastN (-n) list) ++ (removeLastN (-n) list)
  34.                
  35. getLastN :: Int -> [a] -> [a]
  36. getLastN n list | n < 0 = error "n < 0"
  37.                 | n >= length(list) = list
  38.                 | otherwise = getLastN n (tail list)
  39.                
  40. removeLastN :: Int -> [a] -> [a]
  41. removeLastN n list  | n < 0 = error "n < 0"
  42.                     | n >= length(list) = []
  43.                     | otherwise = (head list) : (removeLastN n (tail list))
  44.                    
  45. -- Task 5
  46. remove :: Int -> [a] -> (a, [a])
  47. remove n [] = error "element not found"
  48. remove 0 (x : xs) = (x, xs)
  49. remove n (x : xs)   | n < 0 = error "negative index"
  50.                     | otherwise = (fst p, x : snd p)
  51.                         where p = remove (n - 1) xs
  52.                    
  53. -- Task 6
  54. pushToAll :: a -> [[a]] -> [[a]]
  55. pushToAll z [] = [[z]]
  56. pushToAll z [x] = [z : x]
  57. pushToAll z (x : xs) = (z : x) : (pushToAll z xs)
  58.  
  59. combinations :: Int -> [a] -> [[a]]
  60. combinations n [] = []
  61. combinations 0 list = []
  62. combinations n (x : xs) | n < 0 = []
  63.                         | n > length(x : xs) = []
  64.                         | n == length(x : xs) = [x : xs]
  65.                         | otherwise = (pushToAll x (combinations (n - 1) xs)) ++ (combinations n xs)
  66.        
  67. data Tree a = Empty | Branch (Tree a) a (Tree a) deriving (Show, Eq)
  68. -- Task 7
  69. allBalanced :: Int -> [Tree ()]
  70. allBalanced 0 = [Empty]
  71. allBalanced n   | n < 0 = []
  72.                 | otherwise = allBalanced' (div (n - 1) 2) ((n - 1) - (div (n - 1) 2))
  73.                
  74. allBalanced' :: Int -> Int -> [Tree ()]
  75. allBalanced' left right | left < 0 || right < 0 = []
  76.                         | left < (right - 1) = allBalanced' (left + 1) (right - 1)
  77.                         | left > (right + 1) = []
  78.                         | otherwise = (mergeLists (allBalanced left) (allBalanced right)) ++ (allBalanced' (left + 1) (right - 1))
  79.                        
  80. mergeLists :: [Tree ()] -> [Tree ()] -> [Tree ()]
  81. mergeLists [] list2 = []
  82. mergeLists list1 [] = []
  83. mergeLists (x : xs) list2 = (mergeTree x list2) ++ (mergeLists xs list2)
  84.  
  85. mergeTree :: Tree () -> [Tree ()] -> [Tree ()]
  86. mergeTree tree [] = []
  87. mergeTree tree (x : xs) = (Branch (tree) () (x)) : (mergeTree tree xs)
  88.  
  89. -- Task 8
  90. buildTree :: [a] -> Tree a
  91. buildTree [] = Empty
  92. buildTree list = Branch (buildTree left) root (buildTree right)
  93.                     where
  94.                         (left, root, right) = split (div (length list) 2) list
  95.  
  96. split :: Int -> [a] -> ([a], a, [a])
  97. split pos list = split' pos list 0 []
  98.  
  99. split' :: Int -> [a] -> Int -> [a] -> ([a], a, [a])
  100. split' pos [] cur_pos left = error "root not found"
  101. split' pos (x : xs) cur_pos reversed_left   | cur_pos == pos = (reverse reversed_left, x, xs)
  102.                                             | otherwise = split' pos xs (cur_pos + 1) (x : reversed_left)
  103.  
  104. -- Task 9
  105. allBalancedH :: Int -> [Tree ()]
  106. allbalancedH 0 = [Empty]
  107. allBalancedH n = allBalancedH' n 0
  108.  
  109. -- fix n
  110. allBalancedH' :: Int -> Int -> [Tree ()]
  111. allBalancedH' n h   | h > n - 1 = []
  112.                     | otherwise = (allBalancedH'' n h) ++ (allBalancedH' n (h + 1))
  113.  
  114. -- fix n and h
  115. allBalancedH'' :: Int -> Int -> [Tree ()]
  116. allBalancedH'' 0 h  | h == (-1) = [Empty]
  117.                     | otherwise = []
  118. allBalancedH'' n h  | (h < 0) || (h > n - 1) = []
  119.                     | otherwise = (allBalancedH''' 0 (n - 1) (h - 2) (h - 1)) ++ (allBalancedH''' 0 (n - 1) (h - 1) (h - 2)) ++ (allBalancedH''' 0 (n - 1) (h - 1) (h - 1))
  120.        
  121. -- fix left_h right_h      
  122. allBalancedH''' :: Int -> Int -> Int -> Int -> [Tree ()]
  123. allBalancedH''' left_n right_n left_h right_h   | right_n < 0 = []
  124.                                                 | right_n == 0 = mergeLists (allBalancedH'' left_n left_h) (allBalancedH'' right_n right_h)
  125.                                                 | otherwise = (mergeLists (allBalancedH'' left_n left_h) (allBalancedH'' right_n right_h)) ++ (allBalancedH''' (left_n + 1) (right_n - 1) left_h right_h)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement