This file contains 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 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 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 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 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 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 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] |
This file contains 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 (+ 1) Nothing -- Nothing |
This file contains 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 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 :: [Int] | |
applicativeEx = [(+ 1)] <*> [1, 2, 3] -- [2, 3, 4] |