For starters...

Felipe Lessa felipe.lessa at gmail.com
Thu Jul 17 10:42:26 EDT 2008


On Thu, Jul 17, 2008 at 11:34 AM, Rafael Gustavo da Cunha Pereira
Pinto <RafaelGCPP.Linux at gmail.com> wrote:
> f::[Integer]->Integer
> f a = x a - y a where
>                  x=sum
>                  y=foldr max 0

Using double f g x = (f x, g x),

f a = x a - y a
f a = uncurry (-) (x a, y a)
f a = uncurry (-) (double x y a)
f = uncurry (-) . double sum (foldr max 0)

Now, note that, from Control.Arrow,

  (&&&) :: Arrow a => a b c -> a b c' -> a b (c, c')

and that (->) is an instance of Arrow, so that when (&&&) is
specilized to (->) we get

(***) :: (b -> c) -> (b -> c') -> (b -> (c, c'))

exactly the type of double! (verify that)

So, we may write your function as

f = uncurry (-) . (&&&) sum (foldr max 0)
f = sum &&& foldr max 0 >>> uncurry (-)


------
Final (point-free) code:

import Control.Arrow
f = sum &&& foldr max 0 >>> uncurry (-)

-- 
Felipe.


More information about the Beginners mailing list