List functions - "Under" operations
Tomasz Zielonka
t.zielonka at students.mimuw.edu.pl
Fri Jan 16 13:47:44 EST 2004
On Fri, Jan 16, 2004 at 12:01:42PM +0000, Ross Paterson wrote:
> > No need for so many new functions. Just write a function:
> >
> > composeFGxGy :: (b -> b -> c) -> (a -> b) -> a -> a -> c
> > composeFGxGy f g x y = f (g x) (g y)
> >
> > Then you can:
> >
> > sortFoos = sortBy (composeFGxGy compare f)
>
> The special case may be useful if f is expensive:
>
> -- sortImage f = sortBy (\x y -> compare (f x) (f y))
> sortImage :: Ord b => (a -> b) -> [a] -> [a]
> sortImage f xs = map snd (sortBy cmp_fst [(f x, x) | x <- xs])
> where cmp_fst (x,_) (y,_) = compare x y
Yes, you are right. But you are trading memory for speed, and in Haskell
more memory sometimes means less speed.
BTW. cmp_fst = composeFGxGy compare fst
Best regards,
Tom
--
.signature: Too many levels of symbolic links
More information about the Libraries
mailing list