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. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20080717/8470dd30/attachment.htm |
On Thu, Jul 17, 2008 at 11:34 AM, Rafael Gustavo da Cunha Pereira
Pinto <[hidden email]> 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. |
(Sorry, Rafael, the first e-mail I forgot to "reply all")
On Thu, Jul 17, 2008 at 11:42 AM, Felipe Lessa <[hidden email]> wrote: > specilized to (->) we get > > (***) :: (b -> c) -> (b -> c') -> (b -> (c, c')) That should read (&&&) :: (b -> c) -> (b -> c') -> (b -> (c, c')) :) -- Felipe. |
In reply to this post by Felipe Lessa
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: [hidden email] XMPP: [hidden email] IRC: [hidden email] 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 <[hidden email]> 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 > [hidden email] > http://www.haskell.org/mailman/listinfo/beginners > -- Rafael Gustavo da Cunha Pereira Pinto Electronic Engineer, MSc. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20080717/c0abca98/attachment-0001.htm |
Is there any good tutorial on arrows?
Thanks On Thu, Jul 17, 2008 at 13:08, Rafael Gustavo da Cunha Pereira Pinto < [hidden email]> 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 <[hidden email]> > 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 >> [hidden email] >> 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. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20080717/efaaedc4/attachment.htm |
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 |
In reply to this post by Rafael Gustavo da Cunha Pereira Pinto
2008/7/17 Rafael Gustavo da Cunha Pereira Pinto <[hidden email]>:
> 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? |
In reply to this post by Heinrich Apfelmus
>> Is there any good tutorial on arrows?
> Ross Paterson. Arrows and Computation. > http://www.soi.city.ac.uk/~ross/papers/fop.html<http://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.html<http://www.soi.city.ac.uk/%7Eross/papers/fop.html> > > to be pretty good. > > > Regards, > apfelmus > > > _______________________________________________ > Beginners mailing list > [hidden email] > http://www.haskell.org/mailman/listinfo/beginners > An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20080718/6e328179/attachment-0001.htm |
Free forum by Nabble | Edit this page |