Funkcionálne programovanie

substractInputs :: Int
substractInputs = getInt - getInt
where
getInt :: Int
getInt = read (getLine)
Bude to fungovať?
const action1 = putStrLn("Hello,");
const action2 = putStrLn("world!");
const main = () => return [action1, action2];
const action1 = getLine();
const action2 = putStrLn("Hello, ...!");
const main = () => return [action1, action2];
const action1 = getLine();
const action2 = (name) => putStrLn(`Hello, ${name}!`);
const main = () => return [action1, action2];
const action1 = getLine();
const action2 = (name) => putStrLn(`Hello, ${name}!`);
const main = () => action1.then(action2);
const action1 = getLine();
const action2 = getLine();
const action3 = (x, y) => {
const result = parseInt(x) - parseInt(y);
return putStrLn(`Result: ${result}`);
};
const main = () => action1.then(
(x) => action2.then((y) => action3(x, y)));
Namiesto vykonania operácie, vrátime ju ako výsledok.
main :: IO ()
main = putStrLn "Hello, world!"
IO aA value of type
IO ais a computation which, when performed, does some I/O before returning a value of typea.There is really only one way to “perform” an I/O action: bind it to
Main.mainin your program. When your program is run, the I/O will be performed.
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 -> (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 aIO 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()>>= — then()return — Promise.resolve()return a >>= h = h am >>= 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