CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

CS450

Structure of Higher Level Languages

Lecture 2: Branching and function definitions

Tiago Cogumbreiro

1 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Academic dishonesty

2 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Plagiarism in University

Copying code from others is wrong because:

  • you do not learn
  • you risk being expelled
  • you are risking the other person being expelled
  • you risk not completing your degree
  • you risk being put on a list of cheaters (other universities may reject your application)
3 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Plagiarism in the Industry

Is wrong, because:

  • it is illegal
  • you risk being dismissed from employment
  • you risk being sued
4 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Copying code (when it is right)

  • software licenses define clear rules on
    how you can copy, use, and change other people's code

  • open source promotes sharing of code

    • attribution is important (unless public domain)
    • good way to land a job
5 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Plagiarism in CS 450

  • student's responsibility to learn the Student's code of conduct
  • we use plagiarism detection (renaming functions is not enough)
  • we compare against solutions from past years (and instructor)
  • be careful when working with others, any code sharing may trigger it
  • the plagiarism detection tool can detect code sharing among students
6 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Evaluating a function call

Evaluation works from left-to-right from top-to-bottom

#racket lang
*; Version 1:
(* 3.14159 (* 10 10))
*; Version 2:
(* 3.14159 100)
;          ^^^- Evaluated (* 10 10)
*; Version 3:
314.159
;^^^^^^- Evaluated (* 3.14159 100)
7 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Evaluating a function call

8 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Evaluating a function call

Evaluation works from left-to-right from top-to-bottom

#racket lang
*; Version 1:
(* 3.14159 (* 10 10))
*; Version 2:
(* 3.14159 100)
;          ^^^- Evaluated (* 10 10)
*; Version 3:
314.159
;^^^^^^- Evaluated (* 3.14159 100)
9 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Arithmetic expressions example

$$\big((11 \cdot 15) + (14 + 4)\big) + \Big(\frac{3}{9} - (14 \cdot 3)\Big) $$
10 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Arithmetic expressions example

$$\big((11 \cdot 15) + (14 + 4)\big) + \Big(\frac{3}{9} - (14 \cdot 3)\Big) $$
(+
  (+
    (* 11 15)
    (+ 14 4))
  (-
    (/ 3  9)
    (* 14 3)))
11 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

A longer example

(+
  (+
*   (* 11 15)
    (+ 14 4))
  (-
    (/ 3  9)
    (* 14 3)))
(+
  (+
    165
*   (+ 14 4))
  (-
    (/ 3  9)
    (* 14 3)))
(+
* (+
*   165
*   18)
  (-
    (/ 3  9)
    (* 14 3)))
(+
  183
  (-
*   (/ 3  9)
    (* 14 3)))
12 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

A longer example

(+
  (+
*   (* 11 15)
    (+ 14 4))
  (-
    (/ 3  9)
    (* 14 3)))
(+
  (+
    165
*   (+ 14 4))
  (-
    (/ 3  9)
    (* 14 3)))
(+
* (+
*   165
*   18)
  (-
    (/ 3  9)
    (* 14 3)))
(+
  183
  (-
*   (/ 3  9)
    (* 14 3)))
(+
  183
  (-
    1/3
*   (* 14 3)))
(+
  183
* (-
*   1/3
*   42))
*(+
* 183
* -125/3)
424/3
12 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Interpreting an error in Racket

What would happen if we call a function using the infix notation?

(3 / 9)
13 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Interpreting an error in Racket

What would happen if we call a function using the infix notation?

(3 / 9)
*; application: not a procedure;
*;  expected a procedure that can be applied to arguments
*;   given: 3
*; [,bt for context]
13 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Interpreting an error in Racket

What would happen if we call a function using the infix notation?

(3 / 9)
*; application: not a procedure;
*;  expected a procedure that can be applied to arguments
*;   given: 3
*; [,bt for context]

Line 1

The subject is application. Application is short for function application, aka calling a function.

The symptom is not a procedure. Something that should be a procedure is not. Recall, procedure = function.

13 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Interpreting an error in Racket

What would happen if we call a function using the infix notation?

(3 / 9)
*; application: not a procedure;
*;  expected a procedure that can be applied to arguments
*;   given: 3
*; [,bt for context]

Line 1

The subject is application. Application is short for function application, aka calling a function.

The symptom is not a procedure. Something that should be a procedure is not. Recall, procedure = function.

Line 2

Calling a function requires a function, but we provided something else.

13 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Interpreting an error in Racket

What would happen if we call a function using the infix notation?

(3 / 9)
*; application: not a procedure;
*;  expected a procedure that can be applied to arguments
*;   given: 3
*; [,bt for context]

Line 1

The subject is application. Application is short for function application, aka calling a function.

The symptom is not a procedure. Something that should be a procedure is not. Recall, procedure = function.

Line 2

Calling a function requires a function, but we provided something else.


Line 3

We see what was given instead (number 3, rather than a function).

