Is there exists any? Isn’t functional languages expressive enough to don’t bother with patterns? I won’t give another rant about that topic. Interested people i encourage to look very fun and insightful presentation Functional Design Patterns by Aino Corry on InfoQ.
There are many very useful concepts, ideas techniques (choose your best matching word 🙂 ) in functional programming. I am not an expert, and I don’t know many functional languages (i have played a little with Scheme and used commercially Erlang for a few months) – so i may change my mind about it. But currently I think that there are general enough, sometimes not obvious enough and reoccurring patterns in functional style of programming.
Functional programming is all about defectively using higher order functions, ignoring concept of shared state, doing cool things with lists and using recursion (always tail of course). So this determine basic areas of patterns:
- recursion patterns (Roundabout – pattern language for recursion, proposed by Eugene Walfingford),
- techniques to share state for a couple of functions mainly using closures and similar OOP-like techniques (Envoy – pattern language proposed by Eugene Wilingford) but interesting technique is mentioned in chapter 12 of Scala By Example by Marin Odersky: state changes tracking by storing modified version of object in list,
The concept of using closures to share state in a few dynamically created functions Ihave seen in famous Structure and Interpretation of Computer Programs. I suspect that studying this book alone (and lisp) might bring me a few powerful concepts.
The concepts of fancy list manipulation (lists:foldl, lists:map) is probably basic enough to not be called patterns. Similar situation with pattern matching using to bound variables and extracting elements from data structures. List comprehension is the syntactic construct so we can skip it probably .
Message passing patterns
In Erlang community patterns means OTP behaviours:
- gen_server (remembering some small portion of state by passing it to internally in every invocation, synchronous and asynchronous calls)
- supervisor (managing e.g. restarting after failure work of other tasks)
- gen_fsm (Finite State Machine pattern)
- gen_event (sending events)
There is a few concepts described in Programming Erlang by Joe Armstrong which AFAIK are not directly supported by those behaviours: priority receiving, flushing. But again are probably basic enough to not count as patterns.
Parallel computation patterns
Took me while to find out some tips on how to use Erlang message passing capabilities to do parallel computing. OK I know that I should bang messages to other process mailbox, i could register them or use pids but how actually use them?
Mentioned above book describe this in chapter 20. I found few interesting functions in OTP to support that: rpc:pmap and rpc:parallel_eval. They are very powerful hiding a lot of details about synchronizing results and spreading computation for different nodes/processes. In rpc module there are many interesting functions. And what is most important basic pmap concept can be modified to achieve different building block to create parallel algorithm (see the 20 chapter for details). In here i can’t name patterns yet but i hope this will change.
I don’t know Haskell concepts of monads and lazy evaluation so can’t say anything about them in context of Erlang or functional languages in general. Maybe there are a lot of powerful concepts hiding there 🙂