Haskell, Graphs, and Underpants

With Haskell and GraphViz, it’s a cinch to publish graphs. Unfortunately, it’s not a cinch until you learn that it’s a cinch. I’m not afraid to admit that it took me three days to understand the graphviz library.

  1. Install Haskell, GraphViz, and the graphviz library.
  2. Download gnomes.hs.
  3. Run ./gnomes.hs > gnomes.dot
  4. Run dot -Tpng gnomes.dot > gnomes.png

You’ll get the following image:

graphical image

The intermediate step isn’t important.

Repeat with me.hs and you’ll get another image:

graphical image

An ego-centric graph.

As I said before, the hard part is learning how graphviz is a cinch. Specifying the parameters in graphToDot is tricky. Luckily, Ivan Miljenovic and Brent Yorgey were quick to offer help. Once you get the hang of specifying Attributes, you’re ready to profit!

Update: This guide forms the basis for a much more complicated graph of the Collatz Space.

7 responses to “Haskell, Graphs, and Underpants

  1. Yes, maybe I should go and finish off that graphviz tutorial I started on a few months back…

  2. How do I run “./gnomes.hs > gnomes.dot”? I get an error.

    $ ./gnomes.hs > gnomes.dot
    bash: ./gnomes.hs: Permission denied
    $ bash ./gnomes.hs > gnomes.dot
    ./gnomes.hs: line 3: –: command not found
    ./gnomes.hs: line 4: –: command not found
    ./gnomes.hs: line 5: –: command not found
    ./gnomes.hs: line 6: –: command not found
    ./gnomes.hs: line 7: syntax error near unexpected token `(‘
    ./gnomes.hs: line 7: `– Haskell 2010 (http://haskell.org/)’

    I don’t know how to run haskell files without compiling them (ghc…) and compiling them also gives an error.

  3. The link to gnomes.hs is incomplete. I think you want this link: https://github.com/mcandre/mcandre/blob/master/haskell/gnomes.hs

  4. @holdenlee: maybe you need to “chmod +x gnomes.hs” first.

    Also, do you have GHC installed?

  5. Nah, Ivan. The original article linked to an old code snippet that I’ve since moved and correctly chmod’ed onto GitHub.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s