Chapter 1

Computers memorize things

When we set an alarm clock, we pick our inputs and the alarm remembers them and uses them; when we program, the same principles apply. Let's try out a few examples.

Let's say we want to instruct the computer to remember a number for us. Maybe it's our lucky number. To do this, type the following on a blank line:

luckyNumber = 74

That's it. Whenever this program runs, if you ask it what luckyNumber is, it will say 74. How about our name?

name = "Scrooge McDuck"

There we go, now we are Scrooge McDuck! Notice the quotes above. Names aren't numbers (usually), but a string of text instead. Whenever we want the computer to remember some text, we put that text in quotes.

Variable Declarations

These pieces of data we're storing in our program are called variables. Variables have a name and a value. The act of creating a variable is called a Variable Declaration. You declare them using the general form:

name = value

The left side of the equals sign is the name of the variable, and the right side of the equals is the value given to it. Like our examples, you'll want to name your variables based on what they represent. If you want to store the name of your college for example, collegeName might be a good variable name to use.

collegeName = "Smart People University"

If you want to store the name of your favorite food, favoriteFood might be good. Note: Spaces arent allowed in variable names

favoriteFood = "Super Burrito"

But hmm, what's the point of all of this? You could just as easily type this up in a text document! The difference is, once the computer has some variables in memory, we can do things with them… Let's try some math then!

Addng Variables

Add the following to your code box on a blank line:

johnsMoney = 150
bensMoney = 300
total = johnsMoney + bensMoney

We create variables using the name = value syntax as stated above. This assigns the value to that variable name. If we use these variable names later in the code, the computer substitutes the variable with it's value. So in the line total = johnsMoney + bensMoney

this literally translates to total = 150 + 300

We're also creating a new variable called total here, and its assigned value will be the result of 150 + 300, i.e 450

Why not just write total = 300 + 150? Have you ever read one of those math problems that says something like "Ben has twice as much money as john."? Using variables (and *, the multiplication operator), we can express this more clearly

johnsMoney = 200
bensMoney = johnsMoney * 2

Ah, now no matter what value we give johnsMoney, bensMoney will always be double. Change johnsMoney to 400 and bensMoney will also be changed. If we had done something like

johnsMoney = 200
bensMoney = 400


johnsMoney = 200
bensMoney = 200 * 2

We'd have to update bensMoney anytime we changed johnsMoney to keep accurate with the original math problem. The relationship between John and Ben's money would be less apparent as well.


By the way, I wanted to take a second to note that once a variable has been assigned a value, you can't change it later. For example, you can't do the following:

module Main where
import Prelude

johnsMoney = 100
johnsMoney = 400

The inability to update a variable's value is called Immutability. It might seem limiting but it helps us avoid a lot of problems down the road. But enough about that, lets move forward


Variables store data for us.

Variables are declared using the general syntax name = value

Heres an example of declaring some variables

myVariable = 100

otherVar = "hello"

Self Practice

Create 3 variables to store your name, age, and your most hated dessert! Once you're finished, expand the example answer below to see if what you have looks similar. Who knows, maybe we even hate the same dessert!

module Main where
import Prelude

name = "Vance Palacio"
age = 32
topHatedDessert = "Mint Chocolate"

If what you've done looks similar to the above, and you have no compile errors, then good job! It's ok if you named your variables differently, as long as they make sense!

Let's try a more interesting example