Giving Things Names


Referring to things by names

In both mathematics and life in general, it is convenient to have names for things. It would certainly be awkward to communicate if we did not have words and proper names for commonly used things. In mathematics, you solve word problems by choosing variable names to refer to things in the problem. For example, you might say that name s stands for "the speed of train A".

In this chapter we look at how you can give something a name in Cinnameg.


The form of a name

In mathematics you usually choose single-letter names, such as x and y, for variables. But in a programming language, we need to allow longer names. You can use any sequence of one or more letters for a name. For example, you can call something giraffe.

You can also include digits (0, 1, 2, 3, 4, 5, 6, 7, 8 and 9) in names, provide the name begins with a letter. For example, x1 and horse52 can be used as names.

The case of a letter indicates whether the letter is upper case, such as R, or lower case, such as r. Both Cinnameg and Java pay attention to case. That means that R and r are considered different letters. If you decide to call something giraffe, then keep calling it exactly giraffe, not Giraffe or GIRAFFE.

In Cinnameg, only use names that start with a lower case letter.

Names are not allowed to contain spaces. If you want to use a multi-word name, a common convention is to capitalize the first letter of all but the first word. For example, you might call something theTallGiraffe, or myFavoriteNumber. The choice of what to call something is yours. Just be sure to decide exactly how to spell the name, including which letters are capitalized, and stick with that decision.


Choosing sensible names

Remember that we want to strive for simplicity and elegance. You can go a long way toward that goal by choosing names for things that are sensible and informative. Suppose that you have a name that refers to the length of a word. (Just how you get the length of a word is something that we will see later.) Here are some good and bad choices for that name.

If you want to name the speed of train A, choose a name such as speedOfTrainA.

We will encounter quite a few cases where we are not really sure what something is, and short names such as x and y are appropriate. In those cases, short and simple names are the best choice.


Using Let

In Cinnameg, you give a name name to the value of expression e by writing

  Let name = e.
For example, writing
  Let wolf = 9 + 1.
makes wolf name the number 10. Notice that period at the end. It is very important to write the period. Think of a Let phrase as a sentence, and the period is the end of the sentence. The compiler will be very unforgiving if you forget the period.

Once you have defined what a name refers to, you can use that name in expressions. For example,

  Let x = 50.
  Let y = 2*x + 1.
makes x a name for 50 and y a name for 101.


Lines in programs

Both Cinnameg and Java allow you to break a line whereever you want to. For example, a single let phrase can extend across more than one line if you need it to. For example,

  Let numberOfApples = (100000 * 43 + 22)
                       `div` (80 + 1).
illustrates a two-line let. The end of the line is treated like a space. Notice how the second line is indented. There is a lot more to say about indentation, but for now just use the example.


Statements

In programming language terminology, a phrase such as

  Let r = 20.
is called a statement. In particular, we call it a Let-statement. In English, a statement states a fact. But in programming languages, the term statement has come to mean the same thing as a command. A statement tells the program to do something. For example, statement
  Let x = r + 1.
says to computer the value of r + 1, and then to make x name the result.


Problems

  1. [solve] Is theDog an allowed name?

  2. [solve] Is one2three an allowed name?

  3. [solve] Is my dog an allowed name?

  4. [solve] How would you give compute the value of expression 123456789 `mod` 23 and call the result rem?

  5. [solve] Is statement

      Let 8 = r.
    
    allowed?

  6. [solve] In programming language terminology, what does the term statement mean?

  7. [solve] Can a statement be more than one line long?


Summary

A name can contains letters and digits, provided it starts with a letter. A name cannot have any spaces in it.

The case of letters matters. Names Q and q are not the same, and cannot be used interchangeably.

In Cinnameg, it is important to choose names that start with lower case letters.

A statement is a command. It tells the program to do something.

A Let-statement computes a value and gives it a name. For example,

  Let myAge = 22.
says that myAge names value 22. When you use myAge, you are using 22. The name is on the left-hand side and the value (or expression that computes the value) is on the right-hand side of =. There must be a period at the end.


Review

Use parentheses to group things in an expression, but not square brackets or curly braces.

If you encounter mistakes that you cannot understand, ask questions.