[Haskell-cafe] pointfree-trouble

Daniel Fischer daniel.is.fischer at web.de
Tue Dec 22 09:40:04 EST 2009


Am Dienstag 22 Dezember 2009 15:09:34 schrieb slemi:
> hello everybody, i'm a newbie this is my first post here..
>
> i have trouble making a function pointfree:
>
> data RealFrac a => Matrix a = Matr [[a]] | Scalar a
>   deriving (Show, Eq)
>
> unMatr :: RealFrac a => Matrix a -> [[a]]
> unMatr = (\(Matr a) -> a)
>
> reMatr :: RealFrac a => ([[a]] -> [[a]]) -> (Matrix a -> Matrix a)
> reMatr a = Matr . (flip (.) unMatr) a
>
> this works fine, but if i leave the 'a' in the last function's definition
> like this:
> reMatr = Matr . (flip (.) unMatr)
> it gives an error. can anybody tell me why? (i'm using ghci)

You want

reMatr f = Matr . f . unMatr
    = (.) Matr (f . unMatr)
    = (.) Matr ((.) f unMatr)
    = (.) Matr (flip (.) unMatr f)
    = (.) Matr ((flip (.) unMatr) f)
    = (((.) Matr) . (flip (.) unMatr)) f

So

reMatr = ((.) Matr) . (flip (.) unMatr)

Or, as I prefer it,

reMatr = (Matr .) . (. unMatr)

The point is that g = flip (.) unMatr [or (. unMatr)] takes two arguments, the function f 
and a matrix m, to yield an argument fitting for Matr, so we need to apply one argument 
before we can compose it with Matr, hence we compose it with (compose with Matr) = (.) 
Matr = (Matr .):

(Matr .) . g


More information about the Haskell-Cafe mailing list