Haskell variables must start with a lowercase letter and can contain letters, numbers, underscores, and the single quote character (‘). You can use Unicode letters in Haskell variable names, but we’ll stick to ASCII in this book. By convention, Haskell variables use camelCase. Here are some examples:

​  helloWorld = ​ "Hello, World" ​
​  number5 = 5
​  snake_case_variable = ​ True ​
​  number5' = 6

As you might expect, we can also assign a variable to another variable:

​  five = 5
​  number = five

In ghci you can enter the name of a variable to see its value:

​  ​ λ ​ helloGeorge = ​ "Hello, George" ​
​  ​ λ ​ helloGeorge
​  ​ "Hello, George" ​
​  ​ λ ​ five = 2 + 3
​  ​ λ ​ five
​  5

You can also re-use a variable name in ghci . For example, let’s create a new greeting, and then change it:

​  ​ λ ​ greeting = ​ "Good Morning" ​
​  ​ λ ​ greeting
​  ​ "Good Morning" ​
​  ​ λ ​ greeting = ​ "Good Afternoon" ​
​  ​ λ ​ greeting
​  ​ "Good Afternoon" ​

In this example, we are re-using the variable name greeting , but we are not changing the value of the variable. This can be a subtle distinction, so let’s look at an example to help make it more clear. We’ll start by creating two new variables, one and two :

​  ​ λ ​ one = 1
​  ​ λ ​ one
​  1
​  ​ λ ​ two = one + one
​  ​ λ ​ two
​  2

If we re-use the variable name one , and this time set it to five , we can see that the value of two doesn’t change:

​  ​ λ ​ one = 5
​  ​ λ ​ one
​  5
​  ​ λ ​ two
​  2

From the time that we re-use the name one , anytime we type one we will be referring to the new variable, but any code that was written before we re-used the variable name will continue referring to the original variable.

In languages where we can change the value of a variable, it’s common to set the new value of a variable based on the old value. If we try to do that in ghci , the result might be surprising. Let’s try setting two to one, and then incrementing it:

​  ​ λ ​ two = 1
​  ​ λ ​ two = two + 1

So far, so good. Unfortunately, if we try to print out the value of two you’ll see that ghci will seem to get stuck. Press control+c to regain control of ghci :

​  ​ λ ​ two
​  ^​ CInterrupted ​.

So what happened? This is a common bug that causes problems for people first learning Haskell, and occasionally even catches experienced Haskellers off guard. The problem is, since all of our variables in Haskell are immutable, we can’t actually modify two to increment it. Instead, when we say two = two + 1 we’re creating a brand new variable named two , and its definition is recursive . The two on the right hand side of the expression is referring to the brand new two that we just defined. We have essentially just asked ghci to count forever. Since Haskell is a lazy language, it doesn’t actually try to run this infinitely recursive code until we ask it to print out the value of two . You’ll learn more about recursion, and how to apply it usefully, later on in this chapter, and you’ll learn more about laziness in the next chapter .

For now, we can fix our infinite recursion bug by choosing a different name for our variable. The problem of wanting a variable that represents a slightly modified version of an earlier variable comes up frequently in Haskell, so there’s a common pattern: using prime variables. Let’s look at an example:

​  ​ λ ​ two = 1
​  ​ λ ​ two' = two + 1
​  ​ λ ​ two'
​  2

In this example, you can see that instead of re-using the variable name two directly, we have added a single quote ( ’ ) to the end of the variable name. Haskellers sometimes pronounce this “prime” as in “two prime.” You might also see the symbol pronounced “tick” when it’s used in this context. There’s no special meaning to the character, it’s simply another letter that you’re allowed to use anywhere, except as the first character, when you are naming variables. You can add as many as you like:

​  ​ λ ​ two' = two + 1
​  ​ λ ​ two'' = two' + two
​  ​ λ ​ two''' = two'' + two'
​  ​ λ ​ two'''
​  5

In practice, variables with a single tick are common. Double prime variable names, like two” , are rare but occasionally useful. If you’re using three or more primes then it’s probably time to refactor your code.

Now that you’ve worked with variables in ghci , let’s get a feel for how they work in normal applications by building a full program that defines some variables and then prints them out. We’ll start by creating a new Main.hs . Next, we’ll define a pair of strings named salutation and person . Finally, we’ll make a third variable named greeting that joins the two strings. We can join strings in Haskell using the (<>) operator: "like" <> " " <> "this!" . In some code bases, especially older ones, you might also see the (++) operator being used to join strings: "like" ++ " " ++ "this!" . The (++) operator is a less general way to join things, and it only works for lists and plain Haskell strings. Since (<>) is more general and more commonly used, we’ll stick to it throughout this book.

​  ​ module ​ ​ Main ​ ​ where ​
​  
​  salutation = ​ "Hello" ​
​  person = ​ "George" ​
​  greeting =
​  salutation <> ​ " " ​ <> person
​  
​  main = print greeting

You’ll notice that all of the variables we created in this example are defined at the top level, outside of main . We’ll stick with this approach for now, and you’ll also learn how to define variables inside of functions later in this chapter.

Working with variables inside of a Haskell program is generally the same as working with them in ghci , with one exception. As a concession to practical usability, ghci allows us to redefine variable names. Outside of ghci we would rather not risk bugs caused by having an ambiguous value for a variable, so re-using names inside of the same scope isn’t allowed. If we try to add two variables named message into the top level of Main.hs we’ll get an error. Let’s try it to see what happens:

​  ​ module ​ ​ Main ​ ​ where ​
​  
​  message = ​ "Hello, George" ​
​  message = ​ "Hiya, Porter" ​
​  
​  main = print message

If you try to run this program, you’ll get an error:

​  Main.hs:4:1: error:
​  Multiple declarations of ‘message’
​  Declared at: Main.hs:3:1
​  Main.hs:4:1
​  |
​  4 | message = "Hiya, Porter"
​  | ^^^^^^^

You can see in this example that the compiler has helpfully pointed us to the exact location of the error, and told us what’s wrong. From here we could either remove the duplicate definition, or we can choose a different name. In the next section, you’ll learn about writing your own functions, and later on in this chapter you’ll learn about creating new local variables using let and where . It’s useful to keep in mind as you learn about these things that you can always re-use variable names in a new scope.