Folding an applicative functor, wait,.. what?

Hi all, I'm probably in serious trouble here (i.e. I'm toying with things I don't understand); I have some code that I was using like follows, context removed for clarity: anotherFunc :: Int -> String ... map ((++) <$> show <*> anotherFunc) [1..20] This works great - the integers 1..20 are passed to each function and the results are catenated together. What if I'd like to add a third function? So far, the only workable solution I've been able to come up with is this: map ((++) <$> ((++) <$> show <*> anotherFunc) <*> yetAnotherFunc) [1..20] That's one unfortunate expression. I've been messing around with concat (trying to get the results of the functions into a list when all is well and done for concat to finally string together?), but all in all, have not had much luck with seeding the values in that way. I think there is probably an obvious solution I've missed... and hold on a minute! map (mconcat [show, anotherFunc, yetAnotherFunc]) [1..20] I just stumbled upon this. While I'm not 100% confident on what's just happened here, I thought I'd mail this out for everyone to enjoy the learning process that has been this email. This serves as a helpful pointer for me to study both monoids and applicative functors in depth, as the former suddenly leapt out of the recesses of my mind somewhere and solved it. Thanks for listening, and nice to meet you all! Cheers, Arlen

On Sat, Jan 8, 2011 at 10:45 AM, Arlen Cuss
I think there is probably an obvious solution I've missed... and hold on a minute!
map (mconcat [show, anotherFunc, yetAnotherFunc]) [1..20]
I just stumbled upon this. While I'm not 100% confident on what's just happened here, I thought I'd mail this out for everyone to enjoy the learning process that has been this email.
That's the Monoid b => Monoid (a->b) instance [1], but I don't think it is anywhere near obvious =). Cheers! [1] http://hackage.haskell.org/packages/archive/base/4.3.0.0/doc/html/src/Data-M...) -- Felipe.

On Sat, 2011-01-08 at 10:53 -0200, Felipe Almeida Lessa wrote:
On Sat, Jan 8, 2011 at 10:45 AM, Arlen Cuss
wrote: I think there is probably an obvious solution I've missed... and hold on a minute!
map (mconcat [show, anotherFunc, yetAnotherFunc]) [1..20]
I just stumbled upon this. While I'm not 100% confident on what's just happened here, I thought I'd mail this out for everyone to enjoy the learning process that has been this email.
That's the Monoid b => Monoid (a->b) instance [1], but I don't think it is anywhere near obvious =).
Right you are! I know what's going on now :-) Cheers!
Cheers!
[1] http://hackage.haskell.org/packages/archive/base/4.3.0.0/doc/html/src/Data-M...)
Arlen

Hi,
Initially I didn't even understand how your code worked, but I found a
small tutorial un this kind of stuff here:
http://learnyouahaskell.com/functors-applicative-functors-and-monoids#applic...
Just search for the phrase "Another instance of Applicative is (->) r,
so functions" and you will see examples similar to yours.
I can't wait for the "Learn You a Haskell" Book!
Patrick
On Sat, Jan 8, 2011 at 7:45 AM, Arlen Cuss
Hi all,
I'm probably in serious trouble here (i.e. I'm toying with things I don't understand); I have some code that I was using like follows, context removed for clarity:
anotherFunc :: Int -> String ... map ((++) <$> show <*> anotherFunc) [1..20]
This works great - the integers 1..20 are passed to each function and the results are catenated together.
What if I'd like to add a third function? So far, the only workable solution I've been able to come up with is this:
map ((++) <$> ((++) <$> show <*> anotherFunc) <*> yetAnotherFunc) [1..20]
That's one unfortunate expression. I've been messing around with concat (trying to get the results of the functions into a list when all is well and done for concat to finally string together?), but all in all, have not had much luck with seeding the values in that way.
I think there is probably an obvious solution I've missed... and hold on a minute!
map (mconcat [show, anotherFunc, yetAnotherFunc]) [1..20]
I just stumbled upon this. While I'm not 100% confident on what's just happened here, I thought I'd mail this out for everyone to enjoy the learning process that has been this email.
This serves as a helpful pointer for me to study both monoids and applicative functors in depth, as the former suddenly leapt out of the recesses of my mind somewhere and solved it.
Thanks for listening, and nice to meet you all!
Cheers, Arlen
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- ===================== Patrick LeBoutillier Rosemère, Québec, Canada

Hi,
Initially I didn't even understand how your code worked, but I found a small tutorial un this kind of stuff here:
http://learnyouahaskell.com/functors-applicative-functors-and-monoids#applic...
Just search for the phrase "Another instance of Applicative is (->) r, so functions" and you will see examples similar to yours.
This is actually where I've been learning Haskell from; these parts of the book sort of half-sunk in, but now that I'm actually trying to use them it's sinking in better ;-)
I can't wait for the "Learn You a Haskell" Book!
Would be great!
Patrick
Cheers, Arlen
On Sat, Jan 8, 2011 at 7:45 AM, Arlen Cuss
wrote: Hi all,
I'm probably in serious trouble here (i.e. I'm toying with things I don't understand); I have some code that I was using like follows, context removed for clarity:
anotherFunc :: Int -> String ... map ((++) <$> show <*> anotherFunc) [1..20]
This works great - the integers 1..20 are passed to each function and the results are catenated together.
What if I'd like to add a third function? So far, the only workable solution I've been able to come up with is this:
map ((++) <$> ((++) <$> show <*> anotherFunc) <*> yetAnotherFunc) [1..20]
That's one unfortunate expression. I've been messing around with concat (trying to get the results of the functions into a list when all is well and done for concat to finally string together?), but all in all, have not had much luck with seeding the values in that way.
I think there is probably an obvious solution I've missed... and hold on a minute!
map (mconcat [show, anotherFunc, yetAnotherFunc]) [1..20]
I just stumbled upon this. While I'm not 100% confident on what's just happened here, I thought I'd mail this out for everyone to enjoy the learning process that has been this email.
This serves as a helpful pointer for me to study both monoids and applicative functors in depth, as the former suddenly leapt out of the recesses of my mind somewhere and solved it.
Thanks for listening, and nice to meet you all!
Cheers, Arlen
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Arlen,
I can't wait for the "Learn You a Haskell" Book!
Would be great!
Look here, it's coming: http://www.amazon.com/Learn-You-Haskell-Great-Good/dp/1593272839/ref=sr_1_1?ie=UTF8&s=books&qid=1294533476&sr=8-1 -- ===================== Patrick LeBoutillier Rosemère, Québec, Canada
participants (3)
-
Arlen Cuss
-
Felipe Almeida Lessa
-
Patrick LeBoutillier