I’ve been writing Haskell on and off since about 2001, including three years as a full-time job. This is what I’ve learnt…
- Haskell is the promise that you can write it as cleanly as your understanding of it. Have faith.
- Always be looking for patterns. Abstract them always and only when it simplifies.
- Persevere in getting an abstraction just right. When you find it, everything will magically fall into place.
- The implementation is the design.
- Hide whatever the caller shouldn’t care about. In particular, you can remove type parameters with appropriate quantification.
- There’s a reason
fst3
,snd3
,thd3
are not in base. Triple or bigger: create a data type with fields instead. - Never make an instance unless it morally follows the class’s rules.
- Instances of simpler classes are more valuable than of more complex generalisations.
Monoid
: not a flashy class, but still definitely worthwhile.Applicative
: hugely underappreciated, and good for types that have “static information”. Lets you do deep magic withtraverse
andsequenceA
.Monad
: potential instances are usually easy to spot.- The fewer type parameters in a class, the better. Can you turn any into associated types? Can you split the class into two classes? Can you hive off some of the parameters into a superclass?
- Don’t worry about strictness until it’s time to optimise.
- Intuition about optimisation tends to be bad. Before profiling, limit yourself to reasoning about complexity classes.
- An orphan instance is a very minor wart.
- Types themselves are weightless (i.e., erased). For example,
*
dropped from kind to type (withTypeInType
) carries no information. - You probably won’t need
IORef
. - Template Haskell comes with a high cost in intelligibility. Sometimes it’s worth it.
— Ashley Yakeley