We’ll start our Haskell journey by diving into ghci to run some commands interactively. ghci is Haskell’s REPL, an interactive environment that lets you run commands, call functions, and even debug your programs. Most Haskell developers rely on ghci extensively as they develop their programs, and it’s a natural starting point for you to start familiarizing yourself with the basic syntax of the language. For instructions on getting ghci and the rest of your Haskell environment installed and configured, you can consult the instructions online at haskell.org. [1]

When you start up ghci you’ll see a default prompt:

​  ​ $ ​​ ghci ​
​  GHCi, version 9.4.2: https://www.haskell.org/ghc/ :? for help
​  ​ Prelude> ​

The first thing you’ll see is some information about the current version of GHC that you have installed. It’s okay if you are using a newer, or slightly older, version of ghci .

The next line is the ghci prompt. The prompt shows you a list of all of the current modules that you have imported; in this case it’s just Prelude , which is the name given to all of the built-in things that are available by default from Haskell’s standard library.

For the sake of readability, in this book we’ll use a slightly different prompt. First, we’ll omit the list of imported modules. Second, we’ll change our prompt character to the lowercase Greek letter lambda: λ . You can use any letter or symbol you like, but λ is both a nod to the origins of functional programming and a nice visually distinctive way to help you quickly see when we’re working in an interactive session.

​  ​ $ ​​ ghci ​
​  GHCi, version 9.4.2: https://www.haskell.org/ghc/ :? for help
​  λ

Configuring Your ghci Prompt

If you’d like to make your prompt look like the one in the examples, you can type: :set prompt " λ " when you first start ghci . You can also add it to your ghci configuration file, at ${HOME}/.ghci .

Now try typing in a few simple arithmetic expressions to see how you can use ghci as a simple calculator:

​  ​ λ ​ 1 + 2
​  3
​  ​ λ ​ 3 + 4 + 5
​  12
​  ​ λ ​ 12 * (1 + 2 - 2) / 6
​  2.0

Try experimenting with basic arithmetic using parentheses and the + , - , * , / operators to get comfortable working with ghci and entering different expressions.

Working with Lists

Now that you’re comfortable using ghci as a calculator, try creating some lists:

​  ​ λ ​ [1,2,3]
​  [1,2,3]
​  ​ λ ​ [​ "one" ​, ​ "two" ​, ​ "three" ​]
​  [​ "one" ​,​ "two" ​,​ "three" ​]

When you are working with numbers, you can also create a list using the range syntax. You create a range by creating a list with a starting and ending number separated with two dots (..) :

​  ​ λ ​ [1..10]
​  [1,2,3,4,5,6,7,8,9,10]
​  ​ λ ​ [2..5]
​  [2,3,4,5]

As you can see, the range syntax gives us a list of numbers starting at the first number and going up, one at a time, to the final value. If the starting and ending values are the same, we’ll just get a single element list:

​  ​ λ ​ [10..10]
​  [10]

If the starting value is larger than the ending value, then you’ll get back an empty list.

​  ​ λ ​ [10..1]
​  ​ [] ​

If you start your range with two numbers instead of a single number, then the range will increment, or decrement, by the difference between the two starting numbers. Here’s how you’d get a list of the even numbers up to ten:

​  ​ λ ​ [2,4..10]
​  [2,4,6,8,10]

Or the first one hundred numbers in increments of 23:

​  ​ λ ​ [0,23..100]
​  [0,23,46,69,92]

In this example, you’ll notice that our list stops at 92 since that’s the last multiple of 23 that is less than the end of our range.

You can use the same technique to generate a range that counts down. For example, if you want to get a range that counts down from 10, you just need to provide the first two numbers:

​  ​ λ ​ [10,9..0]
​  [10,9,8,7,6,5,4,3,2,1,0]

You can embed arithmetic expressions inside of lists too. Look at this example. Do you see what the output will be when you run it?

​  ​ λ ​ [10 + 2, 10 * 2, 10 - 2, 10 / 2]

When you’re playing around with longer lists, reading the code in ghci can start to become difficult, especially if you have lines of code longer than the width of your terminal. You can break up a long expression across multiple lines by typing :{ on its own line, entering as many lines as you like, and finishing with :} , also on its own line:

​  ​ λ ​ :{
​  ​ Prelude ​| [ 1
​  ​ Prelude ​| , 2
​  ​ Prelude ​| ]
​  ​ Prelude ​| :}
​  [1,2]

This feature isn’t limited to lists; you can also use it for any other type of expression when you find that it helps readability while working in ghci .

Creating Lists with More Than One Type

You might have noticed that all of our examples are of lists that only contain a single type of value, for example, a list of all strings, or a list of all numbers. In Haskell, a list can only contain one type of value at a time. If you try to create a list that has two different types of values, like a number and a string in the example, you’ll get an error message:

​  λ [1, "two"]
​  
​  <interactive>:6:1: error:
​  • Ambiguous type variable ‘a0’ arising from a use of ‘print’
​  prevents the constraint ‘(Show a0)’ from being solved.
​  Probable fix: use a type annotation to specify what ‘a0’ should be.
​  These potential instances exist:
​  instance Show Ordering -- Defined in ‘GHC.Show’
​  instance Show Integer -- Defined in ‘GHC.Show’
​  instance Show a => Show (Maybe a) -- Defined in ‘GHC.Show’
​  ...plus 22 others
​  ...plus 15 instances involving out-of-scope types
​  (use -fprint-potential-instances to see them all)
​  • In a stmt of an interactive GHCi command: print it

