Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Packages, modules and import in Haskell

Tags:

haskell

I'm newish to Haskell. Would someone kindly explain how code organization works in Haskell? To date I have written everything in one big long file and tested code from GHCi. I want to start writing tests an refactor my code into logical units. There are bits and pieces about how to do this in lots of places, but nothing comprehensive.

Please explain how packages, modules and the import statement work then tell me how to use them to organize code into a larger, test-driven project.

like image 630
John F. Miller Avatar asked Mar 30 '11 01:03

John F. Miller


People also ask

How do I import modules in Haskell?

The syntax for importing modules in a Haskell script is import <module name>. This must be done before defining any functions, so imports are usually done at the top of the file. One script can, of course, import several modules. Just put each import statement into a separate line.

What is a Haskell package?

A package is a library of Haskell modules known to the compiler. GHC comes with several packages: see the accompanying library documentation. More packages to install can be obtained from HackageDB.

Where are Haskell packages stored?

By default stack installs packages to ~/. cabal and ~/. ghc in your home directory.

How do I load a file into Haskell?

Basic Haskell Commands :quit (or :q) :load [filename] (or :l [filename]) :reload -- reload the current file set after you'ved edited it (or :r) :cd -- change to another directory.


1 Answers

Here is an introduction Haskell modules from "Learn You a Haskell for Great Good!":

http://learnyouahaskell.com/modules

I would call a package the smallest deliverable unit of Haskell software. For most people, it is enough to say "packages are the things on Hackage".

If you're shipping an executable I wouldn't worry about breaking up your project into multiple packages until you get to your second project, and want to re-use modules from the first.

like image 80
Antoine Latter Avatar answered Oct 13 '22 13:10

Antoine Latter