Advertisement
blackpab

haskell

Apr 16th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. module Lists where
  2.  
  3. -- 4.1.1
  4. {--
  5. *Lists> :t sum
  6. sum :: (Num a, Foldable t) => t a -> a
  7. *Lists> :t map
  8. map :: (a -> b) -> [a] -> [b]
  9. *Lists> map (^2) [1,3,5]
  10. [1,9,25]
  11. *Lists> map (\x -> x*x) [1,3,5]
  12. [1,9,25]
  13. *Lists> let f = (^2) in map f [1,3,5]
  14. [1,9,25]
  15. *Lists> sqrt (sum [1,3,5])
  16. 3.0
  17. --}
  18.  
  19. sumOfSquares x = sum (map (^2) x)
  20.  
  21. {--
  22. *Lists> sumOfSquares [1,3,5]
  23. 35
  24. --}
  25.  
  26. -- 4.1.2
  27. {--
  28. *Lists> :t (:)
  29. (:) :: a -> [a] -> [a]
  30. *Lists> :t (++)
  31. (++) :: [a] -> [a] -> [a]3
  32. *Lists> 3 : [5,7]
  33. [3,5,7]
  34. *Lists> [3,5] ++ [7]
  35. [3,5,7]3
  36. *Lists> 3:5:[7]
  37. [3,5,7]
  38. *Lists> 3:(5:[7])
  39. [3,5,7]
  40. --}
  41.  
  42. f(x:xs)=(x,xs)
  43. {--
  44. *Lists> f[3,5,7]
  45. (3,[5,7])
  46. *Lists> f[3]
  47. (3,[])
  48. *Lists> f[]
  49. *** Exception: lab4.hs:42:1-14: Non-exhaustive patterns in function f
  50. --}
  51.  
  52.  
  53.  
  54. sumOfSquares' [] = 0
  55. sumOfSquares' (x:xs) = x^2 + sumOfSquares' xs
  56. {--
  57. *Lists> sumOfSquares' []
  58. 0
  59. *Lists> sumOfSquares' [1,3,5]
  60. 35
  61. --}
  62.  
  63. -- 4.2
  64. {--
  65. let f0(x:xs) = x
  66. *Lists> let f1(_:x:xs) = x
  67. *Lists> let f2(_:_:x:xs) = x
  68. *Lists> let f1(_:x:xs) = x
  69. *Lists> let f2(_:_:x:xs) = x
  70. *Lists> f1 [1,3,5,7,9]
  71. 3
  72. *Lists> f1[5,7]
  73. 7
  74. *Lists> f1[5]
  75. *** Exception: <interactive>:4:5-18: Non-exhaustive patterns in function f1
  76.  
  77. *Lists> f1[3]
  78. -- *** Exception: <interactive>:4:5-18: Non-exhaustive patterns in function f1
  79. --}
  80.  
  81. -- 4.2.1
  82. sum1 [ ] = 0
  83. sum1 [_] = 0
  84. sum1 (_:x:xs) = x + sum xs
  85. {--
  86. <interac
  87. *Lists> sum1 [ ]
  88. 0
  89. *Lists> sum1 [4]
  90. 0
  91. *Lists> sum1 [2,3,5,7,8]
  92. 23
  93. --}
  94.  
  95. -- 4.3
  96. {--
  97. *Lists> :t "Ala"
  98. "Ala" :: [Char]
  99. *Lists> ['A','l','a']
  100. "Ala"
  101. *Lists> "Ala" ++ " ma kota"
  102. "Ala ma kota"
  103. *Lists> [3,5] ++ [7,9]
  104. [3,5,7,9]
  105. *Lists> let mala x = 'a' <= x && x <= 'z'
  106. *Lists> mala 'x'
  107. True
  108. *Lists> mala 'C'
  109. False
  110. --}
  111. countLower :: String -> Int
  112. countLower [] = 0
  113. countLower (x:xs) | 'a' <= x && x <= 'z' = 1 + countLower xs
  114. | otherwise = countLower xs
  115. {--
  116. *Lists> countLower "mala"
  117. 4
  118. *Lists> countLower []
  119. 0
  120. *Lists> countLower "qbcdefghi"
  121. 9
  122. *Lists>
  123. --}
  124. cgtx :: Ord a => a -> [a] -> Int --a nalezy do typow uporzadkowanych
  125. cgtx x [] = 0
  126. cgtx x(y:ys) | x < y = 1+ cgtx x ys
  127. | otherwise = cgtx x ys
  128.  
  129.  
  130. gtx :: Ord a => a -> [a] -> [a]
  131. gtx x [] = [ ]
  132. gtx x (y:ys) | x < y = y : gtx x ys
  133. | otherwise = gtx x ys
  134. {-- 4.5
  135. *Lists> cgtx 2 [5, 4, 3,1]
  136. 3
  137. *Lists> cgtx 5 [-1, -3, -4, 7, 8, 9]
  138. 3
  139. *Lists>
  140. --}
  141.  
  142. {-- 4.6
  143. *Lists> read "52"
  144. *** Exception: Prelude.read: no parse
  145. *Lists> read "52" :: Int
  146. 52
  147. *Lists> show 52
  148. "52"
  149.  
  150. --}
  151.  
  152. string2int x = read x :: Int
  153. string2int' :: String -> Int
  154. string2int' x = read x
  155. {--
  156. *Lists> string2int "54"
  157. 54
  158. *Lists> string2int' "54"
  159. 54
  160. --}
  161.  
  162. {-- 4.7
  163. [1..5]
  164. [n | n <- [1..5]]
  165.  
  166. valueOf f x =
  167. f x
  168. {--
  169. *Lists> valueOf(^4) 2
  170. 16
  171. *Lists> valueOf(\x -> x^4) 2
  172. 16
  173. *Lists> let f = (^4) in valueOf f 2
  174. 16
  175. *Lists>
  176. --}
  177.  
  178. suma1 = sum(map(1/)[1..100])
  179. suma1' = sum [1/n | n <- [1..100]]
  180. --}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement