# Happy Learn Haskell Tutorial Vol 1

Written and illustrated by GetContented.

Published on 2024-05-24.

## Contents

Previous chapter: 1. How to learn Haskell enjoyably
... 2.1. Values
... 2.2. Types
... 2.3. Definitions
... 2.4. Functions
... 2.5. Our First Program
...... 2.5.1. A Definition
...... 2.5.2. A Term, or Variable Name
...... 2.5.3. A Function Name
...... 2.5.4. A String Value
...... 2.5.5. An Expression
... 2.6. Homework
Next chapter: 3. Types as Jigsaw Pieces

# 2. Your First Step 🔗

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 `String`.

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.

## 2.1. Values 🔗

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 `"Step"` is `String`).

## 2.2. Types 🔗

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 `5`, and `34` called `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.

## 2.3. Definitions 🔗

The “`=`” 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.

The `=` 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 `5`.

## 2.4. Functions 🔗

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!

## 2.5. Our First Program 🔗

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:

### 2.5.1. A Definition 🔗

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.

### 2.5.2. A Term, or Variable Name 🔗

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.

### 2.5.3. A Function Name 🔗

`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.

### 2.5.4. A String Value 🔗

`"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`). A `String` is a List of `Char` values. `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.

### 2.5.5. An Expression 🔗

Expressions are how we express values. They have a type, too. This particular expression evaluates to the IO action that results from providing the `String` `"Polly wants a cracker"` to the `putStrLn` function.

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.

## 2.6. Homework 🔗

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.

Also, Volume 2 is now in beta and being written! Show your support and register your interest at its Leanpub site.

Previous chapter: 1. How to learn Haskell enjoyably