by calpaterson · 14y ago · 0 comments
The truth is that modern OOP (even in Java) doesn't many of the patterns in the GoF book any longer. That's not much of a surprise though: GoF is 18 years old. Approaches to problems have changed and the patterns used are a bit different.
Idiomatic Haskell is full of design patterns. Here's a few of the popular ones: monads, composition (of functions, not objects), maybe. When I stopped use Haskell actively, they were just getting a handle on the overuse of monads. Pattern overuse had occurred, just as in Java. It seems that pattern popularity follows the Hype Cycle (http://en.wikipedia.org/wiki/Hype_cycle)
lineLengths = map (length . words) . lines
as compared to lineLengths text = map length (map words (lines text))
I don't think that monads are a design pattern either (in this I agree with Tony Morris [1]). Monads are a unified method of doing contextual computations. They are far too general to be a design pattern in themselves (how many design patterns do you know that encompass failure, error handling, nondeterminism, probabilistic computation, logging, backtracking search, mutable state, asynchronous computation, continuations, I/O...)Monads should be more accurately thought of as an interface that is implemented by many types (in Haskell terminology, a class that many types are instances of).
I am not saying that there aren't any design patterns in Haskell - just that function composition and monads are not examples of design patterns. Instead, common design patterns include
* Writing tail-recursive, accumulating helper functions where you'd use a for loop in an imperative language:
sum xs = go 0 xs
where
go result [] = result
go result (x:xs) = go (result + x) xs
* Using strict left folds to accumulate values sum = foldl' (+) 0
* Using runStateT and its cousins to tame stateful computations sum xs = execStateT (go xs) 0
where
go [] = return ()
go (x:xs) = modify (+x) >> go xs
* Using a record of functions to encapsulate related functions that might be given different implementations (like a lightweight version of classes) data Lens a b = Lens { get :: a -> b, set :: a -> b -> a }
* Using libraries like Conduit or Pipes to structure your program as a flow of data through componentized parts.Then there are 'large scale' design patterns, like
* Structuring your code in three parts: (i) data type declarations, (ii) pure functions operating on the data, (iii) a thin I/O wrapper to glue the pure functions together.
* Using the MonadIO, MonadState, MonadWriter etc classes to keep your code implementation agnostic.
To re-iterate: I agree with you that functional languages (like Haskell) still have design patterns. But I don't think that monads or function composition are good examples of functional design patterns.
>The truth is that modern OOP (even in Java) doesn't many of the patterns in the GoF book any longer.
Oh how I wish I shared your optimism. You still need Visitors and Factories and Singletons in Java, because the language still doesn't provide the features that replace them. At least with java 8 we'll finally be getting lambda.
There are cross-cutting concerns in software development that don't have an obvious technical solution. Modular code is one of them. There's no logical reason for us to organize code in such a roundabout way, it's only to help us measly humans reason about it more efficiently during the process of software development. So, there's unlikely to be a way to unproblematically translate that process into something logical.
None of those are design patterns in haskell. They may well be design patterns in other functional languages (probably only monads really), but in haskell they are very basic, fundamental features of the language. That is like saying classes and variables are design patterns.
They everything is design pattern. Except it is not.
I don't interested in what the GoF says, but in the FP world this is not pattern. It's just an another function actually
(.) :: (b -> c) -> (a -> b) -> a -> c
(And referential transparency, lambda calculus, higher-order functions and HM type inference are not design patterns)We have a definitional disagreement: this is a bad definition. Here's what I think, straight off page 3 of GoF:
> A design pattern names, abstracts and identifies the key aspects of a common design structure that make it useful for creating a reusable [object-oriented] design.
Composite is a great counter example to your definition of a design pattern, because it is an example of an important pattern (IMO the most important in GoF) in which no special implementation is required.
PS. Calm down and stop accusing people on the internet of bad faith