Lecture 4
Aligning Language + Mini-Quiz 3
How to login to PollEverywhere!
On your phone / tablet, either scan the QR code here (or up on the screen) or go to pollev.com/baincs111
Step 1. CLICK LOGIN
Step 2. USE @U.NORTHWESTERN EMAIL ADDRESS
Step 3. CLICK LOGIN WITH NORTHWESTERN
Step 4. Follow sign-in steps with MFA
Step 5. You may need to go back to pollev.com/baincs111 after you've setup your account. Make sure to set your NetID as your Screen Name!!!
Step 6. NEW TODAY - you will need to enable location services. If you get a pop-up asking to allow your location to be shared, you'll have to click 'allow.' If you have an issue, please see me at the stage.
How to login to PollEverywhere!
Our “activity url” is pollev.com/baincs111
If you had issues verifying your location on Wednesday…
If your Tutorial assignment on Canvas DOES NOT have a comment saying “Thanks for verifying your location!” then you had an issue.
Logistics and Week Ahead
Ethics Module 1 - Due Wednesday at 1pm - Details on Canvas (signups open at 4pm)
Next Week:
Monday - Iterators! (Ex 2 Posted)
Wednesday - Lambda Abstraction (Pre-Recorded + MQ4) + Tutorial 2
Friday - Conditional Expressions (MQ 5)
The functional model of computation
A function
input(s)
output
FUNCTION
Rules of computation in Racket
What's the difference between these two things?
a-function …
5
-
"Test"
string-append
(a-function …
(+ 5 3)
(- 3 2)
(string-append "oh " "hi mark")
(circle 50 "outline" "green")
A function is a complex piece of data!
A function when called:
The key distinction between a function and expressions is that they can be run/called/executed (they're machines that take inputs to generate outputs)
A String, a number, an image...
A function is a complex piece of data!
A function when called:
The key distinction between a function and expressions is that they can be run/called/executed (they're machines that take inputs to generate outputs)
When Racket reads a function it means it cannot be simplified because there's missing information – the arguments!
A String, a number, an image...
A function is a complex piece of data!
A function when called:
The key distinction between a function and expressions is that they can be run/called/executed (they're machines that take inputs to generate outputs)
When Racket reads a function it means it cannot be simplified because there's missing information – the arguments!
When Racket reads an expression, it can simplify that expression to its value.
A String, a number, an image...
A function is a complex piece of data!
A function when called:
(λ (inputs) (output))
(λ (x) (+ x 1))
A String, a number, an image...
5
"hello"
(- 5 3)
(circle 10 "outline" "green")
Compound functions
(lambda (name1 name2 … namen) output-expression)
(λ (name1 name2 … namen) output-expression)
Remember, functions are just another data object!
You can construct new functions from old code using λ expressions
Note: you type the λ symbol by typing command-\ on Macs or control-\ on Windows
Some New Terminology on Lambdas
> ((lambda (x y) (+ x y))
1 2)
> 3
> (lambda (x y) (+ x y))
Some New Terminology on Lambdas
> ((lambda (x y) (+ x y))
1 2)
> 3
> (define my-add (lambda (x y) (+ x y))
> (my-add 1 2)
Examples of Compound Functions
;; squared: number -> number�;; Compute the square of a number
(define squared� (λ (n) (* n n)))
>(squared 2)
4
> (squared 58.7)
3445.69
;; polynomial: number -> number
;; Compute n2+2n+4
(define polynomial� (λ (n) (+ (squared n)� (* n 2)� 4)))
> (polynomial 32)
1092
the substitution model
The substitution model
Substitution example
(+ 1 (+ 1 2))��
Substitution with compound function
(polynomial 32)
substitute value of polynomial
((λ (n) (+ (squared n)� (* n 2)� 4)))� 32)
replace call with body of function
(+ (squared n) (* n 2) 4)
substitute 32 for n
(+ (squared 32) (* 32 2) 4)
(+ (squared 32) (* 32 2) 4)
What data is encoded here?
What data is encoded here?
What data is encoded here?
Why do we need lambda
Project Bullseye.
Write a function that takes as input a bullseye radius, and makes a 4-circle bullseye where the largest circle is that radius and the colors alternate between blue and green.
Project Bullseye.
;; bullseye: number -> image
;; creates a bullseye shape with the outermost circle the specified size
We'd call your function like this:
(bullseye 100)
higher-order functions
Functions can return other functions
;; add: number -> (number -> number)�;; Make a function called add that �;; adds x to its input
(define add� (λ (x) � (λ (y)� (+ x y))))
Note: the return type is a function that takes a number as input and returns a number, so its type, (number -> number) has its own ->
(add 2)
> ((λ (x) (λ (y) (+ x y))) 2)
is the same thing as…
(λ (y) (+ 2 y))
is the same thing as…
A function that adds 2 to its argument
Functions can return other functions
;; add: number -> (number -> number)�;; Make a function called add that �;; adds x to its input
(define add� (λ (x) � (λ (y)� (+ x y))))
Note: the return type is a function that takes a number as input and returns a number, so its type, (number -> number) has its own ->
((add 2) 4)
(((λ (x) (λ (y) (+ x y))) 2) 4)
((λ (y) (+ 2 y)) 4)
(+ 2 4)
6
Functions can be arguments
;; squarish: picture-func -> image�;; where picture-func is�;; number number string color�;; -> image�;; Makes a square picture using �;; procedure
(define squarish� (λ (func)� (func 10
10 � "outline" � "white")))
(squarish rectangle)
((λ (func)� (func 10 10 "outline" "white")) � rectangle)
(rectangle 10 10 "outline" "white")
—---------------------------------
(squarish ellipse)
((λ (func)� (func 10 10 "outline" "white")) � ellipse)
(ellipse 10 10 "outline" "white")
Or both
(define f (my-compose squared sin))
(define f ((λ (f1 f2) � (λ (arg) � (f1 (f2 arg))))� squared� sin))
(define f ((λ (f1 f2) � (λ (arg) � (f1 (f2 arg))))� (λ (n) (* n n))� sin))
(define f (λ (arg)� ((λ (n) (* n n))� (sin arg))))
;; my-compose: (number -> number)�;; (number -> number)�;; -> (number -> number)�;; Makes a function that runs both its�;; input function
(define my-compose� (λ (f1 f2)� (λ (arg)� (f1 (f2 arg)))))
;; squared: number -> number�;; Compute the square of a number
(define squared� (λ (n)� (* n n)))
Or both
;; my-compose: (number -> number)�;; (number -> number)�;; -> (number -> number)�;; Makes a function that runs both its�;; input function
(define my-compose� (λ (f1 f2)� (λ (arg)� (f1 (f2 arg)))))
;; squared: number -> number�;; Compute the square of a number
(define squared� (λ (n)� (* n n)))
(f 0)
= ((λ (arg)
((λ (n) (* n n))(sin arg)))
0)
= ((λ (n) (* n n))
(sin 0))
= ((λ (n) (* n n))
0)
= (* 0 0)
= 0