This error message should look pretty confusing right now, and that’s okay. As you are getting started with Haskell you’ll run into many situations where unexpected things might cause you to see some errors. Some errors, like this one about ambiguous type errors, will be particular common early on. Don’t worry about these too much yet. As you progress through this text you’ll learn more about what causes these errors, and how to fix them. You’ll be able to understand more about this error in particular once you learn more about lists and Haskell’s type system. You’ll learn much more about how to construct lists and use them effectively in the next chapter . Later, when you start to learn more about Haskell’s type system , you’ll learn how to read and better interpret error messages like this.

Creating Pairs

Tuples in Haskell let you store pairs, or more than pairs, of values. A list can hold any number of values, but they all have to be the same type. A tuple can hold different types of values, but size is fixed. You can’t add or remove items from a tuple. Tuples are defined with parentheses:

​  ​ λ ​ tuple = (2, ​ "two-pule" ​)
​  ​ λ ​ thruple = (3, ​ "triple" ​, ​ "thruple" ​)
​  ​ λ ​ quadruple = (4, ​ "quadruple" ​, 4.0, ​ False ​)

Two-element tuples are the most common kind of tuple, and so there are functions to help you get the first and second elements out of them easily. The functions are called fst and snd :

​  ​ λ ​ fst (2, ​ "tuple" ​)
​  2
​  ​ λ ​ snd (2, ​ "tuple" ​)
​  ​ "tuple" ​

You can nest tuples and lists with other tuples and lists however you like:

​  ​ λ ​ ([1,2,3],[​ "one" ​,​ "two" ​,​ "three" ​])
​  ([1,2,3],[​ "one" ​,​ "two" ​,​ "three" ​])
​  ​ λ ​ [(1,2),(3,4)]
​  [(1,2),(3,4)]

Although the fields of a tuple can be different types, when you have a list of tuples they have to all have the same number of elements, and the types have to all be the same at each position in the tuple. For example, we can have a list of tuples that contain numbers and strings:

​  ​ λ ​ [(​ "haskell" ​, 7), (​ "is" ​, 2), (​ "fun" ​, 3)]
​  [(​ "haskell" ​,7),(​ "is" ​,2),(​ "fun" ​,3)]

However, we’ll get an error if we try to swap the order of fields in some of the tuples:

​  λ [("haskell",7),(2,"is"),("fun",3)]
​  
​  <interactive>:3:1: error:
​  • Ambiguous type variable ‘a0’ arising from a use of ‘print’
​  prevents the constraint ‘(Show a0)’ from being solved.
​  Probable fix: use a type annotation to specify what ‘a0’ should be.
​  These potential instances exist:
​  instance (Show a, Show b) => Show (Either a b)
​  -- Defined in ‘Data.Either’
​  instance Show Ordering -- Defined in ‘GHC.Show’
​  instance Show a => Show (Maybe a) -- Defined in ‘GHC.Show’
​  ...plus 24 others
​  
​  ...plus 47 instances involving out-of-scope types
​  (use -fprint-potential-instances to see them all)
​  • In a stmt of an interactive GHCi command: print it

We’ll also get an error if one of the tuples has a different number of elements, even if the elements are all of the same type:

​  λ [(1,2), (4,5), (5,6,7)]
​  
​  <interactive>:5:16: error:
​  • Couldn't match expected type: (a, b)
​  with actual type: (a0, b0, c0)
​  • In the expression: (5, 6, 7)
​  In the expression: [(1, 2), (4, 5), (5, 6, 7)]
​  In an equation for ‘it’: it = [(1, 2), (4, 5), (5, 6, 7)]
​  • Relevant bindings include
​  it :: [(a, b)] (bound at <interactive>:5:1)

As with the other error messages you’ll see in this chapter, you don’t need to worry about understanding the details of what went wrong just yet. You’ll learn how to read these sorts of error messages later on in the book.

Printing Things to the Screen

Although ghci automatically prints the value of an expression to the screen, when you’re writing full programs you’ll want to control when you print things out to the screen. The putStrLn function will print a string to the screen with a newline (you can use putStr if you don’t want the extra newline). You can print values in ghci as well.

Try it out yourself by typing putStrLn "Hello, World!" into ghci . Notice that in Haskell calling a function doesn’t require any parentheses. Calling a function (or as we often say in Haskell, applying a function to some arguments) is as simple as functionName arg1 arg2 arg3 .

If you try to use putStrLn to print a number to the screen you’ll get another type error, since it only works with strings. Pick your favorite number and try passing it to putStrLn to see an example of this.

You can convert most types, like numbers and lists, into strings using the show function, as in the following examples. Try running these yourself to get a feel for how it works:

​  putStrLn (show 12)
​  putStrLn (show [1..10])
​  putStrLn (show ​ "Hello, World!" ​)

Calling show manually can add a lot of unnecessary boilerplate to your code. The print function combines show and putStrLn into a single function. Try it yourself by using print with your favorite number to print it onto the screen.

Spend as much time as you’d like trying out different expressions in ghci . When you’re ready to move onto the next section, you’ll write your first real Haskell program. Throughout the rest of this chapter you’ll learn more about the basics of Haskell’s syntax and semantics and be able to write more useful expressions in ghci .