Guest User

Untitled

a guest
Dec 15th, 2018
65
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-
  2. WIELOLINIJKOWY
  3. KOMENTARZ
  4. -}
  5. --zadanie1
  6. let f xs = [k | k <- xs, k `mod` (head xs) /= 0]
  7. let l = f [2..]
  8.  
  9. let primes = map head (iterate f l)
  10.  
  11. --zadanie2
  12. primes' = 2:[p | p <- [3..], all (\x -> p `mod` x /= 0) (takeWhile (\x -> x*x <= p) primes')]
  13.  
  14. --zadanie3
  15. let fib = 1:1:(zipWith (+) fib (tail fib))
  16.  
  17.  
  18. --zadanie4
  19. :{
  20. iperm :: [a] -> [[a]]
  21. iperm [] = [[]]
  22. iperm (x:xs) =
  23. foldr (++) [] (map (insert [] x) (iperm xs))
  24. where
  25. insert :: [a] -> a -> [a] -> [[a]]
  26. insert xs x [] = [xs ++ [x]]
  27. insert xs x (y:ys) =
  28. (xs ++ (x : y : ys)) : (insert (xs ++ [y]) x ys)
  29. :}
  30.  
  31. insEv :: a -> [a] -> [[a]]
  32. insEv x [] = [[x]]
  33. insEv x (ys as (y:ys')) = (x:ys):[y:zs | zs <- insEv x ys'] (lub map (y:) (insEv x ys'))
  34. (list comprehension to inna skladnia na concatmapa)
  35.  
  36. iperm (x:xs) = [p' | p' <- insEv x p, p <- iperm xs]
  37.  
  38.  
  39. select
  40. chcemy uzyskac element i reszte elementow
  41. split :: [a] -> [(a,[a])]
  42. split [x] = [(x, [])]
  43. split x:xs = (x:xs):[(y,x:ys) | (y,ys) <- split xs]
  44.  
  45. sperm [] -> [[]]
  46. sperm xs = [x:p | (x, r) <- split xs, p<- sperm r]
  47.  
  48.  
  49. --zadanie5
  50. :{
  51. sublist :: [a] -> [[a]]
  52. sublist [] = [[]]
  53. sublist (x:xs) = let sxs = sublist xs in (map (\ys -> x:ys) sxs) ++ sxs
  54. :}
  55. sublist (x:xs) = [x:sub | sub <- subs] ++ subs where subs = sublist xs
  56.  
  57. --zadanie6
  58. --to jest zip
  59. diagonal _ [] = []
  60. diagonal [] _ = []
  61. diagonal (x:xs) (y:ys) = (x,y):(diagonal xs ys)
  62.  
  63. walk xs rys (y:ys) = (diagonal xs rys):(walk xs (y:rys) ys)
  64. walk (x:xs) rys [] = (diagonal xs rys):(walk xs rys [])
  65. walk [] _ _ = []
  66.  
  67. (><) xs ys = concat (walk xs [] ys)
  68.  
  69. --zadanie7
  70. data Tree a = Node (Tree a) a (Tree a) | Leaf
  71. data Set a = Fin (Tree a) | Cofin (Tree a)
  72. :{
  73.  
  74. --tego brakuje
  75. --setUnion, setIntersection :: Ord a => Set a -> Set a -> Set a
  76. --setComplement :: Ord a => Set a -> Set a
  77.  
  78. setComplement (Fin t) = (Cofin t)
  79. setComplement (Cofin t) = (Fin t)
  80.  
  81. --mozemy wziac wierzcholek pierwszego drzewa i podzielic drugie drzewo na dwa (mniejsze od tego elementu i wieksze od tego elementu)
  82. -- trzeba napisac splita do dzielenia drzewa
  83. -- potem bierzemy czesc wspolna lewego podrzewa nr 1 z lewym ktore powstalo po podzieleniu i prawego z prawym ktore powstalo
  84. setIntersection Node (alt av art) Node (blt bv brt)
  85. case compare av bv of
  86. EQ -> Node (setIntersection alt blt) av (setIntersection art brt)
  87.  
  88. --dzielenie drzewa wzgledem x
  89. split x (Node l y r) =
  90. case compare x y of
  91. | EQ -> (l,r)
  92. | Lt -> let (l',r') = split x l in (l', Node r' y r)
  93. | Gt -> let (l',r') = split x r in (Node l y l', r')
  94.  
  95. tunion Leaf t = t
  96. tunion (Node l x r) t = Node (tunion l l') x (tunion r, r')
  97. where (l',r') = split x t
  98.  
  99. tintersection --donapisania
  100.  
  101. insert :: Ord a => a -> Tree a -> Tree a
  102. insert a Leaf = Node Leaf a Leaf
  103. insert a (Node left val right)
  104. | a < val = Node (insert a left) val right
  105. | otherwise = Node left val (insert a right)
  106.  
  107. buildTree :: Ord a => [a] -> Tree a
  108. buildTree [] = Leaf
  109. buildTree (x:xs) = insert x $ buildTree xs
  110.  
  111. setFromList :: Ord a => [a] -> Set a
  112. setFromList [] = Fin Leaf
  113. setFromList (x:xs) = Fin (buildTree (x:xs))
  114.  
  115. setEmpty :: Ord a => Set a
  116. setEmpty = Fin Leaf
  117.  
  118. setFull :: Ord a => Set a
  119. setFull = Cofin Leaf
  120.  
  121.  
  122. setMember :: Ord a => a -> Set a -> Bool
  123. setMember _ (Fin Leaf) = False
  124. setMember a (Cofin s) = not (setMember a (Fin s))
  125. setMember a (Fin (Node left val right))
  126. | a == val = True
  127. | otherwise = setMember a (Fin left) || setMember a (Fin right)
  128. :}
  129.  
  130.  
  131.  
  132. --ZADANIE 4
  133.  
  134. iperm :: [a] -> [[a]]
  135.  
  136. iperm [] = [[]]
  137. iperm (x:xs) =
  138. foldr (++) [] (map (insert [] x) (iperm xs))
  139. where
  140. insert :: [a] -> a -> [a] -> [[a]]
  141. insert xs x [] = [xs ++ [x]]
  142. insert xs x (y:ys) =
  143. (xs ++ (x : y : ys)) : (insert (xs ++ [y]) x ys)
  144.  
  145. sperm :: [a] -> [[a]]
  146. sperm [] = []
  147. sperm [x] = [[x]]
  148. sperm xs =
  149. [y:zs | (y,ys) <- select xs, zs <- sperm ys]
  150. where
  151. select :: [a] -> [(a, [a])]
  152. select [x] = [(x, [])]
  153. select (x:xs) = (x, xs) : [(y, x:ys) | (y, ys) <- select xs]
  154.  
  155.  
  156. --ZADANIE 5
  157. sublist :: [a] -> [[a]]
  158. sublist [] = [[]]
  159. sublist (x:xs) = [x:sublists | sublists <- sublist xs] ++ sublist xs
RAW Paste Data