Advertisement
Guest User

Untitled

a guest
Jun 7th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. doubleMe x = x + x  
  3.  
  4. doubleUs x y = doubleMe x + doubleMe y  
  5.  
  6. hypotenuse a b = sqrt (a^2 + b^2)
  7.  
  8.  
  9. identifyCamel humps = if humps == 1
  10.                         then "Dromader"
  11.                         else "Dwugarbny"
  12.  
  13.  
  14. doubleSmallNumber x = if x > 100  
  15.                             then x  
  16.                             else x*2  
  17.  
  18.  
  19. --inna wersja
  20. doubleSmallNumber' x = (if x > 100 then x else x*2) + 1
  21.  
  22.  
  23.  
  24. --succ 8  to 9
  25.  
  26.  
  27. --ghci> 5:[1,2,3,4,5]  
  28. --[5,1,2,3,4,5]  
  29.  
  30.  
  31. --ghci> "Steve Buscemi" !! 6  
  32. --'B'  
  33. --Lista zawsze zaczyna się od 0
  34.  
  35.  
  36. --[1,2,3,4]
  37. --Head 1
  38. --Tail [2,3,4]
  39. --Init [1,2,3]
  40. --Last 4
  41.  
  42. --ghci> take 3 [5,4,3,2,1]  
  43. --[5,4,3]
  44. --ghci> take 10 (cycle [1,2,3])  
  45. --[1,2,3,1,2,3,1,2,3,1]  
  46.  
  47.  
  48. --ghci> drop 3 [8,4,2,1,5,6]  
  49. --[1,5,6]  
  50.  
  51. --ghci> sum [5,2,1,6,3,2,5,7]  
  52. --31  
  53.  
  54. --ghci> 4 `elem` [3,4,5,6]  
  55. --True                           4 element z listy
  56.  
  57. --Texas Range
  58. --ghci> [2,4..20]  dwie "." zamiast pisania
  59. --[2,4,6,8,10,12,14,16,18,20]  
  60.  
  61.  
  62.  
  63. boomBangs xs = [ if x < 10 then "BOOM!" else "BANG!" | x <- xs, odd x]  
  64.  
  65. --ghci> [x*2 | x <- [1..10], x*2 >= 12]  
  66. --[12,14,16,18,20]                            Zasada | Wejście , Warunek
  67.  
  68. length' xs = sum [1 | _ <- xs]   --_ means that we don't care what we'll draw from the list anyway so instead of writing a variable name that we'll never use, we just write _
  69.  
  70. --ghci> [ x*y | x <- [2,5,10], y <- [8,10,11], x*y > 50]  
  71. --[55,80,100,110]                                          Przykład z 2 argumentami  
  72.  
  73. removeNonUppercase :: [Char] -> [Char]  
  74. removeNonUppercase st = [ c | c <- st, c `elem` ['A'..'Z']]  
  75.  
  76.  
  77. --Tuples  
  78. --ghci> fst ("Wow", False)  
  79. --"Wow"  
  80. --ghci> snd ("Wow", False)  
  81. --False  
  82.  
  83. --ghci> zip [1,2,3,4,5] [5,5,5,5,5]  
  84. --[(1,5),(2,5),(3,5),(4,5),(5,5)]       zip łączy 2 listy w jedną listę par
  85.  
  86.  
  87. --let triangles = [ (a,b,c) | c <- [1..10], b <- [1..10], a <- [1..10] ]            wypisze wszystkie opcje czyli 100 wartości
  88.  
  89. --ghci> let rightTriangles' = [ (a,b,c) | c <- [1..10], b <- [1..c], a <- [1..b], a^2 + b^2 == c^2, a+b+c == 24]                obwód = 24 i poprawnie zbudowane
  90.  
  91.  
  92. --------------------------------------------------------------------------------------------------
  93. --Value -> Type -> Typeclass
  94.  
  95. f :: [Int] -> Int
  96. f ls = head ls + length ls
  97.  
  98. dividesEvenly :: Int -> Int -> Bool
  99. dividesEvenly x y = (y `div` x) * x == y
  100.  
  101.  
  102. --ghci> :t (==)  
  103. --(==) :: (Eq a) => a -> a -> Bool                   => symbol is called a class constraint.
  104. --We can read the previous type declaration like this: the equality function takes any two values that are of the same type and returns a Bool. The type of those two values must be a member of the Eq class
  105.  
  106. --ghci> :t (>)  
  107. --(>) :: (Ord a) => a -> a -> Bool  
  108.  
  109. --ghci> show 5.334  
  110. --"5.334"
  111.  
  112. --ghci> read "8.2" + 3.8  
  113. --12.0  
  114.  
  115. --ghci> [3 .. 5]  
  116. --[3,4,5]  
  117. --ghci> succ 'B'  
  118. --'C'               enum
  119.  
  120. --ghci> minBound :: Int  
  121. --(-2147483648)  
  122. --ghci> maxBound :: Char  
  123. --'\1114111'  
  124.  
  125. --ghci> :t (*)  
  126. --(*) :: (Num a) => a -> a -> a  
  127.  
  128. --fromIntegral (length [1,2,3,4]) + 3.2
  129.  
  130. ---- Eq is used for types that support equality testing
  131. ---- Ord is for types that have an ordering.
  132. ---- Members of Show can be presented as strings.
  133. ---- Read is sort of the opposite typeclass of Show. The read function takes a string and returns a type which is a member of Read.
  134. ---- Enum members are sequentially ordered types — they can be enumerated. The main advantage of the Enum typeclass is that we can use its types in list ranges
  135. ---- Bounded members have an upper and a lower bound.
  136. ---- Num is a numeric typeclass. Its members have the property of being able to act like numbers. Let's examine the type of a number.
  137. ---- Integral is also a numeric typeclass. Num includes all numbers, including real numbers and integral numbers, Integral includes only integral (whole) numbers. In this typeclass are Int and Integer.
  138. ---- Floating includes only floating point numbers, so Float and Double.
  139. ---- A very useful function for dealing with numbers is fromIntegral. It has a type declaration of fromIntegral :: (Num b, Integral a) => a -> b
  140.  
  141.  
  142. lucky :: (Integral a) => a -> String  
  143. lucky 7 = "LUCKY NUMBER SEVEN!"  
  144. lucky x = "Sorry, you're out of luck, pal!"  
  145.  
  146. factorial :: (Integral a) => a -> a  
  147. factorial 0 = 1  
  148. factorial n = n * factorial (n - 1)  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155. ----------------------------------------------------------------------------------------------------
  156.  
  157.  
  158.  
  159. --1. Prosty program:
  160.  --  - odczytanie danych ze standardowego wejścia
  161.   -- - proste obliczenie
  162.   -- - wydrukowanie wyniku na standardowe wyjście
  163.  
  164. --2. Funktory:
  165.   -- - umiejętność wykorzystania operacji: fmap, <*>, >>=, >> do przeprowadzenia działań wewnątrz funktora
  166.   -- - definiowanie instancji klas Funktor, Applicative, Monad dla prostych funktorów (lista, drzewo, Maybe, Either)
  167.   --   (patrz dyda -> Może.hs, Stan.hs)
  168.  
  169. --https://hckr.pl/tlumaczenia/Adit/Funktory_funktory_aplikatywne_i_monady_w_obrazkach.html
  170.  
  171.  
  172.   --Definicja funktora--
  173. class Functor f where
  174.     fmap :: (a -> b) -> f a -> f b
  175.  
  176. -- jak fmap funktora odnosi sie do map (tablice)--
  177. instance Functor [] where
  178.     fmap = map
  179.  
  180. -- funktor i Maybe
  181. data Maybe a = Nothing | Just a
  182.  
  183. instance Functor Maybe where
  184.     fmap f (Just x) = Just (f x)
  185.     fmap f Nothing = Nothing
  186.  
  187. --funktor i drzewo
  188. instance Functor Tree where
  189.     fmap _ EmptyTree = EmptyTree
  190.     fmap f (Node x leftsub rightsub) = Node (f x) (fmap f leftsub) (fmap f rightsub)
  191.  
  192. --funktor i Either
  193. data Either a b = Left a | Right b
  194.     fmap f (Right x) = Right (f x)
  195.     fmap f (Left x) = Left x
  196.  
  197.  
  198.  
  199.  
  200. --Funktory aplikatywne (Applicative) wchodzą na nieco wyższy poziom.
  201. --Podobnie jak ze zwyczajnymi funktorami (Functor), nasze wartości są opakowane w kontekst
  202.  
  203.  
  204. --Control.Applicative definiuje <*>
  205.  
  206. -- http://learnyouahaskell.com/a-fistful-of-monads
  207.  
  208. (<*>) :: (Applicative f) => f (a -> b) -> f a -> f b
  209.  
  210. -- Funktor aplikatywny:
  211. -- > (+) <$> (Just 5)
  212. --Just (+5)
  213. -- > Just (+5) <*> (Just 3)
  214. --Just 8
  215.  
  216. --3. Obliczenia monadowe (patrz dyda->ewaluator[12].hs)
  217.   -- - przekształcenie zwykłej funkcji na funkcję wykonującą obliczenie w monadzie
  218.   -- - uruchomienie obliczenia wewnątrz monady
  219.  
  220. half x = if even x
  221.     then Just (x `div` 2)
  222.     else Nothing
  223.  
  224. --Musimy użyć >>=, żeby wepchnąć naszą zapakowaną wartość do tej funkcji
  225.  
  226. -- > Just 3 >>= half
  227. -- Nothing
  228. -- > Just 4 >>= half
  229. -- Just 2
  230. -- > Nothing >>= half
  231. -- Nothing
  232.  
  233. class Monad m where
  234.     (>>=) :: m a -> (a -> m b) -> m b
  235.     (>>) :: m a -> m b -> m b
  236.     return :: a -> m a
  237.     fail :: String -
  238.  
  239. --(>>=) :: (Monad m) => m a -> (a -> m b) -> m b  
  240.  
  241.  
  242. -- >>= bierze monadę (jak Just 3)
  243. -- i funkcję, która zwraca monadę (jak half)
  244. -- i zwraca monadę.
  245.  
  246. instance Monad Maybe where
  247.     Nothing >>= func = Nothing
  248.     Just val >>= func  = func val
  249.  
  250. --Wywołania można też połączyć w łańcuch:
  251.  
  252. -- > Just 20 >>= half >>= half >>= half
  253. -- Nothing (20 ok, 10 ok, 5 złe)
  254.  
  255.  
  256.  
  257. --4. Transformery monad: (patrz dyda->ewaluator[12].hs)
  258.   -- - umiejętność zbudowania 2-3 wartwowej monady z użyciem transformerów monad
  259.   -- - uruchomienie obliczenia i "odpakowanie" wyniku
  260. --https://www.damii.pl/speakers/Haskell2
  261.  
  262.   myAdd:: [ Double ] -> [ Double ] -> [ Double ]
  263.   myAdd mx my = do
  264.        x <- mx;
  265.        y <- my;
  266.        return (x + y)
  267.  
  268.   myDiv:: [ Double ] -> [ Double ] -> [ Double ]
  269.   myDiv mx my = do
  270.        x <- mx;
  271.        y <- my;
  272.        case y of
  273.          0 -> []
  274.          otherwise -> [x / y]
  275.  
  276.   myPrint :: Show a => [ a ] -> IO ()
  277.   myPrint ( [ x ] ) = print x
  278.   myPrint n         = print n
  279.    
  280.   main = do
  281.        myPrint(myAdd [ 2.0 ] (myDiv [ 10.0 ] [ 3.0 ]))
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.       -- ghci> (\x -> Just (x+1)) 1  
  294.       -- Just 2  
  295.      --  ghci> (\x -> Just (x+1)) 100  
  296.       -- Just 101  
  297.  
  298.  
  299. applyMaybe :: Maybe a -> (a -> Maybe b) -> Maybe b  
  300. applyMaybe Nothing f  = Nothing  
  301. applyMaybe (Just x) f = f x  
  302.  
  303. -- hci> Just 3 `applyMaybe` \x -> Just (x+1)  
  304. -- Just 4  
  305. -- ghci> Just "smile" `applyMaybe` \x -> Just (x ++ " :)")  
  306. -- Just "smile :)"
  307.  
  308.  
  309. --ghci> Just 3 `applyMaybe` \x -> if x > 2 then Just x else Nothing  
  310. --Just
  311.  
  312.  
  313.  
  314. --monada
  315.  
  316. --ghci> Just 9 >>= \x -> return (x*10)  
  317. --Just 90
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement