Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- doubleMe x = x + x
- doubleUs x y = doubleMe x + doubleMe y
- hypotenuse a b = sqrt (a^2 + b^2)
- identifyCamel humps = if humps == 1
- then "Dromader"
- else "Dwugarbny"
- doubleSmallNumber x = if x > 100
- then x
- else x*2
- --inna wersja
- doubleSmallNumber' x = (if x > 100 then x else x*2) + 1
- --succ 8 to 9
- --ghci> 5:[1,2,3,4,5]
- --[5,1,2,3,4,5]
- --ghci> "Steve Buscemi" !! 6
- --'B'
- --Lista zawsze zaczyna się od 0
- --[1,2,3,4]
- --Head 1
- --Tail [2,3,4]
- --Init [1,2,3]
- --Last 4
- --ghci> take 3 [5,4,3,2,1]
- --[5,4,3]
- --ghci> take 10 (cycle [1,2,3])
- --[1,2,3,1,2,3,1,2,3,1]
- --ghci> drop 3 [8,4,2,1,5,6]
- --[1,5,6]
- --ghci> sum [5,2,1,6,3,2,5,7]
- --31
- --ghci> 4 `elem` [3,4,5,6]
- --True 4 element z listy
- --Texas Range
- --ghci> [2,4..20] dwie "." zamiast pisania
- --[2,4,6,8,10,12,14,16,18,20]
- boomBangs xs = [ if x < 10 then "BOOM!" else "BANG!" | x <- xs, odd x]
- --ghci> [x*2 | x <- [1..10], x*2 >= 12]
- --[12,14,16,18,20] Zasada | Wejście , Warunek
- 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 _
- --ghci> [ x*y | x <- [2,5,10], y <- [8,10,11], x*y > 50]
- --[55,80,100,110] Przykład z 2 argumentami
- removeNonUppercase :: [Char] -> [Char]
- removeNonUppercase st = [ c | c <- st, c `elem` ['A'..'Z']]
- --Tuples
- --ghci> fst ("Wow", False)
- --"Wow"
- --ghci> snd ("Wow", False)
- --False
- --ghci> zip [1,2,3,4,5] [5,5,5,5,5]
- --[(1,5),(2,5),(3,5),(4,5),(5,5)] zip łączy 2 listy w jedną listę par
- --let triangles = [ (a,b,c) | c <- [1..10], b <- [1..10], a <- [1..10] ] wypisze wszystkie opcje czyli 100 wartości
- --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
- --------------------------------------------------------------------------------------------------
- --Value -> Type -> Typeclass
- f :: [Int] -> Int
- f ls = head ls + length ls
- dividesEvenly :: Int -> Int -> Bool
- dividesEvenly x y = (y `div` x) * x == y
- --ghci> :t (==)
- --(==) :: (Eq a) => a -> a -> Bool => symbol is called a class constraint.
- --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
- --ghci> :t (>)
- --(>) :: (Ord a) => a -> a -> Bool
- --ghci> show 5.334
- --"5.334"
- --ghci> read "8.2" + 3.8
- --12.0
- --ghci> [3 .. 5]
- --[3,4,5]
- --ghci> succ 'B'
- --'C' enum
- --ghci> minBound :: Int
- --(-2147483648)
- --ghci> maxBound :: Char
- --'\1114111'
- --ghci> :t (*)
- --(*) :: (Num a) => a -> a -> a
- --fromIntegral (length [1,2,3,4]) + 3.2
- ---- Eq is used for types that support equality testing
- ---- Ord is for types that have an ordering.
- ---- Members of Show can be presented as strings.
- ---- 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.
- ---- 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
- ---- Bounded members have an upper and a lower bound.
- ---- 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.
- ---- 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.
- ---- Floating includes only floating point numbers, so Float and Double.
- ---- A very useful function for dealing with numbers is fromIntegral. It has a type declaration of fromIntegral :: (Num b, Integral a) => a -> b
- lucky :: (Integral a) => a -> String
- lucky 7 = "LUCKY NUMBER SEVEN!"
- lucky x = "Sorry, you're out of luck, pal!"
- factorial :: (Integral a) => a -> a
- factorial 0 = 1
- factorial n = n * factorial (n - 1)
- ----------------------------------------------------------------------------------------------------
- --1. Prosty program:
- -- - odczytanie danych ze standardowego wejścia
- -- - proste obliczenie
- -- - wydrukowanie wyniku na standardowe wyjście
- --2. Funktory:
- -- - umiejętność wykorzystania operacji: fmap, <*>, >>=, >> do przeprowadzenia działań wewnątrz funktora
- -- - definiowanie instancji klas Funktor, Applicative, Monad dla prostych funktorów (lista, drzewo, Maybe, Either)
- -- (patrz dyda -> Może.hs, Stan.hs)
- --https://hckr.pl/tlumaczenia/Adit/Funktory_funktory_aplikatywne_i_monady_w_obrazkach.html
- --Definicja funktora--
- class Functor f where
- fmap :: (a -> b) -> f a -> f b
- -- jak fmap funktora odnosi sie do map (tablice)--
- instance Functor [] where
- fmap = map
- -- funktor i Maybe
- data Maybe a = Nothing | Just a
- instance Functor Maybe where
- fmap f (Just x) = Just (f x)
- fmap f Nothing = Nothing
- --funktor i drzewo
- instance Functor Tree where
- fmap _ EmptyTree = EmptyTree
- fmap f (Node x leftsub rightsub) = Node (f x) (fmap f leftsub) (fmap f rightsub)
- --funktor i Either
- data Either a b = Left a | Right b
- fmap f (Right x) = Right (f x)
- fmap f (Left x) = Left x
- --Funktory aplikatywne (Applicative) wchodzą na nieco wyższy poziom.
- --Podobnie jak ze zwyczajnymi funktorami (Functor), nasze wartości są opakowane w kontekst
- --Control.Applicative definiuje <*>
- -- http://learnyouahaskell.com/a-fistful-of-monads
- (<*>) :: (Applicative f) => f (a -> b) -> f a -> f b
- -- Funktor aplikatywny:
- -- > (+) <$> (Just 5)
- --Just (+5)
- -- > Just (+5) <*> (Just 3)
- --Just 8
- --3. Obliczenia monadowe (patrz dyda->ewaluator[12].hs)
- -- - przekształcenie zwykłej funkcji na funkcję wykonującą obliczenie w monadzie
- -- - uruchomienie obliczenia wewnątrz monady
- half x = if even x
- then Just (x `div` 2)
- else Nothing
- --Musimy użyć >>=, żeby wepchnąć naszą zapakowaną wartość do tej funkcji
- -- > Just 3 >>= half
- -- Nothing
- -- > Just 4 >>= half
- -- Just 2
- -- > Nothing >>= half
- -- Nothing
- class Monad m where
- (>>=) :: m a -> (a -> m b) -> m b
- (>>) :: m a -> m b -> m b
- return :: a -> m a
- fail :: String -
- --(>>=) :: (Monad m) => m a -> (a -> m b) -> m b
- -- >>= bierze monadę (jak Just 3)
- -- i funkcję, która zwraca monadę (jak half)
- -- i zwraca monadę.
- instance Monad Maybe where
- Nothing >>= func = Nothing
- Just val >>= func = func val
- --Wywołania można też połączyć w łańcuch:
- -- > Just 20 >>= half >>= half >>= half
- -- Nothing (20 ok, 10 ok, 5 złe)
- --4. Transformery monad: (patrz dyda->ewaluator[12].hs)
- -- - umiejętność zbudowania 2-3 wartwowej monady z użyciem transformerów monad
- -- - uruchomienie obliczenia i "odpakowanie" wyniku
- --https://www.damii.pl/speakers/Haskell2
- myAdd:: [ Double ] -> [ Double ] -> [ Double ]
- myAdd mx my = do
- x <- mx;
- y <- my;
- return (x + y)
- myDiv:: [ Double ] -> [ Double ] -> [ Double ]
- myDiv mx my = do
- x <- mx;
- y <- my;
- case y of
- 0 -> []
- otherwise -> [x / y]
- myPrint :: Show a => [ a ] -> IO ()
- myPrint ( [ x ] ) = print x
- myPrint n = print n
- main = do
- myPrint(myAdd [ 2.0 ] (myDiv [ 10.0 ] [ 3.0 ]))
- -- ghci> (\x -> Just (x+1)) 1
- -- Just 2
- -- ghci> (\x -> Just (x+1)) 100
- -- Just 101
- applyMaybe :: Maybe a -> (a -> Maybe b) -> Maybe b
- applyMaybe Nothing f = Nothing
- applyMaybe (Just x) f = f x
- -- hci> Just 3 `applyMaybe` \x -> Just (x+1)
- -- Just 4
- -- ghci> Just "smile" `applyMaybe` \x -> Just (x ++ " :)")
- -- Just "smile :)"
- --ghci> Just 3 `applyMaybe` \x -> if x > 2 then Just x else Nothing
- --Just
- --monada
- --ghci> Just 9 >>= \x -> return (x*10)
- --Just 90
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement