[Haskell-beginners] points-free problem
Daniel Fischer
daniel.is.fischer at web.de
Fri Nov 20 17:41:53 EST 2009
Am Freitag 20 November 2009 23:22:08 schrieb I. J. Kennedy:
> I know I'm going to kick myself when the answer is revealed by one of
> you kind folks, but after staring at this a while I just can't figure
> out what's going on. The compiler (ghci) is trying so hard to tell me
> what's wrong, but I am failing to understand. I thought for the most
> part one could take a function definition like
>
> f x = (blah blah blah) x
>
> and turn it into points-free style by
>
> f = (blah blah blah)
>
> But from the dialog below, my assumption is incorrect.
> Help?
>
> I. J. Kennedy
Monomorphism restriction.
By that, if you bind f via
f = (blah blah blah)
and don't give a type signature, f gets a monomorphic type. Dreadful details in the
report, section 4.5.(?)
Put ":set -XNoMonomorphismRestriction" in your .ghci file.
>
> > sortBy (compare `on` fst) [(2,3),(0,1),(1,5)] -- test a little sort
> > expression
>
> [(0,1),(1,5),(2,3)]
>
> > let sortpairs xss = sortBy (compare `on` fst) xss -- make it into a
> > function
> >
> > :t sortpairs
>
> sortpairs :: (Ord a) => [(a, b)] -> [(a, b)]
>
> > sortpairs [(2,3),(0,1),(1,5)]
>
> [(0,1),(1,5),(2,3)]
>
> > let sortpairs = sortBy (compare `on` fst) -- points-free style
> > sortpairs [(2,3),(0,1),(1,5)]
>
> <interactive>:1:24:
> No instance for (Num ())
> arising from the literal `1' at <interactive>:1:24
> Possible fix: add an instance declaration for (Num ())
> In the expression: 1
> In the expression: (1, 5)
> In the first argument of `sortpairs', namely
> `[(2, 3), (0, 1), (1, 5)]'
>
> > :t sortpairs
>
> sortpairs :: [((), b)] -> [((), b)] -- what?!
More information about the Beginners
mailing list