CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
Copying code from others is wrong because:
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
Is wrong, because:
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
software licenses define clear rules on
how you can copy, use, and change other people's code
open source promotes sharing of code
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
#racket lang
*; Version 1:
(* 3.14159 (* 10 10))
*; Version 2:
(* 3.14159 100)
; ^^^- Evaluated (* 10 10)
*; Version 3:
314.159
;^^^^^^- Evaluated (* 3.14159 100)
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
#racket lang
*; Version 1:
(* 3.14159 (* 10 10))
*; Version 2:
(* 3.14159 100)
; ^^^- Evaluated (* 10 10)
*; Version 3:
314.159
;^^^^^^- Evaluated (* 3.14159 100)
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
| $$\big((11 \cdot 15) + (14 + 4)\big) + \Big(\frac{3}{9} - (14 \cdot 3)\Big) $$ |
|
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
(+
(+
* (* 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)))
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
(+
(+
* (* 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
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
What would happen if we call a function using the infix notation?
(3 / 9)
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
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]
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
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.
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
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.
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
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).
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
#lang racket
sin
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
#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.
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
program = #lang racket expression*
expression = value | variable | function-call | $\cdots$
value = number | $\cdots$
function-call = ( expression+ )
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
value = number | boolean | $\cdots$
boolean = #t | #f
#f#f(not e) negates the boolean result of expression e<, >, <=, >=, =To avoid subtle bugs, avoid using non-
#tand non-#fvalues as true. In particular, contrary to C the number0corresponds to true. Tip: There is no numeric inequality operator. Instead, use(not (= x y))
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
and/orexpression = value | variable | function-call | or | and | $\cdots$
or = ( or expression* )
and = ( and expression* )
and (0 or more arguments, 0-arguments yield #t)or (0 or more arguments, 0-arguments yield #f)CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
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.
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
condcond 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
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])
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
A definition binds a variable to the result of evaluating an expression down to a value.
( define variable expression )
#lang racket
(define pi 3.14159)
pi
(* pi 2)
$ racket def-val.rkt
3.14159
6.28318
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
A program consists of zero or more terms.
#lang racket
term*
A term is either an expression or a definition.
term = expression | definition
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
define evaluate to?CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
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)
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
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)
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
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
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
Variables are considered expressions, so the runtime must look up the value bound to a variable as one step of the evaluation.
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
The following is legal Racket code:
#lang racket
(define pi 3.14159)
(* pi 2)
(define + #f)
(+ pi 2)
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
%) in Racket is function moduloequal?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
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
(define n 16)
(cond [(equal? (modulo n 15) 0) "fizzbuzz"]
[(equal? (modulo n 3) 0) "fizz"]
[(equal? (modulo n 5) 0) "buzz"]
[else n])
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
#| How many evaluation steps should we expect? |#
(define x (* 10 2))
(+ x (* 4 2))
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
#| How many evaluation steps should we expect? |#
(define x (* 10 2))
(+ x (* 4 2))
; Step 1: `eval *`
(define x `20`)
(+ x (* 4 2))
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
#| How many evaluation steps should we expect? |#
(define x (* 10 2))
(+ x (* 4 2))
; Step 1: `eval *`
(define x `20`)
(+ x (* 4 2))
; Step 2: `eval define`
; x = 20
(+ x (* 4 2))
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
#| How many evaluation steps should we expect? |#
(define x (* 10 2))
(+ x (* 4 2))
; 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))
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
#| How many evaluation steps should we expect? |#
(define x (* 10 2))
(+ x (* 4 2))
; 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)
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
#| How many evaluation steps should we expect? |#
(define x (* 10 2))
(+ x (* 4 2))
; 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
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
A function declaration creates an anonymous function and consists of:
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
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
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.
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
Racket introduces a shorthand notation for defining functions.
( define (variable+ ) term+ )
#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+ ))
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
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 $$CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
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 $$CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
The homework template is in our Directory page:
(define ex1 'todo)
(define ex2 'todo)
(define ex3 'todo)
;; ...
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro
CS450 ☽ Branching and function definitions ☽ Lecture 2 ☽ Tiago Cogumbreiro