Polymorphism and Design Pattern Haskell Style
The following short demonstration does a nice job capturing parametric polymorphism and design patterns in Haskell.
Domain Modelling with Haskell: Generalizing with Foldable and Traversable
It is easy to see polymorphism in a list. You can have a list of people, and you map a function over them to get their names. Now you have a list of strings, or Maybe Strings for the names of each person on the list. So the list went from List of People to List of Maybe Strings.
This a bit more challenging to see in your own data types. In the video above the data type for Project is modified to use a polymorphic parameter "a" instead of fixed project id. This allows for the project to maintain its structure (the constructors) as functions are applied to the subelements of the structure. This is just like what happens in the list but it is a bit harder to see if you are accustomed to generics in the OO sense.
The notion of design pattern is also interesting in this lecture. There are two main issues with the traditional GOF design pattern. First, is that if there is a pattern why isn't it implemented in code once and reused everywhere. Second, patterns are not in the design but in forms of computation. In this video, you can see the computational patterns Functor, Foldable and Traversable are used in the computation. More importantly, as they are patterns it can be automatically derived.
Domain Modelling with Haskell: Generalizing with Foldable and Traversable
It is easy to see polymorphism in a list. You can have a list of people, and you map a function over them to get their names. Now you have a list of strings, or Maybe Strings for the names of each person on the list. So the list went from List of People to List of Maybe Strings.
This a bit more challenging to see in your own data types. In the video above the data type for Project is modified to use a polymorphic parameter "a" instead of fixed project id. This allows for the project to maintain its structure (the constructors) as functions are applied to the subelements of the structure. This is just like what happens in the list but it is a bit harder to see if you are accustomed to generics in the OO sense.
The notion of design pattern is also interesting in this lecture. There are two main issues with the traditional GOF design pattern. First, is that if there is a pattern why isn't it implemented in code once and reused everywhere. Second, patterns are not in the design but in forms of computation. In this video, you can see the computational patterns Functor, Foldable and Traversable are used in the computation. More importantly, as they are patterns it can be automatically derived.