Refresher: Algebraic expressions and functions from Math

Algebraic expressions are mathematical expressions that are constructed using

  • numbers

  • variables

  • and operators

For example the expression

\[1 + 2\]

is constructed by using

  1. The number \(1\)

  2. The operator \(+\)

  3. The number \(2\)

Operators like \(+\) take inputs, in the case of \(+\) 2 inputs one to the left of the \(+\) and one to the right, and gives us back (we say return) an answer.

Numbers on the other had do not take any inputs, they just denote a value.

We can also use variables, these are names that we introduce in our expressions and refer to numbers or even other expressions. For example

\[\begin{eqnarray*} x & = & 10 \\ y & = & 20 \\ z & = & x + y \\ \end{eqnarray*}\]

The natural question now is, how do we go about evaluating the preceding 3 equations in order to find out what \(z\) refers to. To do so, we need to know

  1. The rules of evaluation for each operator, i.e., what does \(+\) do when given two numbers

  2. The mapping of each variable used in our set of equations

So, in our example, given that \(x\) is \(10\) and \(y\) is \(20\) we can calculate \(z\) by adding \(20\) to \(10\) to give us \(30\)

Of course there are many other operators in math that we can use, and each operator comes with a set of rules that tell us what the operator does to its inputs. Here are some other, typical, mathemtical operators

  1. \(-\), subtraction, takes 2 inputs like \(+\), but instead of adding one number to the other, we take away one number from the other

  2. \(\times\), multiplication, takes 2 inputs and returns the result of adding the first input to itself exactly the number of times as the second input, i.e., \(2 \times 3\) is the same as adding \(2\) to itself 3 times, e.g., \(2 + 2 + 2\) or to follow our definitions here

    \[\begin{eqnarray*} x & = & 2 + 2 \\ z & = & x + 2 \end{eqnarray*}\]
  3. \(\sqrt{x}\), square root, takes 1 input \(x\) and returns a new number \(y\) such that \(y \times y\) returns \(x\)

We can use more than one operator in our expressions, we already slipped and provided an example above with the explanation of multiplication. We know that we can build large and complicated expressions that use more than one operator and in fact more than one different operator. The question then becomes, which operator are we supposed to work on first? Does the order matter or do we get the same end result regardless of the order of evaluation that we take? We already know that the answer is it depends. We also know that in Mathematics we have a way of grouping sub-parts of an expression (called sub-expressions) in order to indicate the order using parenthesis. For example

\[q = ((1 + 2) - 3) + ((4 - 2) + 10)\]

We match an open parenthesis with a closing parenthesis starting from left and moving to the right. Everything inside the matched parenthesis denotes a sub-expression. So the above expression has the following sub-expressions as denoted by the matching parenthesis

\[\begin{eqnarray*} ((1 + 2) - 3) \\ (1 + 2) \\ ((4 - 2) + 10) \\ (4 - 2) \\ \end{eqnarray*}\]

We can even rewrite our expression by generating variables for each sub-expression and replacing with our new variable name the sub-expression in our equation e.g.,

\[\begin{eqnarray*} w & = & (1 + 2) \\ x & = & (w - 3) \\ y & = & (4 - 2) \\ z & = & (y + 10) \\ q & = & x + y \end{eqnarray*}\]

We know from Mathematics that if we want to evaluate our original \(q\) expression we do so by going from left to right inner-most parenthesized sub-expression first. For each sub-expression that we evaluate we use the result to replace the sub-expression. Here are the steps

\[\begin{eqnarray*} q & = & ((1 +2) - 3) + ((4 - 2) + 10) & & & & \\ q & = & (3 - 3) + ((4 - 2) + 10) & \mbox{replace} & (1 + 2) & \mbox{with} & 3 \\ q & = & 0 + ((4 - 2) + 10) & \mbox{replace} & (3 - 3) & \mbox{with} & 0 \\ q & = & 0 + (2 + 10) & \mbox{replace} & (4 - 2) & \mbox{with} & 2 \\ q & = & 0 + 12 & \mbox{replace} & (2 + 10) & \mbox{with} & 12 \\ q & = & 12 & \mbox{replace} & (0 + 12) & \mbox{with} & 12 \\ \end{eqnarray*}\]

