
I am trying to write a toy function to sum all members of a list but the biggest. This list is entirely composed by integers bigger than 0. I have: f::[Integer]->Integer f a = x a - y a where x=sum y=foldr max 0 Is there any way to implement that using composition, so I have a point-free implementation? -- Rafael Gustavo da Cunha Pereira Pinto Electronic Engineer, MSc.

On Thu, Jul 17, 2008 at 11:34 AM, Rafael Gustavo da Cunha Pereira
Pinto
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.

Hi. Em Qui, 2008-07-17 às 11:42 -0300, Felipe Lessa escreveu:
f = sum &&& foldr max 0 >>> uncurry (-)
You can replace
foldr max 0 with simply foldr1 max or even maximum So you get: f = sum &&& maximum >>> uncurry (-)
Greetings. -- Marco Túlio Gontijo e Silva Página: http://marcotmarcot.googlepages.com/ Blog: http://marcotmarcot.blogspot.com/ Correio: marcot@riseup.net XMPP: marcot@jabber.org IRC: marcot@irc.freenode.net Telefone: 25151920 Celular: 98116720 Endereço: Rua Turfa, 639/701 Prado 30410-370 Belo Horizonte/MG Brasil

Marco Túlio Gontijo e Silva wrote:
Hi.
Em Qui, 2008-07-17 às 11:42 -0300, Felipe Lessa escreveu:
f = sum &&& foldr max 0 >>> uncurry (-)
You can replace
foldr max 0 with simply foldr1 max or even maximum
If it's important that the function return 0 when given an empty list, then you don't want to use foldr1 or maximum. But, if you don't need it to, then it may be sounder to have it fail on an empty list, because it's not particularly well-defined what it should return then. -Isaac

I will use foldr max 0, because I want 0 for the empty list.
On Thu, Jul 17, 2008 at 12:09, Isaac Dupree
Marco Túlio Gontijo e Silva wrote:
Hi.
Em Qui, 2008-07-17 às 11:42 -0300, Felipe Lessa escreveu:
f = sum &&& foldr max 0 >>> uncurry (-)
You can replace
foldr max 0
with simply
foldr1 max
or even
maximum
If it's important that the function return 0 when given an empty list, then you don't want to use foldr1 or maximum. But, if you don't need it to, then it may be sounder to have it fail on an empty list, because it's not particularly well-defined what it should return then.
-Isaac
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Rafael Gustavo da Cunha Pereira Pinto Electronic Engineer, MSc.

Is there any good tutorial on arrows? Thanks On Thu, Jul 17, 2008 at 13:08, Rafael Gustavo da Cunha Pereira Pinto < rafaelgcpp@gmail.com> wrote:
I will use foldr max 0, because I want 0 for the empty list.
On Thu, Jul 17, 2008 at 12:09, Isaac Dupree
wrote: Marco Túlio Gontijo e Silva wrote:
Hi.
Em Qui, 2008-07-17 às 11:42 -0300, Felipe Lessa escreveu:
f = sum &&& foldr max 0 >>> uncurry (-)
You can replace
foldr max 0
with simply
foldr1 max
or even
maximum
If it's important that the function return 0 when given an empty list, then you don't want to use foldr1 or maximum. But, if you don't need it to, then it may be sounder to have it fail on an empty list, because it's not particularly well-defined what it should return then.
-Isaac
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Rafael Gustavo da Cunha Pereira Pinto Electronic Engineer, MSc.
-- Rafael Gustavo da Cunha Pereira Pinto Electronic Engineer, MSc.

Rafael Gustavo da Cunha Pereira Pinto wrote:
Is there any good tutorial on arrows?
I found Ross Paterson. Arrows and Computation. http://www.soi.city.ac.uk/~ross/papers/fop.html to be pretty good. Regards, apfelmus

Is there any good tutorial on arrows? Ross Paterson. Arrows and Computation. http://www.soi.city.ac.uk/~ross/papers/fop.htmlhttp://www.soi.city.ac.uk/%7Eross/papers/fop.html
If you're new to Haskell, I'm guessing you're looking at the instance of Arrow for ordinary functions, so that you can write in the point-free style. instance Arrow (->) where arr f = f f >>> g = g . f first f = f *** id second f = id *** f (***) f g ~(x,y) = (f x, g y) If so, Ross Paterson's paper may scare you off. I'm not aware of any good tutorial for ordinary functions - maybe because using arrows this way is sort of a point-free programming gimmick. Anyway, here's my attempt at a quick tutorial of the Arrow combinators for ordinary functions. -- Pass input to one function, then its output to another (f >>> g) x == g (f x) -- Pass two inputs to two functions (f *** g) ~(x,y) == (f x, g y) -- Pass one input to both functions (f &&& g) x == (f x, g x) -- Apply function to only first of two inputs (first f) (x,y) == (f x, y) -- Apply function to only second of two inputs (second f) (x,y) == (x, f y) The pictures on this page are helpful: http://www.haskell.org/arrows/ Arrows are more interesting when composing more abstract things where it doesn't make sense to give the user access to inputs and outputs, such as modeling circuits where the inputs are wires. Lots of good examples in the paper apfelmus recommended. -Greg On Fri, Jul 18, 2008 at 1:33 AM, apfelmus wrote:
Rafael Gustavo da Cunha Pereira Pinto wrote:
Is there any good tutorial on arrows?
I found
Ross Paterson. Arrows and Computation. http://www.soi.city.ac.uk/~ross/papers/fop.htmlhttp://www.soi.city.ac.uk/%7Eross/papers/fop.html
to be pretty good.
Regards, apfelmus
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

2008/7/17 Rafael Gustavo da Cunha Pereira Pinto
I will use foldr max 0, because I want 0 for the empty list.
In this case use rather "foldl' max 0", using foldr you would have to construct a huge thunk of max application before getting your result whereas foldl' max will find the answer in constant space (and quite a bit faster). This wiki page will explain the difference between the folds : http://www.haskell.org/haskellwiki/Foldr_Foldl_Foldl%27 -- Jedaï
participants (8)
-
apfelmus
-
Chaddaï Fouché
-
Felipe Lessa
-
Greg Fitzgerald
-
Isaac Dupree
-
Marco Túlio Gontijo e Silva
-
Rafael Gustavo da Cunha Pereira Pinto
-
Rafael Gustavo da Cunha Pereira Pinto