There are a lot of functional idioms: monads, applicatives, arrows, etc. They are documented in different articles but unfortunately I don't know any book or article where they're summarized in one place (there is Typeclassopedia but it has a lot of areas that aren't covered well). Can anyone recommend an article/book which covers them well in one place and which can be accessible to a programmer with intermediate skills in FP?

- 52,967
- 18
- 114
- 136

- 10,252
- 8
- 58
- 88
-
It doesn't directly answer your question but this other question has some interesting info (and at least a link or two): http://stackoverflow.com/questions/327955/does-functional-programming-replace-gof-design-patterns – reuben Jul 05 '12 at 07:29
-
15Could you expand on which areas you feel the Typeclassopedia doesn't cover well? – dave4420 Jul 05 '12 at 07:30
-
And also http://programmers.stackexchange.com/questions/89273/where-are-all-the-functional-programming-design-patterns – reuben Jul 05 '12 at 07:30
-
I'd also love to hear of an introductory book covering such more advanced functional programming idioms. – Malte Schwerhoff Jul 05 '12 at 07:59
-
2@dave4420 If I read typeclassopedia from start to end, the more I read, the less I understand. First sections are really good but latter sections are hardly readable for me. – Konstantin Solomatov Jul 05 '12 at 08:19
-
3@KonstantinSolomatov it might be that you'll need to research the sections you don't understand, look at blog posts, and look hard at the code. all of these "design patterns" in the typeclassopedia are really abstractions, and often deep ones, that take a while to sink in sometimes. – jberryman Jul 05 '12 at 13:56
-
See also [Functional programming equivalent of design patterns book?](http://stackoverflow.com/questions/2930277/functional-programming-equivalent-of-design-patterns-book) and also [Design patterns for functional oo hybrid languages?](http://stackoverflow.com/questions/3084546/design-patterns-for-functional-oo-hybrid-languages) – Dan Burton Jul 05 '12 at 19:36
-
@jberryman in fairness, Arrows are not used nearly as much as the MonadFoo classes. Most Haskell "design patterns" revolve around first class functions, and the Functor, Applicative, and Monad typeclasses. – Dan Burton Jul 05 '12 at 19:41
-
1@DanBurton not sure if you misread my comment, but I'd categorize all of those as deep abstractions, although it's easy to forget that when you've had your "Ah ha!" moment. – jberryman Jul 06 '12 at 03:18
5 Answers
My suggestion is, if you want to learn Scala, to read the book from Paul Chiusano and Runar Bjarnason:
Part II: Functional design and combinator libraries
- Making little languages
- JSON serialization
- Specification-based testing
- Parsers
- Purely functional parallelism
- Purely functional state
Part III: Functional design patterns
- The case for abstraction
- Monoids
- Functors
- Monads
- Applicative functors
- Traversable and foldable data structures
- Comonads
Part IV: Breaking the rules: effects and I/O
- Effects vs. side-effects
- Stream processing and incremental I/O
- Enforcing effect-scoping with the type system

- 2,116
- 2
- 21
- 39

- 19,559
- 13
- 62
- 115
I'm sorry I don't know of articles or books which cover in detail the different usages for all of those constructs, but I can give you a few links to individual resources.
A quite common pattern is to build monad transformers instead of simple monads (see also the link in the next paragraph). It basically means you build something that must be combined with other monads, resulting in a more complex one able to handle features of both of them.
In Real World Haskell there are a few chapters about monads. In Chapter 14. Monads the authors explain the basics and some common usages (maybe, list, state). Chapter 15. Programming with monads provides more explanations about how to effectively use them (it covers the reader monad as well). The following chapter explains how to use Parsec, but it may be more interesting to search for articles covering how it actually works: it should be a really good example of a well-organized use of monads for parsing. Fianlly, Chapter 18. Monad transformers introduces how monad transformers work and then shows how to build one, step by step. The considerations towards the final sections of the chapter are also interesting.
I read once a really interesting question on SO about creative uses of monads. The proposed links were awesome reads about the topic. With that spirit, I tried to ask the same for arrows: I definitely got less answers than the one on monads, but interesting ones nevertheless.
With respect to OOP patterns by the gang of four, there is a nice set of 3 articles by IBM about the topic in their series Functional thinking. The target functional language is Scala. They proceed by explaining usual design patterns in OOP and showing how they map into Scala.
- Functional thinking: Functional design patterns, Part 1. Here they cover factories, template methods, strategy, flyweight. The bottom line is that by having functions as first class values, everything is much simpler.
- Functional thinking: Functional design patterns, Part 2. This is about java and groovy. It adresses the adapter pattern.
- Functional thinking: Functional design patterns, Part 3. Here they talk about the interpreter pattern. Again, the target language is groovy.
The most relevant article w.r.t. your question is for sure the first one, but the other two may be interesting related readings nevertheless.

- 1
- 1

- 8,907
- 5
- 38
- 78
-
Thanks, but I mean different kinds of patterns, like Monad, Arrow, Applicative no GoF patterns. – Konstantin Solomatov Jul 05 '12 at 08:18
-
@KonstantinSolomatov: I misunderstood your question, sorry. I added some references for monads and arrows. – Riccardo T. Jul 05 '12 at 08:32
Jeremy Gibbons has a Patterns in FP blog which is destined eventually to become pretty much the book you're asking for. Of course, that's not yet in a condition to be as useful as you might want just now, but he deserves some encouragement!
Meanwhile, I'll say +1 for Brent Yorgey's Typeclassopedia. It's really useful, and if there are later parts which confuse, this site is a good place to get to the bottom of them. I know Brent keeps it under review. If he's not reaching his readers, give him some help.

- 43,025
- 18
- 121
- 214
Lot of FP stuff are published on Oleg's site: http://okmij.org/ftp/
Presentation about FP patterns from Josh Suereth: http://jsuereth.com/intro-to-fp/

- 7,883
- 2
- 44
- 68
Have you read the later chapters of Learn You a Haskell for Great Good?
Chapter 6 covers maps and folds, which are two of the more important "design patterns" in functional languages.
Chapters 11-13 cover Functors, Applicative Functors and Monads, in that order. This is helpful - many tutorials introduce Functors and then Monads, and then tack Applicative Functors on at the end (if they cover it at all). The order in LYAH is better, because moving from Functors => Applicative Functors => Monads moves you gradually up the ladder of generality and power.
Chapter 14 covers Zippers - you can effectively think of these as container classes with a pointer to a specific piece of data, which mean you get O(1) access and update at the pointer location.
It doesn't cover Arrows or Comonads, which are a more advanced topic in Haskell. To understand how and why to use Arrows or Comonads you should definitely have a firm grasp of Monads already, so I don't think this is a problem - LYAH is firmly aimed at the beginner end of the Haskell market.

- 46,912
- 15
- 110
- 154
-
1Yes, I read the book. I liked it very much. Unfortunately it doesn't covert arrows and many other advanced patterns. I also want to read more about Monads and Applicatives (for example, LYHGG doesn't cover continuation monad). – Konstantin Solomatov Jul 05 '12 at 08:32