Haskell:
Starting a new project

How to:

-- 1. Initializing a new Haskell project using Stack
$ stack new myproject

-- The above command creates a new directory `myproject` with some files:
-- myproject/
-- ├── app/
-- │   └── Main.hs        # Your Main application file
-- ├── src/               # Source files for the library
-- ├── test/              # Test files
-- ├── myproject.cabal    # Package description file
-- ├── stack.yaml         # Stack configuration
-- └── Setup.hs           # Build setup script

-- 2. Building the project
$ cd myproject
$ stack build

-- 3. Running your new Haskell project
$ stack run

-- Sample output:
someFunc

Deep Dive

Haskell projects often rely on tools like Stack or Cabal. Stack manages dependencies, ensuring consistent builds. In 2008, Stack was a game-changer for Haskell, addressing Cabal’s shortcomings with package conflicts.

Alternatives include using Cabal alone or newer tools like GHCup or Nix for reproducible builds. You might choose Cabal for simplicity or Nix when your work demands reproducibility, but Stack strikes a happy balance for many.

Under the hood, stack new leverages a template to scaffold a project. It includes not just your source code but also configurations for building and dependencies. The .cabal file is pivotal, containing metadata and build instructions.

See Also