13 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Is this example a legal Racket program?

#lang racket
sin
14 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Is this example a legal Racket program?

#lang racket
sin

Yes! sin is a variable, so a valid expression. Hence, Racket just prints what is in variable sin.

$ racket sin.rkt
#<procedure:sin>

Note: In Racket lingo the word procedure is a synonym for function.

14 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Racket specification

program = #lang racket expression*
expression = value | variable | function-call | $\cdots$
value = number | $\cdots$
function-call = ( expression+ )

15 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Logic

16 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Values

  • Numbers
  • Void
  • Booleans
  • Lists
  • …
17 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Boolean, numeric comparisons

value = number | boolean | $\cdots$
boolean = #t | #f

  • False: #f
  • True: anything that is not #f
  • Logical negation: function (not e) negates the boolean result of expression e
  • Numeric comparisons: <, >, <=, >=, =

To avoid subtle bugs, avoid using non-#t and non-#f values as true. In particular, contrary to C the number 0 corresponds to true. Tip: There is no numeric inequality operator. Instead, use (not (= x y))

18 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Logical and/or

expression = value | variable | function-call | or | and | $\cdots$
or = ( or expression* )
and = ( and expression* )

  • Logical-and with short-circuit: and (0 or more arguments, 0-arguments yield #t)
  • Logical-or with short-circuit: or (0 or more arguments, 0-arguments yield #f)
19 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Boolean examples

Operations and/or accept multiple parameters. Rectangle intersection:

(and (< a-left b-right)
     (> a-right b-left)
     (> a-top b-bottom)
     (< a-bottom b-top))

As an example of short-circuit logic, the expression

(or #t (f x y z))

evaluates to #t and does not evaluate (f x y z). Recall that and also short-circuits.

20 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Branching

21 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Branching with cond

cond evaluates each branch sequentially until the first branch's condition evaluates to true.

expression = value | variable | function-call | or | and | cond
cond = ( cond branch+ )
branch = [ condition expression ]
condition = expression | else

Example

If x is greater than 3 returns 100, otherwise if x is between 1 and 3 returns 200, otherwise returns 300:

(cond [(> x 3) 100]
      [(> x 1) 200]
      [else 300])
22 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Creating variables

23 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Variable definition

A definition binds a variable to the result of evaluating an expression down to a value.

( define variable expression )

Examples

#lang racket
(define pi 3.14159)
pi
(* pi 2)
$ racket def-val.rkt
3.14159
6.28318
24 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Revisiting the language specification

A program consists of zero or more terms.

#lang racket
term*

A term is either an expression or a definition.

term = expression | definition

25 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

If everything evaluates down to a value,

then what does define evaluate to?

26 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Void

Definitions evaluate to #<void>, which is the only value that is not printed to the screen.

(define pi 3.14159)      <-- A definition evaluates to --> #<void>

The void value cannot be created directly. Another way of getting a void value #<void> is by calling function (void).

Try running this program and confirm that its output is empty:

#lang racket
(void)
27 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Evaluating variable definition

When we execute a Racket program, we have an environment to bookkeep each variable, that is a map from variable names to values.

*(define pi 3.14159)
(* pi 2)
; pi = 3.14159
*#<void>
;^^^^^- Eval define
(* pi 2)
; pi = 3.14159
*; Prints #<void>
*(* pi 2)
28 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Evaluating variable definition

When we execute a Racket program, we have an environment to bookkeep each variable, that is a map from variable names to values.

*(define pi 3.14159)
(* pi 2)
; pi = 3.14159
*#<void>
;^^^^^- Eval define
(* pi 2)
; pi = 3.14159
*; Prints #<void>
*(* pi 2)
; pi = 3.14159
*(* 3.14159 2)
;  ^^^^^^^- Subst pi
; pi = 3.14159
*6.28318
;^^^^^^- Eval func
; pi = 3.14159
*; Print 6.28318
28 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Do variables evaluate?

Variables are considered expressions, so the runtime must look up the value bound to a variable as one step of the evaluation.

29 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Beware of re-definitions

The following is legal Racket code:

#lang racket
(define pi 3.14159)
(* pi 2)
(define + #f)
(+ pi 2)

Redefinitions lead to subtle errors!

  • Redefinitions produce subtle side-effects and may void existing assumptions
  • As we will see, redefinitions also complicate the semantics and code analysis
30 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Exercises and recap

31 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Exercise: conditionals

  • The modulo operator (%) in Racket is function modulo
  • The equality operator in Racket is equal?

Translate the following code to Racket:

n = 16
if n % 15 == 0:
    return "fizzbuzz"
if n % 3 == 0:
    return "fizz"
if n % 5 == 0:
    return "buzz"
return n
32 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Exercise: conditionals (solution)

(define n 16)
(cond [(equal? (modulo n 15) 0) "fizzbuzz"]
      [(equal? (modulo n 3) 0) "fizz"]
      [(equal? (modulo n 5) 0) "buzz"]
      [else n])
33 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Exercise: evaluation

#| How many evaluation steps should we expect? |#
(define x (* 10 2))
(+ x (* 4 2))

Solution

34 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Exercise: evaluation

#| How many evaluation steps should we expect? |#
(define x (* 10 2))
(+ x (* 4 2))

Solution

5 steps

; Step 1: `eval *`
(define x `20`)
(+ x (* 4 2))
34 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Exercise: evaluation

#| How many evaluation steps should we expect? |#
(define x (* 10 2))
(+ x (* 4 2))

Solution

5 steps

; Step 1: `eval *`
(define x `20`)
(+ x (* 4 2))
; Step 2: `eval define`
; x = 20
(+ x (* 4 2))
34 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Exercise: evaluation

#| How many evaluation steps should we expect? |#
(define x (* 10 2))
(+ x (* 4 2))

Solution

5 steps

; Step 1: `eval *`
(define x `20`)
(+ x (* 4 2))
; Step 2: `eval define`
; x = 20
(+ x (* 4 2))
; Step 3: `eval x`
; x = 20
(+ `20` (* 4 2))
34 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Exercise: evaluation

#| How many evaluation steps should we expect? |#
(define x (* 10 2))
(+ x (* 4 2))

Solution

5 steps

; Step 1: `eval *`
(define x `20`)
(+ x (* 4 2))
; Step 2: `eval define`
; x = 20
(+ x (* 4 2))
; Step 3: `eval x`
; x = 20
(+ `20` (* 4 2))
; Step 4: `eval *`
(+ 20 8)
34 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Exercise: evaluation

#| How many evaluation steps should we expect? |#
(define x (* 10 2))
(+ x (* 4 2))

Solution

5 steps

; Step 1: `eval *`
(define x `20`)
(+ x (* 4 2))
; Step 2: `eval define`
; x = 20
(+ x (* 4 2))
; Step 3: `eval x`
; x = 20
(+ `20` (* 4 2))
; Step 4: `eval *`
(+ 20 8)
; Step 5: `eval +`
28
34 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Function declaration

35 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Function declaration

A function declaration creates an anonymous function and consists of:

  • parameters: zero or more parameters (identifiers, known as symbols)
  • body which consists of one or more terms

When calling a function we replace each parameter defined in the lambda by the corresponding argument. If the number of arguments is not the expected one, then we get an error. The return value of the function corresponds to the evaluation of the last term in the body (known as the tail position).

function-dec = ( lambda ( variable* ) term+)

We can define circumference as a function and parameterize the radius:

#lang racket
(define circumference (lambda (radius) (* 2 3.14159 radius)))
(circumference 2)
$ racket func.rkt
12.56636
36 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Evaluating a lambda

(define circ
 (lambda (radius) (* 2 3.14159 radius)))
(circ 2)
; circ = lambda ...
#<void>
;^^^^^- Eval define
(circ 2)
; circ = lambda ...
; Prints #<void>
(circ 2)
; circ = lambda ...
((lambda (radius) (* 2 3.14159 radius)) 2)
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- Subst circ
; circ = lambda ...
(* 2 3.14159 2)
;^^^^^^^^^^^^^^- Applied func
; circ = lambda ...
12.56636
; circ = lambda ...
; Prints 12.56636

For more information on evaluation, read Section 1.1.5 of SICP.

37 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Function definition

38 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Function definition

Racket introduces a shorthand notation for defining functions.

( define (variable+ ) term+ )

Example

#lang racket
(define (circumference radius)
  (* 2 3.14159 radius)))
(circumference 2)

 

A function definition expects one or more variables (symbols). The first variable is the function variable. The remaining variables are the arguments of the function declaration. The one-or-more terms are the body of the function declaration.

Which is a short-hand for:

( define variable (lambda ( variable* ) term+ ))

39 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Exercise

The McCarthy 91 function was invented by computer scientist John McCarthy to motivate formal verification.

$$M(n) = n - 10 \text{ if } n > 100 \\ M(n) = M(M(n+11)) \text{ if } n \le 100 $$
  • Implement the function in Racket
  • What is $M(99)$?
40 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Exercise

The McCarthy 91 function was invented by computer scientist John McCarthy to motivate formal verification.

$$M(n) = n - 10 \text{ if } n > 100 \\ M(n) = M(M(n+11)) \text{ if } n \le 100 $$
  • Implement the function in Racket
  • What is $M(99)$?

The McCarthy 91 function is equivalent to

$$M(n) = n - 10 \quad \text{if } n > 100 \\ M(n) = 91 \quad \text{if } n \le 100 $$
40 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Homework example

41 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

Template excerpt

The homework template is in our Directory page:

(define ex1 'todo)
(define ex2 'todo)
(define ex3 'todo)

;; ...
42 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

43 / 44

CS450  ☽  Branching and function definitions  ☽  Lecture 2  ☽  Tiago Cogumbreiro

44 / 44
Rotate for a larger view