How to setup a new Haskell project?

For simple programs with no dependencies, simply create a .hs file and compile it with ghc:

ghc main.hs && ./main

For bigger programs I use Stack.

Initialize Stack (this creates a new directory; use --bare to use the current directory).

stack new myapp

Add any dependencies to package.yaml under the “dependencies” section.

Place source files in app directory and then:

stack build

To rebuild as code changes:

stack build --file-watch

To rebuild and execute on changes (doesn’t work for things that run forever, e.g. servers):

stack build --file-watch --exec $(stack path --local-install-root)/bin/myapp-exe

Execute the binary with

stack run myapp