Advertisement
yingpotter

[99p]Lists(Complete)

Jun 29th, 2011
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import System.Random
  2. import Control.Monad (replicateM)
  3.  
  4. -- problem 1
  5. myLast :: [c] -> c
  6. myLast = head . reverse
  7.  
  8. -- problem 2
  9. myButLast :: [c] -> c
  10. myButLast = head. tail . reverse
  11.  
  12. -- problem 3
  13. elementAt :: Int -> [c] -> c
  14. elementAt x =myLast . take x
  15.  
  16. -- problem 4
  17. myLength :: Num a => [t] -> a
  18. myLength [] = 0
  19. myLength (x:xs) = 1 + myLength xs
  20.  
  21. -- problem 5
  22. rev :: [a] -> [a]
  23. rev [] = []
  24. rev (x:xs) = rev xs ++ [x]
  25.  
  26. -- problem 6
  27. isPalindrome :: Eq a => [a] -> Bool
  28. isPalindrome xs = (reverse xs == id xs)
  29.  
  30. -- problem 7
  31. data LiL a = Elem a | List [LiL a]
  32. flatten :: LiL a -> [a]
  33. flatten (Elem a) = [a]
  34. flatten (List []) = []
  35. flatten (List (x:xs)) = flatten x ++ flatten (List xs)
  36.  
  37. -- problem 8
  38. compress :: Eq a => [a] -> [a]
  39. compress [] =[]
  40. compress [x] = [x]
  41. compress (x:xs)
  42.   | x == head xs   = compress xs
  43.   | x /= head xs   = x : compress xs
  44.  
  45. -- problem 9
  46. pack :: Eq a => [a] -> [[a]]
  47. pack [] = []
  48. pack [x] = [[x]]
  49. pack (x:xs)  
  50.   | x == head (head (pack xs))  = (x : (head (pack xs))) : (tail (pack xs))
  51.   | x /= head (head (pack xs))  = [x] : (pack xs)
  52.  
  53. -- problem 10
  54. encode :: (Num a, Eq b) =>[b] -> [(a,b)]
  55. encode [] = []
  56. encode xs = zip (lenlist xs) (compress xs)
  57.             where lenlist xs =  map myLength (pack xs)
  58.  
  59. -- problem 11
  60. data LI a = S a | M Int a
  61.     deriving (Show)
  62. encodeM :: Eq a => [a] -> [LI a]
  63. encodeM = map encodeM0 . encode
  64.     where
  65.       encodeM0 (1,x) = S x
  66.       encodeM0 (n,x) = M n x
  67.      
  68. -- problem 12
  69. decode :: Eq a => [LI a] -> [a]
  70. decode = concatMap decodeM0
  71.         where decodeM0 (S x) = [x]
  72.               decodeM0 (M n x) = replicate n x
  73.  
  74. -- problem 13
  75. lenlistD :: (Num a, Eq a1) => [a1] -> [a]
  76. lenlistD [] = []
  77. lenlistD [x] = [1]
  78. lenlistD (x:xs)
  79.    | x == (head xs)    = head(lenlistD xs) + 1 : tail(lenlistD xs)
  80.    | otherwise         = 1 : lenlistD xs
  81. encodeD :: Eq a => [a] -> [LI a]
  82. encodeD xs = map encodeM0 (zip (lenlistD xs) (compress xs))
  83.                  where
  84.                    encodeM0 (1,x) = S x
  85.                    encodeM0 (n,x) = M n x
  86.                    
  87. -- problem 14
  88. dupli :: [a] -> [a]
  89. dupli [] =[]
  90. dupli (x:xs) = x:x:dupli xs
  91.  
  92. -- problem 15
  93. repli :: [a] -> Int -> [a]
  94. repli [] n = []
  95. repli (x:xs) n = replicate n x ++ repli xs n
  96.  
  97. -- problem 16
  98. dropEvery :: [a] -> Int -> [a]
  99. dropEvery [] n = []
  100. dropEvery xs n  = take (n-1) xs ++ dropEvery (drop n xs) n
  101.  
  102. -- problem 17
  103. splitD :: [a] -> Int -> ([a], [a])
  104. splitD [] n = ([], [])
  105. splitD xs 0 = ([],xs)
  106. splitD (x:xs) n = (x:fst(splitD xs (n-1)),snd(splitD xs (n-1)))
  107.  
  108. -- problem 18
  109. slice :: [a] -> Int -> Int -> [a]
  110. slice xs m n = fst (splitD (drop (m-1) xs) (n-m+1))
  111.  
  112. -- problem 19
  113. rotate :: [a] -> Int -> [a]
  114. rotate [] _ = []
  115. rotate xs m
  116.     | m >= 0   = snd(splitD xs m) ++ fst(splitD xs m)
  117.     | m < 0    = snd(splitD xs (length(xs)+m)) ++ fst(splitD xs (length(xs)+m))
  118.    
  119. -- problem 20
  120. removeAt :: Int -> [a] -> ([a],[a])
  121. removeAt n xs
  122.     = ( (take 1 (rotate xs (n-1))), rotate (drop 1 (rotate xs (n-1))) (1-n) )
  123.  
  124.    
  125. -- problem 21
  126. insertAt :: a-> [a] -> Int -> [a]
  127. insertAt x xs n
  128.     = fst(splitAt (n-1) xs)++[x]++snd(splitAt (n-1) xs)
  129.    
  130. -- problem 22
  131. range :: Int -> Int -> [Int]
  132. range m n
  133.    |m==n  = [m]
  134.    |m<n   = m : (range (m+1) n)
  135.    |m>n   = m : (range (m-1) n)
  136.  
  137. -- problem 23
  138. rnd_select :: [a] -> Int -> IO [a]
  139. rnd_select [] _ = return []
  140. rnd_select l  n
  141.     | n<0 = error "n cant be neg"
  142.     | otherwise = do pos <- replicateM n $
  143.                             getStdRandom $ randomR (0, (length l)-1)
  144.                      return [l!!p | p <- pos]
  145.                      
  146. -- problem 24
  147. diff_select n m = diff_sel n [1..m]
  148. diff_sel :: Num a => a -> [a1] -> IO [a1]
  149. diff_sel 0 _  = return []
  150. diff_sel _ [] = error "not enuff elements"
  151. diff_sel n xs = do g <- randomRIO (0,(length xs)-1)
  152.                    let remaining = take g xs ++ drop (g+1) xs
  153.                    rest <- diff_sel (n-1) remaining
  154.                    return ((xs!!g) : rest)
  155.  
  156. -- problem 25
  157. rnd_permu :: [a] -> IO [a]
  158. rnd_permu xs = diff_sel len xs
  159.                    where len = length xs
  160.                    
  161. -- problem 26
  162.  
  163. cmb :: Int-> [a] -> [[a]]
  164. cmb 0 _ = []
  165. cmb 1 xs = map f xs
  166.                where f x = [x]
  167. cmb n (x:xs)
  168.     | n<length(xs)+1  =(map (x: ) (cmb (n-1) xs)) ++ (cmb n xs)
  169.     | n==length(xs)+1  =[x:xs]
  170.     | n>length(xs)+1  = error "not enuff elements"
  171.  
  172. -- problem 27
  173. listminus :: Eq a => [[a]] -> [a] ->[[a]]
  174. listminus [] ys = []
  175. listminus xs [] = xs
  176. listminus (x:xs) ys = filter (`notElem` x) ys : (listminus xs ys)
  177. cmb_rest :: Eq a => Int -> [a] -> [([a],[a])]
  178. cmb_rest n xs = zip (cmb n xs) (listminus (cmb n xs) xs)
  179.  
  180. group :: Eq a => [Int] -> [a] -> [[[a]]]
  181. group [] _ = [[]]
  182. group (x:xs) ys  | sum (x:xs) /= (length ys)    = error "not compatible"
  183.                  | sum (x:xs) == (length ys)    = [ z:zs | (z,ws) <- cmb_rest x ys
  184.                                                   , zs <- group xs ws ]
  185.  
  186. {- quicksort
  187. quicksort :: Ord a => [a] -> [a]
  188. quicksort [] = []
  189. quicksort (x:xs) = quicksort small ++ (x : quicksort large)
  190.    where small = [ y | y <- xs, y <= x]
  191.          large = [ y | y <- xs, y > x]
  192. -}
  193.  
  194. -- problem 28
  195. ---- sortlength
  196. sortlength :: Ord a => [[a]] -> [[a]]
  197. sortlength [] = []
  198. sortlength (x:xs) = sortlength small ++ (x : sortlength large)
  199.    where small = [ y | y <- xs, length y <= length x]
  200.          large = [ y | y <- xs, length y > length x]
  201. ---- sortfreq
  202. lenfreq :: [[a]] -> [a] -> Int
  203. lenfreq [] x = 0
  204. lenfreq (y:ys) x = (if length x == length y then 1 else 0) + lenfreq ys x
  205. sortfreq :: [[a]] -> [[a]]
  206. sortfreq [] = []
  207. sortfreq (x:xs) =  sortfreq small ++ (x : sortfreq large)
  208.          where small = [ y | y <- xs, lenfreq (x:xs) y < lenfreq (x:xs) x]
  209.                large = [ y | y <- xs, lenfreq (x:xs) y >= lenfreq (x:xs) x]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement