-- komentovaný zdroják k druhému cviku, UTF-8 -- typování výrzazů -- postupujeme zvnitř, od podvýrazů: {- :t [ "a", "b", "c" ] ^^^ "a" :: [Char] ~~ String ^^^ "b" :: [Char] ^^^ "c" :: [Char] ^^^^^^^^^^^^^^^^^ :: [ [Char] ] :t [ 'a', 'b', 'c' ] ^^^ 'a' :: Char ^^^^^^^^^^^^^^^^^ :: [ Char ] :t "abc" :: [Char] :t [ (True, ()), (False, ()) ] ^^^^ True :: Bool ^^ () :: () ^^^^^ False :: Bool ^^ () :: () ^^^^^^^^^^ (True, ()) :: (Bool, ()) ^^^^^^^^^^^ (False, ()) :: (Bool, ()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ :: [ (Bool, ()) ] :t [ (&&), (||) ] ^^^^ (&&) :: Bool -> Bool -> Bool ^^^^ (||) :: Bool -> Bool -> Bool ^^^^^^^^^^^^^^ :: [ Bool -> Bool -> Bool ] plus :: Integer -> Integer -> Integer plus x y = x + y :t [ plus, (||) ] ^^^^ plus :: Integer -> Integer -> Integer ^^^^ (||) :: Bool -> Bool -> Bool typová chyba! :t [] :: [a] :t [[]] ^^ [] :: [a] ^^^^ [[a]] :t [ [], [""] ] ^^ [] :: [a] ^^ "" :: [Char] ^^^^ [""] :: [ [Char] ] ^^ [] -- substituce a / [Char] :: [ [Char] ] ^^^^^^^^^^^^ :: [ [ [Char] ] ] :t map toUpper "abc" ^^^ map :: (a -> b) -> [a] -> [b] ^^^^^^^ toUpper :: Char -> Char ^^^^^ "abc" :: [Char] ^^^ map -- substituce a/Char, b/Char :: (Char -> Char) -> [Char] -> [Char] (map toUpper) "abc" ^^^^^^^^^^^^^ map toUpper :: [Char] -> [Char] ^^^^^^^^^^^^^^^^^^^ :: [Char] -} {- LAMBDA FUNKCE - funcke nemusíme pojmenovávat, můžeme je definovat přímo - na místě použití -a - Lambda funkce používají jinou syntax naž klasickoé - začínají \ , místo = píšeme -> - - map (+ 4) [1,2,3] ~>* [1 + 4, 2 + 4, 3 + 4] - map (\x -> x + 5) [1,2,3] - - filter (\x -> x > 3 && x < 10) [1,2,3,7,5,9] -} -- > map (\x -> 3 * x) [ 1, 2, 3 ] ~>* [ 3, 6, 9 ] {- odstraňování formálních parametrů -- parametr můžeme odstranit nachází-li - se na levé straně definice úplně vpravo a na pravé straně jako poslední - parametr nejvnějšnějšího podvýrazu \x -> 3 * x ~~ \x -> (3 *) x -- operátorová sekcve ~~ (3 *) \x -> 3 * x ~~ \x -> (*) 3 x -- přepisem do prefixu ~~ (*) 3 -- částečná aplikace \x -> x ^ 3 ~~ \x -> (^ 3) x ~~ (^ 3) flip' :: (a -> b -> c) -> b -> a -> c flip' f x y = f y x -- flip (-) 3 2 ~> (-) 2 3 ~> -1 \x -> x ^ 3 ~~ \x -> (^) x 3 ^f^ ^x ^y ~~ \x -> flip (^) 3 x ~~ flip (^) 3 :t flip (^) 3 ^^^^ flip :: (a -> b -> c) -> b -> a -> c ^^^ (^) :: Integer -> Integer -> Integer ^ 3 :: Integer ^^^^^^^^ flip (^) :: Integer -> Integer -> Integer ^^^^^^^^^^ :: Integer -> Integer flip (^) 3 ~~ \x -> flip (^) 3 x ~~ \x -> (^) x 3 ~~ \x -> x ^ 3 \x -> [x] ~~ \x -> x : [] ~~ \x -> (: []) x ~~ (: []) :t (: []) :: a -> [a] :t \x -> (:) x [] ^^^ (:) :: a -> [a] -> [a] ^^ [] :: [c] ^ x :: b ^^^^^ (:) x :: [b] -> [b] -- subs a/b ^^^^^^^^ (:) x [] :: [c] -- subs b/c ^^^^^^^^^^^^^^ c -> [c] \x y -> x ^ y ~~ \x y -> (^) x y ~~ \x -> (^) x ~~ (^) \x -> 8 : x ~~ \x -> (8 :) x ~~ (8 :) ~~ \x -> (:) 8 x ~~ (:) 8 \x y -> y ^ x ~~ \x y -> (^) y x ~~ \x y -> flip (^) x y ~~ flip (^) compose :: (b -> c) -> (a -> b) -> a -> c compose f g x = f (g x) -- využití skládání funkcí k "vytažení" parametru do vnějšího výrazu \x -> "(" ++ x ++ ")" ~~ \x -> "(" ++ ( x ++ ")" ) ~~ \x -> "(" ++ ( (++ ")") x ) ~~ \x -> ("(" ++) ( (++ ")") x ) ^^^f^^^^ ^^^g^^^^ ^x ~~ \x -> ( ("(" ++) . (++ ")") ) x ~~ ( ("(" ++) . (++ ")") ) \x -> 0 < 35 - 3 * 2 ^ x ~~ \x -> 0 < (35 - (3 * (2 ^ x))) ~~ \x -> (0 <) ( (35 -) ( (3 *) ( (2 ^) x ) ) ) ^^f^^ ^^g^^ ^x ~~ \x -> (0 <) ( (35 -) ( ( (3*) . (2^) ) x ) ) ^^f^^^ ^^^^^^^g^^^^^^^ ^x ~~ \x -> (0 <) ( ( (35 -) . ( (3*) . (2^) ) ) x ) ~~ \x -> (0 <) ( ( (35 -) . (3*) . (2^) ) x ) ^^f^^ ^^^^^^^^^^^g^^^^^^^^^^^^ ^x ~~ \x -> ( (0 <) . (35 -) . (3 *) . (2 ^) ) x ~~ ( (0 <) . (35 -) . (3 *) . (2 ^) ) ~~ -} -- funkce curry a uncurry -- převádějí funkce na dvojicích -- na funkce umožnující částečnou aplikace a naopak curry' :: ( (a, b) -> c ) -> a -> b -> c curry' f x y = f (x, y) uncurry' :: (a -> b -> c) -> (a, b) -> c uncurry' f (x, y) = f x y distr :: (a -> b -> c) -> (a -> b) -> a -> c distr f g x = f x (g x) compose :: (b -> c) -> (a -> b) -> a -> c -- (.) compose f g x = f (g x) -- (f . g) x ~~ f (g x) -- distr (uncurry id) id x ~~ (uncurry id) x (id x) ~~ uncurry id x x ~~ (x, x) -- pair = uncurry (distr . ((.) (curry id))) -- pair (x, y) = uncurry (distr . ((.) (curry id))) (x, y) -- pair (x, y) = (distr . ((.) (curry id))) x y -- pair (x, y) = ((distr . ((.) (curry id))) x) y -- pair (x, y) = (distr (((.) (curry id)) x)) y -- pair (f, y) = (distr (curry id . f)) y -- pair (f, g) = distr (curry id . f) g -- pair (f, g) z = distr (curry id . f) g z -- pair (f, g) z = (curry id . f) z (g z) -- pair (f, g) z = ((curry id . f) z) (g z) -- pair (f, g) z = (curry id (f z)) (g z) -- pair (f, g) z = curry id (f z) (g z) -- pair (f, g) z = id ((f z), (g z)) pair (f, g) z = ((f z), (g z)) pair :: (a -> b, a -> c) -> a -> (b, c) -- negace predikátu negp :: (a -> Bool) -> a -> Bool negp f = not . f