
16 Jan
2004
16 Jan
'04
7:47 a.m.
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