Created
November 15, 2017 22:15
-
-
Save reedrosenbluth/4a90cd0beb2c67caab996783d4856cc5 to your computer and use it in GitHub Desktop.
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
{-# OPTIONS -fwarn-incomplete-patterns #-} | |
-- return a Maybe value either containing the first number | |
-- over 20, or Nothing | |
over20 :: [Int] -> Maybe Int | |
over20 (h:tl) | |
| h > 20 = Just h | |
| otherwise = over20 tl | |
over20 [] = Nothing | |
-- how do we add two Maybe Ints? | |
maybePlus :: Maybe Int -> Maybe Int -> Maybe Int | |
maybePlus (Just a) (Just b) = Just (a + b) | |
maybePlus _ _ = Nothing | |
-- writing a wrapper function for every function we | |
-- want to apply to a Maybe value is annoying... | |
-- | |
-- Instead, we can use a higher order function to | |
-- generate these functions | |
-- use this like so: lift2 (+) (Just 1) (Just 2) | |
lift2 :: (a -> b -> c) -> Maybe a -> Maybe b -> Maybe c | |
lift2 f (Just a) (Just b) = Just (f a b) | |
lift2 _ _ _ = Nothing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment