Skip to content

Instantly share code, notes, and snippets.

@diegs
Created May 29, 2014 17:03
Show Gist options
  • Select an option

  • Save diegs/28213590135032854cfb to your computer and use it in GitHub Desktop.

Select an option

Save diegs/28213590135032854cfb to your computer and use it in GitHub Desktop.
test1 :: String -> Maybe Integer
test1 "a" = Just 1
test1 _ = Nothing
test2 :: Integer -> Integer
test2 = (1+)
-- does not type check!
-- Calc.hs:63:9:
-- Couldn't match type `Integer' with `Maybe Integer'
-- Expected type: String -> Maybe Integer
-- Actual type: String -> Integer
-- In the expression: test2 <$> test1
-- In an equation for `test3': test3 = test2 <$> test1
-- Calc.hs:63:19:
-- Couldn't match type `Maybe Integer' with `Integer'
-- Expected type: String -> Integer
-- Actual type: String -> Maybe Integer
-- In the second argument of `(<$>)', namely `test1'
-- In the expression: test2 <$> test1
-- In an equation for `test3': test3 = test2 <$> test1
test3 :: String -> Maybe Integer
test3 = test2 <$> test1
-- type checks
test3' :: String -> Maybe Integer
test3' str = test2 <$> (test1 str)
-- also type checks
test3'' :: String -> Maybe Integer
test3'' = fmap test2 . test1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment