Skip to content

Instantly share code, notes, and snippets.

@paveldedik
Created December 16, 2016 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paveldedik/fae1656027fa042b1f8bd418d3c78a72 to your computer and use it in GitHub Desktop.
Save paveldedik/fae1656027fa042b1f8bd418d3c78a72 to your computer and use it in GitHub Desktop.
From my talk: Haskell in 10 minutes
-- :` ./
-- ++:` ./oo
-- ++++:` ./oooo
-- ++++++:` ./oooooo
-- ++++++++:`./oooooooo
-- ++++++++ohhsoooooooo
-- ++++++oyddddhsoooooo
-- ++++oyddddddddhsoooo
-- ++oyddddddddddddhsoo
-- oyddddddddddddddddhs
--
-- Haskell
-- | od základů po monády za 10+ minut
-- Úvod
-- | deklarativní vs imperativním
-- | "Co" vs "Jak"
-- | SQL vs Python
-- 1. Základní funkce
-- | max, min, succ, ||, &&
-- | if then else
-- 2. Seznamy
-- | head, tail, init, last
-- | product, sum, maximum
-- 3. Generátory seznamů
-- | repeat, cycle, zip
-- 4. Typy
-- | Char, Bool, Integer
-- | :t factorial, :t (+), :t head
factorial :: Integer -> Integer
factorial 0 = 1
factorial n = product [1..n]
-- 5. Typové třídy
-- | Eq, Num, Ord, Functor, Monad
-- | něco jako rozhraní
-- | :t (==), :t fmap, :t (>>=)
class Eq a where
(==) :: a -> a -> Bool
(/=) :: a -> a -> Bool
instance Eq Integer where
x == y = x `integerEq` y
x /= y = not (x == y)
instance Eq JSON where
JNumber a == JNumber b = a == b
-- 6. Pattern Matching (Vzory)
-- | vzory u seznamů
factorial :: (Integral a) => a -> a
factorial 0 = 1
factorial n = n * factorial (n - 1)
head' :: [a] -> a
head' [] = error "Empty list, dummy!"
head' (x:xs) = x
-- 7. Guards, let in, case of
-- 8. Rekurze
-- | nejdůležitější část jazyka
maxim :: (Ord a) => [a] -> a
maxim [] = error "wtf?"
maxim [x] = x
maxim (x:xs) = max x (maxim xs)
reversed :: [a] -> [a]
reversed [] = []
reversed (x:xs) = (reversed xs) ++ [x]
-- 9. Currying
-- | Každá funkce v Haskellu bere
-- | oficiálně pouze jeden parametr.
-- | max 4 5 == (max 4) 5
-- 10. Skládání funkcí
-- | (.) :: (b -> c) -> (a -> b) -> a -> c
(snd.fst) ((1, 'b'), (2, 'c'))
((*2).(+5)) 5
-- 9. Funkce vyššího řádu
-- | tj. funkce, která bere jako argument
-- | další funkci
-- | map :: (a -> b) -> [a] -> [b]
-- 10. Maybe
-- | Just a, Nothing
-- | Podobné jak máme v imperativních jazycích
-- | funkce vracející NULL nebo None
f :: Int -> Maybe Int
f 0 = Nothing
f x = Just x
-- 11. Typová třída Functor
-- | něco, přes co se dá iterovat
-- | fmap
class Functor f where
fmap :: (a -> b) -> f a -> f b
instance Functor [] where
fmap = map
instance Functor Maybe where
fmap f (Just x) = Just (f x)
fmap f Nothing = Nothing
-- 12. IO
-- | Vypadá to, jakoby se pracovalo v
-- | imperativním režimu
-- | Pouze díky magii zvané monády
main = do
putStrLn "Hello, what's your name?"
name <- getLine
putStrLn ("Hey " ++ name ++ ", you rock!")
return name
-- 13. Monády
-- | "Monad is a monoid in the category
-- | of endofunctors"
foo :: Maybe Int
foo = do
x <- Just 3
y <- Just 2
return (x + y)
foo' :: Maybe Int
foo' = Just 3 >>= (
\x -> Just 2 >>= (\y -> Just (x + y)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment