This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| withApplicative :: Int -> String -> Maybe String | |
| withApplicative age name = greet <$> (Just name) <*> validateAge age |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| validateAge :: Int -> Maybe Int | |
| validateAge age | |
| | age >= 18 = return age | |
| | otherwise = Nothing | |
| greet :: String -> String | |
| greet name = "Hello " ++ name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| divide :: Float -> Float -> Maybe Float | |
| divide _ 0 = Nothing | |
| divide x y = return (x / y) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| plusOne :: Int -> Maybe Int | |
| plusOne x = return (x + 1) | |
| monadEx :: Maybe Int | |
| monadEx = Just 42 >>= plusOne -- Just 43 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| alt2 :: Int -> Maybe Int | |
| alt2 41 = Nothing | |
| alt2 x = return (x + 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| plusOne :: Int -> Maybe Int | |
| plusOne x = return (x + 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| plusOne :: Int -> Maybe Int | |
| plusOne x = Just (x + 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| applicativeEx :: Maybe Int | |
| applicativeEx = (+ 1) <*> Just 42 -- Just 43 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fmapEx :: Maybe Int | |
| fmapEx = fmap (Just (+ 1)) (Just 42) -- Does not compile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fmapEx :: [Int] -- A function returning a list | |
| fmapEx = fmap (+ 1) [1, 2, 3] -- [2, 3, 4] |