Please Buy now at Leanpub
Written and illustrated by GetContented.
Published on 2016-05-20.
Let’s begin our journey to learn Haskell with a program that gets a message on the screen.
It’s important to begin simply, because doing small steps helps you to avoid frustration and increases happiness!
So, in this lesson, we’ll discover this very simple program together, and pull it apart to understand it. None of the following sentence will make any sense yet, but it will by the end of the lesson: our first program will be a single definition for an entry-point called
main, and it will use a built-in function called
putStrLn and a value of type
So, we’ll first explain what the terms values, types, definitions and functions mean when talking about Haskell, then we’ll show you the program, and explain it piece by piece.
A value in Haskell is any data. The word
"Step", for example. All values have a type, which tells us and Haskell what sort of data they are. (The type of
A type is a name for a set or group of values that are similar. As an example, there is a type for representing values like
Integer. Most of the time, the types of our values are not written down in the program. Haskell will work out what the types are for you, however, Haskell lets you write them down if you’d like to, like this:
5 :: Integer and
34 :: Integer.
We’ll definitely learn much more about types later, so don’t worry too much about them for now. To whet your appetite, though, Haskell lets us make our own types. This means it’d be impossible to list them all here, but you can easily write most programs using only a handful of types, so it’s not too hard to think about.
Types are important because they decide how the pieces of your program can fit together, and which values are allowed to go where.
=” symbol lets us tell Haskell that we want it to remember a name for an expression. The name goes on the left of it, and the expression on the right. An expression is simply a way to connect up some values together in relation to each other.
= symbol relates (or we can say binds) the name or pattern on the left of it with the expression on the right of it. For example,
five = 5 tells Haskell that the name
five means the value
A function is a relation between one type and another type, and they’re used in expressions with values. In Haskell a function is itself a value. If you provide a function with an input value, (by applying the function to the value), it will return (give you back) the corresponding output value of the output type when it’s evaluated.
Be careful how you think about this! Function means something different in Haskell than in most other programming languages. Functions in Haskell are not sets of steps for the computer to follow.
Our first program will show you an example of function application, so read on!
Here, then is our first program to read. Just one line. We see an expression that is being named “main”. The expression involves a function being applied to a value, and the types are not shown:
main = putStrLn "Polly wants a cracker"
If you were to run this first program, it will put
"Polly wants a cracker" on to the screen.
This may look very confusing, but keep calm. We’ll break it into five parts, and explanations:
This whole line is called a definition. It’s made of three pieces: 1. the variable or term name, 2. the “
=” symbol, and 3. an expression.
Haskell programs are created by making definitions and expressions and embedding expressions in other definitions and expressions.
It’s important to realise that the “
=” symbol doesn’t mean we’re “setting” anything here, or “putting” anything into anything else. We use it to name expressions, is all. The name does not “contain” the value, so we cannot re-define the same name at a different point in our program, or change the name’s “content” or definition after it has been defined.
main: This piece is the name (called the term or variable) that we’re defining. In this particular case, it’s “
main”, and that is a special name to Haskell. Every program must define
main, and it is evaluated and executed by Haskell to run your program. If you named it
pain instead of
main, your program would not work. This is called the entry point because it’s where Haskell will start executing your program from.
main is a value, and also an input/output action (IO action for short). IO actions are different to normal values because they describe how to make input & output happen. We’ll learn more about this later.
putStrLn: this is a function name. If we put the name of a function in an expression and a value to its right like this, when it’s evaluated, Haskell will apply the function to the value to “produce” another value. We say it takes a String value, and returns an IO action. When this IO action value is executed, it will output its String to the screen.
"Polly wants a cracker": This is one way to write text in Haskell programs. It is a value whose type is
String. (We can just call this a
String is a List of
Char values, or characters, are any written letter, number, or symbol. If you like, you can imagine a
String as symbols pegged to a piece of yarn — strung together. We’re using this
String in our program to provide the
putStrLn function with the data it needs to print to the screen.
Expressions are how we express values. They have a type, too. This particular expression evaluates to the IO action that results from providing the
"Polly wants a cracker" to the
This is a lot to take in all at once. Study it well, but don’t worry if things aren’t clear yet, it will become easier as you read on. Check back to this page later for reference.
Your homework is to try to run this program, if you have a computer. This will involve setting up Haskell, too! Look at Chapter 21 if you want some helpful hints on how to get started and set up your Haskell development system.
If you’ve enjoyed reading this, please consider purchasing a copy at Leanpub today
Please follow us, and check out our videos Follow @HappyLearnTutes
Also, Volume 2 is now in beta and being written! Show your support and register your interest at its Leanpub site.