All hypercubes with Haskell

Posted on March 10, 2018 by Stéphane Laurent

Nothing really exciting in this post. But these days I did some graphics dealing with hypercubes, and the functions I show below simplified my job. Perhaps it is worth to share.

This Haskell function generates the vertices of the hypercube of a given dimension \(n\):

For example:

It can be useful to have the indices of the vertices. The indexed function of the nice Data.List.Index package is appropriate for that. I also import Text.Show.Pretty to get a pretty output thanks to the pPrint function.

Sometimes I prefer to have the index at the second position. Then I use swap:

The vertices are not enough to get a graph. We need the hypercube graph \(Q_n\). It is available in the HaskellForMaths library: