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.
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.
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.
By default stack installs packages to ~/. cabal and ~/. ghc in your home directory.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With