How to run Haskell source files like shell scripts

submited by
Style Pass
2024-11-15 13:00:06

This sucked me into a wikipedia rabbit hole, so I will not tell you whether the # character was a comment before #! was used for “run this file by handing it to this other program”.

If you also have cabal installed, this way is nice because you can put package dependencies in the source file (and specify compiler version and more).

You can try this yourself by copying the Haskell program above into “Foo.hs” and running chmod u+x ./Foo.hs and then running “./Foo.hs”.

I heard from Simon Michael that this can break if your installed GHC changes versions and the library no longer matches the API. That makes sense, but I hadn’t thought about it.

The hedgehog script above takes one minute forty seconds on first run! I don’t know why it takes so much time. On later runs it’s less than two seconds, but golly that first run is rough.

If you prefer using stack instead of cabal, that works too. Here’s an example from hledger, written by the above linked Simon Michael.

Leave a Comment