What are some advanced features of Haskell that I should learn?

I realized that learning functional programming has given more tools to elegantly and concisely compose my programs that are correct. I noticed this when I was reviewing my old algorithms book. The algorithms that they provided are written in a somewhat ugly way where you have to do a lot of imperative pointer manipulation and there is an excessive use of sentinel values. For example, you have to compare pointer addresses in the book to determine the left child of binary tree node. Rather than doing this, you can simply use pattern-matching.

I noticed that you can rewrite these algorithms in a much more elegant, type-safe and easier to understand way using tools from Haskell such as GADTs and Monads. Seeing this elegance, it has actually got me obsessed to learn more about Haskell.

I was trying to learn more of Haskell’s advanced features, but there doesn’t seem to have a definitive of guide for it. I was wondering what are some advanced features I should learn from Haskell. Namely, I am interested in learning different ways to make my code more type-safe and more cononical ways to compose my programs.

In terms of my background in functional programming, I have written professional OCaml for over a year and I have used many of the advanced features of the language, such as GADTs and developed my own ppxs.

2 Likes

I think proper laziness is quite an advanced topic. One example of what I would call proper laziness are the online parser combinators developed by Doaitse Swierstra: http://www.cs.tufts.edu/~nr/cs257/archive/doaitse-swierstra/combinator-parsing-tutorial.pdf

An implementation of these ideas is in the uu-parsinglib package.

2 Likes

My opinion is that some of them (e.g. lenses) are overrated; I think recursion schemes are beautiful but perhaps lean towards arcane. Monads are beautiful, but especially the way the IO monad works. I think Generics exist in OCaml world but it’s impressive if you are used to mainstream languages like C/C++/Rust/Swift/Python &c.

There’s a lot of wonderful stuff out there. Some lesser-known bits: c2hs, accelerate, happy, alex. Some libraries unique to Haskell: dhall (have a special look at generic marshaling! it’s quite impressive). dlist is neat and I’ve actually had opportunity to use it to make my code faster. Lazy I/O via the ST monad (as in here) is both questionable and very good to work with.

I haven’t used fingertree myself, or EdisonCore but they look neat. ZipperFS and XMonad have nice examples of how to use functional data (even lazy!) structures where you actually get good bang for your buck.

2 Likes

I’m interested in a general answer to your question as well. Meanwhile you might be interested in reading through Chapter 9 of GHC user’s guide which provides a window into many of these advanced features.

1 Like

Dependent types is one avenue for exploration. GHC will support them in future, but for now you may explore libraries like dependent-sum, constraints-th, aeson-gadt-th (all deal with GADTs, and generating instances for them); here’s an example.

Here are a few advanced topics that I find attractive:

1 Like

A style of programming that requires some conceptually advanced features of Haskell is represented by Functor-Oriented Programming http://r6.ca/blog/20171010T001746Z.html

For this sort of question I always recommend What I Wish I Knew When Learning Haskell. It’s an overview of many advanced features that you might want to learn, summarizing what they do and how they work. It’s particularly good for finding new things to learn about Haskell: when I was learning Haskell, I regularly would visit it, find an interesting-looking feature and then learn all about that, before going on to the next interesting feature I found.