[Haskell-cafe] Function composition
roma at ro-che.info
Sat Dec 27 04:40:43 EST 2008
* Roman Cheplyaka <roma at ro-che.info> [2008-12-27 11:33:22+0200]
> * Oscar Picasso <oscarpicasso at gmail.com> [2008-12-26 22:37:26-0500]
> > Hi,
> > I can write:
> > *Main> let yes = not . not
> > *Main> :t yes
> > yes :: Bool -> Bool
> > But not:
> > *Main> let isNotEqual = not . (==)
> > <interactive>:1:23:
> > Couldn't match expected type `Bool'
> > against inferred type `a -> Bool'
> > Probable cause: `==' is applied to too few arguments
> > In the second argument of `(.)', namely `(==)'
> > In the expression: not . (==)
> > Why?
> You might want to read about currying. This will explain why (==)
> does not take a pair of values, it rather takes one value and then
> another, and that's why it is not composable in the way you want.
> What you're trying to do is easier to do with uncurried functions:
> Prelude> let isNotEqual = not . uncurry (==)
> Prelude> :t isNotEqual
> isNotEqual :: (Eq a) => (a, a) -> Bool
> Prelude> isNotEqual (3,4)
> Prelude> isNotEqual (3,3)
> (note that -XNoMonomorphismRestriction is used here)
> 1. http://www.haskell.org/haskellwiki/Currying
isNotEqual = curry $ not . uncurry (==)
is curried version, which has type
isNotEqual :: (Eq b) => b -> b -> Bool
Roman I. Cheplyaka :: http://ro-che.info/
"Don't let school get in the way of your education." - Mark Twain
More information about the Haskell-Cafe