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

Arlen Cuss celtic at sairyx.org
Sat Jan 8 13:45:17 CET 2011


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110108/481e9d84/attachment.pgp>


More information about the Beginners mailing list