Now that you’ve learned how to create new top-level variables and functions, it’s time to look at how to build more complex functions. In this section, you’ll learn how to build larger and more complex functions by building up intermediate computations using let bindings.

Let bindings allow you to give a name to some particular expression in your program. Let’s start with an example. We’ll rewrite our makeGreeting function from before to use an intermediate value. If you still have access to your previous file where you defined makeGreeting, feel free to re-use it. Otherwise, create a new Main.hs with a function named makeGreeting with the following definition:

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

Next, update your makeGreeting method to create an intermediate value using a let binding. A let binding consists of the keyword let followed by some variable definitions, and then the keyword in followed by an expression. In short: let vars in expr.

Let’s see how to we can add a let binding to makeGreeting:

​ makeGreeting salutation person =
​ ​let​ messageWithTrailingSpace = salutation <> ​" "​
​ ​in​ messageWithTrailingSpace <> person

You aren’t limited to a single variable inside of a let binding; you can create as many different local variables as you want. We can write an extended greeting function that demonstrates how we can create multiple local variables in a single let binding:

​ extendedGreeting person =
​ ​let​ hello = makeGreeting ​"Hello"​ person
​ goodDay = makeGreeting ​"I hope you have a nice afternoon"​ person
​ goodBye = makeGreeting ​"See you later"​ person
​ ​in​ hello <> ​"​​\n​​"​ <> goodDay <> ​"​​\n​​"​ <> goodBye

As with top-level bindings, you are free to reference bindings that you define later in the same let expression. For example, we could have rewritten extendedGreeting by saying:

​ extendedGreeting person =
​ ​let​ hello = makeGreeting helloStr person
​ goodDay = makeGreeting ​"I hope you have a nice afternoon"​ person
​ goodBye = makeGreeting ​"See you later"​ person
​ helloStr = ​"Hello"​
​ ​in​ hello <> ​"​​\n​​"​ <> goodDay <> ​"​​\n​​"​ <> goodBye

In this example, hello references the helloStr variable that we don’t define until later in the same let binding.

When you create a let binding, the expression you are binding a name to doesn’t need to be a constant, like a string or a number. You can also use let bindings to define new functions. The syntax is the same as defining a top-level function:

​ extendedGreeting person =
​ ​let​ joinWithNewlines a b = a <> ​"​​\n​​"​ <> b
​ hello = makeGreeting ​"Hello"​ person
​ goodbye = makeGreeting ​"Goodbye"​ person
​ ​in​ joinWithNewlines hello goodbye

Haskell supports recursive let bindings, which means that the items inside of our let bindings can refer to one another. The order doesn’t matter; you can refer to items that you define further down in the let binding like in this example:

​ exntededGreeting person =
​ ​let​ joinWithNewlines a b = a <> ​"​​\n​​"​ <> b
​ joined = joinWithNewlines hello goodbye
​ hello = makeGreeting ​"Hello"​ person
​ goodbye = makeGreeting ​"Goodbye"​ person
​ ​in​ joined

Let bindings can also be nested. For example, if you are defining a new function inside of a let expression, and you want to define some variables inside of that function, you can use nested let expressions.

Now that you understand a little bit more about let bindings, follow along with the next example to create an extendedGreeting function that uses let bindings to define intermediate values and functions:

​ extendedGreeting person =
​ ​let​ joinWithNewlines a b = a <> ​"​​\n​​"​ <> b
​ helloAndGoodbye hello goodbye =
​ ​let​ hello' = makeGreeting hello person
​ goodbye' = makeGreeting goodbye person
​ ​in​ joinWithNewlines hello' goodbye'
​ ​in​ helloAndGoodbye ​"Hello"​ ​"Goodbye"​

There’s one final type of binding called a where binding. A where binding follows all the same rules as a let binding, except it comes at the end of a function instead of the beginning, and uses the where keyword instead of let .. in. Any parameters that you’ve bound to a variable name in your function will be available to your where binding, but not anything you’ve defined in a let binding. Conversely, anything you define inside of a where binding will be available to use in let bindings:

​ letWhereGreeting name place =
​ ​let​
​ salutation = ​"Hello "​ <> name
​ meetingInfo = location ​"Tuesday"​
​ ​in​ salutation <> ​" "​ <> meetingInfo
​ ​where​
​ location day = ​"we met at "​ <> place <> ​" on a "​ <> day

You can see in this example that the location function in our where clause can access the place parameter to the function, but we have to explicitly pass in day as a parameter, since location does not have access to variables defined in the let binding.

Try rewriting your extendedGreeting function to use a where binding instead of a let binding:

​ extendedGreeting person =
​ helloAndGoodbye ​"Hello"​ ​"Goodbye"​
​ ​where​
​ helloAndGoodbye hello goodbye =
​ joinWithNewlines hello' goodbye'
​ ​where​
​ hello' = makeGreeting hello person
​ goodbye' = makeGreeting goodbye person
​ joinWithNewlines a b = a <> ​"​​\n​​"​ <> b

The choice of whether to use let or where bindings is a matter of personal style and what seems to be the most readable as you are writing the code. In this book we’ll use a mixture of both, and it’s common to see them both used in real applications. As a general rule of thumb, we’ll use let bindings for intermediate values and where bindings for ancillary and helper functions, but this is an arbitrary choice and we might deviate from the pattern from time to time if doing so will help make the code more readable. As you work through the examples in this book, consider experimenting with both styles of bindings to get a feel for your own personal style.