Can you see the relation between the steps that we took to evaluate \(q\) and the set of equations that we generated when we re-wrote \(q\) by introducing new variables for each sub-expression?

Having all these operators in Mathematics is useful but we typically also would like to define our own operations. In fact all the given operations like \(+\), \(\times\) etc. have been defined by mathematicians and because of their common use we all learn these operations early on and consider them as given. For this purpose we use a function which typically has the following form

\[ f(x) = e\]

where

  • \(f\) is the new name or symbol that we are introducing as a new operator, this is the function name.

  • \(x\) is called a formal argument, it is a name that we are giving to the input that our new operator will accept. If we have more than one input we then add a new argument and separate each argument with a comma, i.e., \(f(x,y,x) = e\)

  • the equal sign \(=\)

  • and an expression \(e\), we will refer to this expression as the body of the function, that will use the inputs by referring to their names, and when evaluated will provide the output for our new function \(f\)

  • we say that \(f(x) = e\) is the function’s definition.

Here is the same function definition but annotated with the names that we use for each component

\[ \underbrace{f}_{\text{function name}}(\overbrace{x}^{\text{formal argument}}) = \underbrace{e}_{\text{body}}\]

Let’s define a function for raising a number to a given power. We are going to call this function \(poower\), here is our definition

\[ power(x,y) = \underbrace{x \times x \times \cdots \times x}_{\mbox{ $y$ times }}\]

Notice how \(x\) is used inside the body of the function \(power\) to refer to the functions first input. Let’s decompose \(power\) into its subparts

function name

\(power\)

function arguments

\(x\) and \(y\)

function body

\(\underbrace{x \times x \times \cdots \times x}_{\mbox{ $y$ times }}\)

Now that we have a new function defined how do we use it. We use the function by writing the name of the function and providing inpouts for each of the arguments, for example \(power(2,3)\). We call \(power(2,3)\) a function call and we call the numbers that we are giving to \(power\) inputs.

In the case of an expression that uses a function like \(power\) our process of evaluating the function call to \(power\) we need to take care of passing the inputs as arguments. We take the inputs provided in the function call and match the inputs by position to the argument names given in the function’s definition. This matching gives us a temporary mapping from arguments to inputs for this specfific function call. Now that we have our temporary mapping we then perform a search and replace on the function definition’s body and replace all occurrence of an argument with its mapped input. Once we are done with our search and replace we proceed with the evaluation of the now modified function definition body.

Let’s look at the steps in detail using an example.

We would like to evaluate the following expression \(q = power(2,4)\)

