Funkcionálne programovanie
substractInputs :: Int
substractInputs = getInt - getInt
where
getInt :: Int
getInt = read (getLine)
Bude to fungovať?
Namiesto vykonánia operácie, vrátime ju ako výsledok.
main :: IO ()
main = putStrLn "Hello, world!"
IO a
putChar :: Char -> IO ()
putStr :: String -> IO ()
putStrLn :: String -> IO ()
print :: Show a => a -> IO ()
()
()
getChar :: IO Char
getLine :: IO String
getContents :: IO String
main :: IO ()
main = putStr (histogram values)
(>>) :: IO a -> IO b -> IO b
main = putStrLn "Hello, what is your name?"
>> getLine
>> putStrLn ("Hello, world!")
(>>) :: IO a -> IO b -> IO b
main = putStrLn "Hello, what is your name?"
>> getLine
>> putStrLn ("Hello, world!")
(>>=) :: IO a -> (a -> IO b) -> IO b
main = putStrLn "Hello, what is your name?"
>> getLine
>>= \name -> putStrLn ("Hello, " ++ name ++ "!")
return :: a -> IO a
substractInputs :: IO Int
substractInputs =
getInt
>>= \ x -> getInt
>>= \ y -> return (x - y)
where
getInt :: IO Int
getInt = getLine >>= \ line -> return (read line)
do
notáciasubstractInputs :: IO Int
substractInputs = do
x <- getInt
y <- getInt
return (x - y)
where
getInt :: IO Int
getInt = getLine >>= return . read
main = putStrLn "Hello, what is your name?"
>> getLine
>>= \name -> putStrLn ("Hello, " ++ name ++ "!")
main = do
putStrLn "Hello, what is your name?"
name <- getLine
putStrLn ("Hello, " ++ name ++ "!")
main = putStrLn "Hello, what is your name?"
>> getLine
>>= \name -> putStrLn ("Hello, " ++ name ++ "!")
main = do {
putStrLn "Hello, what is your name?";
name <- getLine;
putStrLn ("Hello, " ++ name ++ "!")
}
IO a
IO a
Monáda je monoid v kategórii endofunktorov
studentStatus :: String -> String
studentStatus name = lookup status statusNames
where
status = lookup id statuses
id = lookup name studentIDs
Problém: Maybe
studentStatus :: String -> Maybe String
studentStatus name =
case lookup name studentIDs of
Just id -> case lookup id statuses of
Just status -> lookup status statusNames
Nothing -> Nothing
Nothing -> Nothing
>>=
>>=
studentStatus :: String -> Maybe String
studentStatus name =
lookup name studentIDs
>>= \ id -> lookup id statuses
>>= \ status -> lookup readableStatus status
studentStatus :: String -> Maybe String
studentStatus name = do
id <- lookup name studentIDs
status <- lookup id statuses
lookup readableStatus status
IO
je iba špeciálny prípad Monádyclass Monad m where
(>>=) :: m a -> (a -> m b) -> m b
(>>) :: m a -> m b -> m b
return :: a -> m a
instance Monad Maybe where
(Just x) >>= k = k x
Nothing >>= _ = Nothing
return = Just
instance Monad (Either e) where
Left l >>= _ = Left l
Right r >>= k = k r
return = Right
Right
— výsledokLeft
— správa o chybeinstance Monad [] where
xs >>= f = concat (map f xs)
return x = [x]
;
>>=
— flatMap()
return
— of()
return a >>= h
= h a
m >>= return
= m
(m >>= g) >>= h
= m >>= (\x -> g x >>= h)
square 2
4
map square [2]
[4]
fmap square [2]
➜ [4]
fmap square (Just 2)
➜ Just 4
fmap square Nothing
➜ Nothing
class Functor f where
fmap :: (a -> b) -> f a -> f b
(<$) :: a -> f b -> f a
(<$>) = fmap
instance Functor ((->) r) where
fmap f g = f . g
let squareDouble = fmap square (*2)
squareDouble 5
➜ 100
Just square <*> Just 2
➜ Just 4
[(*2), (+3)] <*> [1, 2, 3]
➜ [2, 4, 6, 4, 5, 6]
fmap
, <$>
) aplikuje funkciu na zabalenú hodnotu<*>
) aplikuje zabalenú funkciu na zabalenú hodnotu>>=
) aplikuje funkciu produkujúcu zabalenú hodnotu na zabalenú hodnotu