[Haskell-beginners] Folding an applicative functor, wait, .. what?

Patrick LeBoutillier patrick.leboutillier at gmail.com
Sat Jan 8 18:58:03 CET 2011


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#applicative-functors

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 <celtic at sairyx.org> 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 at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>



-- 
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada



More information about the Beginners mailing list