Can you make heterogeneous lists in Haskell? Sure — as long your intent is clear

submited by
Style Pass
2021-09-27 15:00:13

Sometimes, Haskell’s type system seems a bit restrictive compared to dynamic languages like Python. The most obvious example is the heterogenous list:

This is a contrived example, of course. But consider this use-case: I just want to print the content of the list. It’s unfortunate I can’t write:

For this specific application, the type system is overly restrictive – as long as all I want to do is print the content of my list! In this post, I’ll show you how to do something like this using the ExistentialQuantification language extension.

Let’s say I want to list American football players. There are two broad classes of players (offensive and defensive) and we want to keep track of the players in a list – the player registry. Our final objective is to print the list of players to standard output.

The Player type is too general; we’re not using the type system to its full potential. It’s much more representative of our situation to use two separate types:

Leave a Comment