Program #
File #
A simple Haskell program is written in a file with the extension .hs.
For instance, MyProgram.hs.
Content #
Functions #
A Haskell program primarily consists of function definitions.
Hint. The order of function definitions in a program is irrelevant.
The function main
#
To make a program executable, one of its functions must be the entry point.
By default, this function is called main, and has type IO ().
Example. This is a small executable Haskell program.
main :: IO () main = print $ square 3 square :: Int -> Int square x = x^2The function
maincalls the functionsquarewith the argument3, and prints the result.
Syntax. The
$operator is an alternative syntax for function application.In the function
mainabove, the equationmain = print $ square 3is equivalent to:
main = print (square 3)
Reminder. Types in Haskell are optional, but highly recommended. Your IDE may be able to generate them.
Modules #
A Haskell program can import other Haskell programs, called modules.
The Haskell Prelude #
Every program implicitly imports a default module called the Haskell Prelude. It contains many of the functions and types that we will need in this course.
Importing a module #
If you want to import another module, add the following to your file, before any function definition:
import <moduleName>
Example. The function
ordthat we have seen earlier is defined in a module calledData.Char.In order to use this function, you can import this module, as follows:
import Data.Char main :: IO () main = print $ ord 'V'
Warning. If you define (and use) a function that is already defined in the Prelude or an imported module, then your program will not compile.
Example. The function
reverseis already defined in the Prelude. So if we try to implement it, we should give it a different name, for instancereverse'.
To go further. You can also:
- selectively import certain functions from a module (e.g. write
import Data.Char (ord)in the example above), or- require imported function to be preceded by the module name (this is called a qualified import).
For the syntax, you may for instance read this page.
Declaring a module #
Optionally, you can make your own program a module (which lets you import it in other programs).
Syntax. A module name must start with a capital letter.
Syntax. To make your program a module, start with (assuming that you want to name your module
MyModule):module MyModule where
Syntax. By convention, the name of a module is often the name of its file, without extension.
To go further. You can also select which functions of your module will be exported. This lets you expose only the important functions of your module (hiding auxiliary functions).
For the syntax, you may for instance read this page.
Comments #
- A commented line starts with
--. - A commented block is between
{-and-}.
Example.
-- Squares an integer square :: Integer -> Integer square x = x^2 {- Computes the factorial of a non‑negative integer. Does not terminate for a negative integer. -} factorial :: Integer -> Integer -- Base case factorial 0 = 1 -- Inductive case factorial n = n * factorial (n - 1)
Compilation, execution #
Your IDE may provide a way to compile and run an executable program.
Otherwise, within a terminal (assuming that your file is named MyProgram.hs), run:
runghc MyProgram.hs