\[\begin{eqnarray*} q & = & power(2,4) & \mbox{ } & \\ q & = & \underbrace{2 \times 2 \times \cdots \times 2}_{\mbox{ $4$ times }} & \mbox{ } & \text{replace $x$ for $2$ and $y$ for $4$ in the function's body} \\ q & = & 2 \times 2 \times 2 \times 2 & \mbox{ } & \text{expand $\cdots$ to get $4$ multiplications of $2$} \\ q & = & 16 & \mbox{ } & \text{evaluation of $\times$} \end{eqnarray*}\]

From Math to Racket

Most languages and especially Racket have their roots in Mathematics. The languages that we will be using in this course have functions, similar to mathematical functions, as the main mechanism to transcribe a computation for the computer to evaluate.

The syntax used in our languages is close but not identical to mathematical equations.

Math syntax

Racket syntax

Comments

\(1\)

1

We type numbers and Racket knows what we mean

\(1 + 1\)

(+ 1 1)

Parenthesis are required and the operator goes first!

\(1 + 1 + 1 + 1\)

(+ 1 1 1 1)

Parenthesis are required and the operator goes first!

\((1 + 2) - (3 + 4)\)

(- (+ 1 2) (+ 3 4))

Nesting of sub expressions is similar but, operators first!

\(x = 2\)

(define x 2)

Special word define allows us to create mappings from variable names to numbers

The languages we are going to use can deal with numbers just like Mathematics. Our languages however also accommodate for other inputs like

  • Strings, we create a string by wrapping the text we want to use as a string with ", e.g. "this is a string"

  • Symbols, we create a symbol by prepending a ' to a word, e.g., 'name. Symbols do not allow spaces like strings and we will later see more on the difference between symbols and strings

  • Booleans, are exactly 2 distinct values, the value #true and the value #false.

All of these are expressions in our language. They are a special sub-group of expressions in that, like numbers, we can create them by typing them in our programs and we can provide them as inputs to a function, and/or, they can be returned as results from calling a function. We refer to this special sub-group of expressions as values and we have a way of identifying them

Values

A value is anything in our program that can be

  1. Given as an input to a function.

  2. Returned as a result of a function call.

  3. Stored (this criterion will make more sense as we progress in the course and introduce more language features)

The Stepper

DrRacket (by extension your computer as well) is a very fast evaluator. The languages that we use allow us to use their syntax in order to write our expressions and our own functions. [1] Once we have written down our expressions we then ask from the computer (in our case DrRacket) to use its predefined rules for evaluation to read in our expressions and evaluate them. DrRacket will then do one of two things

  1. return back a result. The result might be the one we were expecting or not.

  2. return an error because something went wrong while evaluating our expressions.

Let’s try a little experiment.

  1. Take our expression \(q\) from earlier.

  2. Rewrite \(q\) using the syntax that DrRacket understands (remember, parenthesis are required and operators go first)

  3. Type your rewritten expression in DrRacket’s definitions window

  4. Click Run. Did you get an answer? Was it the one you expected?

  5. Let’s try something else, this time click on Step. A new window should appear

This new window is the Stepper Window. The Stepper is part of DrRacket. The Stepper’s job is to evaluate your expression, in the same way that clicking Run will do, but the Stepper does not perform all the evaluation steps in one go. The stepper

  1. stops before each evaluation step

  2. displays the expression before taking an evaluation step (the expression on the left side of the Stepper window)

  3. marks in green the sub-expression that the evaluation step worked on

  4. displays the expression after taking an evaluation step (the expression on the right side of the Stepper window)

  5. marks in purple the result of the evaluation step taken on the previously selected sub-expression (the one on the left in green)

The top left corner of the Stepper window allows you to navigate forward and backwards (like you do on your browser when you want to move forward in your browser history or backwards) in the evaluation steps taken by DrRacket. This is close to what we write when we evaluate our mathematical expressions and we need to show all our steps.

Functions

Our languages in DrRacket allow us to define functions. Let’s write a simple function that takes one argument and adds 2 to it in both mathematical syntax and Racket syntax and then explain in detail how function definitions work in Racket.

Mathematical syntax

Racket syntax

\(add2(x) = x + 2\)

(define (add2 x)
  (+ x 2))

To define a function in Racket we again use the special keyword define but this time the parenthesis look a little weird. Let’s walk through it. Let’s first split our Racket’s function definition in pieces

  1. (define and its matching closing parenthesis on the next line ) we’ve seen before. This is part of using define we need to wrap it with parenthesis

  2. (add2 x) this part of the definition is new. When we use define and immediately after it we have a sub-expression in parenthesis then we are defining a function.

    1. The name of the function is the first word (or symbol) after the open parenthesis, in this example add2

    2. Following the name of the function we then provide our arguments separated by one space, in this example we have one argument x

  3. (+ x 2) this is the body of our function which must be a valid Racket expression.

Great! Let’s use our new function. Paste the definition of add2 in the Definitions Window and click Run. The Interactions window refreshes letting you know that DrRacket has read your code in the Definitions Window and it is now ready to run any expression that you type in the Interactions Window. Let’s try

(add2 3)

What do you get back as the answer?

It is always a good idea to check (we say test) your code once you have it written down. Having to write tests every time in the Interactions Window is a lot of work. There is a better way to write tests. We can use a special function called check-expect. check-expect takes 2 arguments and checks if the two arguments are the same. Let’s use it to write a test for add2.

Write the following in your Definitions Window and click Run

(define (add2 x)
  (+ x 2))

(check-expect (add2 3) 5)

What do you see in the Interactions Window?

Now let’s try this again, this time click Step. What happens now?

Observe that the first evaluation step that the Stepper performed was to work on the sub-expression (add2 3). This is one of the rules of our languages.

When we call a function and we provide the necessary inputs for the function being called, the first step in our evaluation is to make sure that all the expressions passed as inputs to the function are values. This gives us 2 cases

  1. the input is already a value, e.g., a number or a boolean or a string. Then there is nothing to do here.

  2. the input is an expression that is not a value. Then we grab that input and we start evaluating until we get to a value or the evaluation hits an error.

We process all of our inputs, left to right first until they are all value. Once all our inputs are values we then proceed to call our function.

Use the Stepper to step through the evaluation of check-expect.

Let’s intentionally cause an error in our test to see what happens? Let’s change the code in our Definitions Window to be

(define (add2 x)
  (+ x 2))

(check-expect (add2 3) 4)

Click Run. What happens now? Use the Stepper again and see how the evaluation proceeds.

The need for comments

Notice how the documentation for check-expect tells the reader in English what the function does and also provides examples. It is always a good idea to document your code, both for others to read and understand your code but also for yourself when you have to revisit your code.

In Racket (and the languages we are going to use in this class) we can create a comment by typing a semicolon ; this indicates that after the semicolon and until the end of the line, the text is a comment and it does not take part in the evaluation process.

;; add2 takes as input a number and returns the result of add 2 to the number
;; example:
;; (add2 1) => 3

(define (add2 x)
  (+ x 2))

(check-expect (add2 3) 5)

Operations on different values

The languages that we will use come with an extensive library of functions predefined in DrRacket that we can use.

Making decisions

A common pattern when building our expressions to solve a problem is the ability to make a decision based on a condition. Let’s take a simple toy example here. We would like to detect if a given number falls above or below a threshold. Let’s say that our threshold is 100. We would like to have a function that will take as input a number and return #true if the given number is equal to or greater than 100, otherwise it should return #false.

Racket has special syntax on how to make decisions called cond. cond takes a series of question-answer pairs. Each question-answer pair is wrapped in square parenthesis. The evaluation rules for cond are as follows:

  1. We proceed from top to bottom, left to right.

  2. Grab the first question-answer pair. The first element of the pair is the question. This is an expression that when evaluated returns a Boolean. The second element of the pair can be any expression that can return whatever we deem appropriate.

  3. Evaluate the question in our question-answer pair and

    1. if the question evaluates to #true then proceed to evaluate the answer from our question-answer pair. The result of the answer element from our question-answer pair is the result of the whole cond expression.

    2. if the question evaluates to #false, then move down one line to the next question-answer pair and repeat the process starting at step 2.

Let’s write the code to detect if a given number is greater or equal to 100

;; given a number return true if the number is greater or equal to 100
;; else return false
(define (over-threshold? number)
  (cond
    [(< number 100) #false]
    [(>= number 100) #true]))


(check-expect (over-threshold? 100) #true)
(check-expect (over-threshold? 150) #true)
(check-expect (over-threshold? 10) #false)

Here is another function that uses cond can you figure out what it does?

(define (to-letter total)
  (cond
    [(>= 100 total 95) 'A]
    [(>= 94 total 90) 'A-]
    [(>= 89 total 85) 'B+]
    [(>= 84 total 80) 'B]
    [(>= 79 total 75) 'B-]
    [(>= 74 total 70) 'C+]
    [(>= 69 total 65) 'C]
    [(>= 64 total 60) 'C-]
    [(>= 59 total) 'D]))

1. We will cover functions in just a second.