Lesson 34. Organizing Haskell code with modules

 

After reading lesson 34, you’ll be able to

  • Understand the main module implicitly used when you create a program
  • Create namespaces for your functions by using modules
  • Separate programs into multiple files
  • Selectively import functions from modules

Up to this point in the book, we’ve covered a wide range of interesting topics related to Haskell. But we haven’t discussed one of the most basic topics: creating namespaces for your functions. Haskell uses a system of modules to create separate namespaces for functions and allow you to organize your code much better. This works similarly to modules in languages such as Ruby and Python, and to packages and namespaces in languages such as Java and C#.

You’ve already used Haskell’s module system. Every time you use import, you’re including a new module in your program. Additionally, all the built-in functions and types you have—such as [], length, and (:)—are all included in the standard module named Prelude that’s automatically imported. The full documentation for Prelude can be found on Hackage (https://hackage.haskell.org/package/base/docs/Prelude.html).

34.1. What happens when you write a function with the same name as one in Prelude?

34.2. Building a multifile program with modules

Summary