[Haskell-cafe] pair (f,g) x = (f x, g x)?
Stefan Holdermans
stefan at cs.uu.nl
Sat Jul 2 15:45:38 EDT 2005
Wenduan,
> you get a Int and Char out of the two composed functions, namely
> square.fst, Char.toUpper.snd.But in the type declaration of
> pair, which appeared to me,it meant its arguments must be two
> functions which are of the same type namely a,whereas Int and
> Char passed to as arguments are of different types here, and that's
> the reason I thought it wouldn't work.
Well, actually the two argument functions are not required to be of
exactly the same type. The only restriction is that the types of their
parameters match:
pair :: (a -> b) -> (a -> c) -> a -> (b, c)
So, in
pair (square . fst) (toUpper . snd)
a matches the type of the parameters of (square . fst) and (toUpper .
snd), i.e., (Int, Char), b matches the result type of (square . fst),
i.e., Int, and c matches the result type of (toUpper . snd), i.e.,
Char; so the type of pair get instantiated with
((Int, Char) -> Int) -> ((Int, Char) -> Char) -> (Int, Char) -> (Int,
Char)
You might also want to use
(***) :: (a -> c) -> (b -> d) -> (a, b) -> (c, d)
(f *** g) (a, b) = (f a, g b)
HTH,
Stefan
More information about the Haskell-Cafe
mailing list