Lecture 4 Slides - Aligning Language

1 of 31

Lecture 4

Aligning Language + Mini-Quiz 3

2 of 31

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.

3 of 31

How to login to PollEverywhere!

Our “activity url” is pollev.com/baincs111

If you had issues verifying your location on Wednesday…

  • try using the PollEverywhere app on your phone
  • If you get stuck in a login loop on iOS, in the app, click on SETTINGS and login there. Then return to the PollEv home screen and enter our activity url (you might have to type it in and hit enter twice).
  • If you do NOT see a green check mark at the top of the screen, you’re not checked-in
  • If you have not configured Chrome to have access to your precise location, it will likely say you’re in an invalid location. If this is the case, and you don’t have access to a phone/tablet, please come physically check-in with me at some point during the lecture.

If your Tutorial assignment on Canvas DOES NOT have a comment saying “Thanks for verifying your location!” then you had an issue.

4 of 31

Logistics and Week Ahead

  • Mini-Quiz 3 today
    • Poll Everywhere for students in-class
    • Canvas Quiz for students completing remotely (due 11:59pm on Sunday)
  • Exercise 1 - Due tonight at 11:59pm (last chance deadline of Sunday)
  • All work that has been submitted (except Ex1) will be graded by 5pm and grades are posted on Canvas (including 0s). If something is wrong you need to see me ASAP.
    • Note: I DO NOT get notifications of Canvas comments left on assignment submissions

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)

5 of 31

The functional model of computation

A function

  • Takes a set of inputs
    • Information encoded in some representation(s)
  • Does some stuff (computes)
  • Generates an output
    • Again, information encoded in some representation
  • Stops

input(s)

output

FUNCTION

6 of 31

Rules of computation in Racket

  • If it’s a constant (a number or string)
    • It’s its own value
    • Return it

  • If it’s a variable name (e.g. a word, hyphenated-phrase, or symbol like +)
    • Look up its value in the dictionary
    • Return (output) it

  • If it has parens (i.e.(a b c …))
    • Check if it’s a special form
      • (define name value)�Update dictionary to list value as definition of name.
      • (λ (input-names …) output-exp)�Makes a new procedure, whose inputs are called input-names and whose output is computed by output-exp.
    • Otherwise, find the values of a, b, c, etc. using these same rules
      • The value of a had better be a function
      • Call it with the values of b, c, etc. as inputs
      • Return its output

7 of 31

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")

8 of 31

A function is a complex piece of data!

A function when called:

  • Takes a set of inputs
  • Does some stuff (computes)
  • Generates an output
  • Returns the output

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...

  • They just exist.

9 of 31

A function is a complex piece of data!

A function when called:

  • Takes a set of inputs
  • Does some stuff (computes)
  • Generates an output
  • Returns the output

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...

  • They just exist.

10 of 31

A function is a complex piece of data!

A function when called:

  • Takes a set of inputs
  • Does some stuff (computes)
  • Generates an output
  • Returns the output

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...

  • They just exist.

11 of 31

A function is a complex piece of data!

A function when called:

  • Takes a set of inputs
  • Does some stuff (computes)
  • Generates an output
  • Returns the output

(λ (inputs) (output))

(λ (x) (+ x 1))

A String, a number, an image...

  • They just exist.

5

"hello"

(- 5 3)

(circle 10 "outline" "green")

12 of 31

Compound functions

(lambda (name1 name2namen) output-expression)

(λ (name1 name2namen) output-expression)

Remember, functions are just another data object!

You can construct new functions from old code using λ expressions

  • name1 name2namen are names (i.e. placeholders or containers) to give to the inputs of the function when it’s called
  • output-expression is an expression to compute the output of the function
  • Inside this output expression we can refer to the inputs by the names we given them in that first part of the lambda expression

Note: you type the λ symbol by typing command-\ on Macs or control-\ on Windows

13 of 31

Some New Terminology on Lambdas

  • We call something an expression if it can be simplified to some concrete value

> ((lambda (x y) (+ x y))

1 2)

> 3

  • This is as opposed to a function which can't be simplified because they have missing information (their arguments)

> (lambda (x y) (+ x y))

14 of 31

Some New Terminology on Lambdas

  • Functions like rectangle, square, +, -, string-append are what we informally call named functions
  • When we create functions with lambda expressions, there's no requirement to give it a name

> ((lambda (x y) (+ x y))

1 2)

> 3

  • When we don't give a function a name, we call these anonymous functions
    • If we only need to use a function once, there's no reason to give it a name.
  • But if we want to be able to re-use it later, we give it a name using define

> (define my-add (lambda (x y) (+ x y))

> (my-add 1 2)

15 of 31

Examples of Compound Functions

  • squared is a compound procedure built from *
  • polynomial is a compound procedure built from * (i.e. multiplication), +, and squared
  • Notice they both use the name n for their argument
    • But it’s okay because they’re local names
    • They each have variables named n, but they’re different variables

;; 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

16 of 31

the substitution model

17 of 31

The substitution model

  • The SICP book teaches a model of how procedure calls operate called the substitution model
  • Based on algebraic simplification
    • Calls to primitive procedures work by replacing the call with the value of its output
    • Calls to compound functions are computed by
      • Replacing the call with the body of the function
      • Replace the argument names with their values
      • So ((λ (n) (+ n 1)) 5)
        • (+ n 1), remembering n is 5
        • (+ 5 1)

18 of 31

Substitution example

(+ 1 (+ 1 2))��

  • (+ 1 3)

  • 4

19 of 31

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)

    • (squared 32)
    • ((λ (n) (* n n)) 32)
    • (* 32 32)
    • 1024
  • (+ 1024 (* 32 2) 4)
  • (+ 1024 64 4)
  • 1092

20 of 31

What data is encoded here?

21 of 31

What data is encoded here?

22 of 31

What data is encoded here?

23 of 31

Why do we need lambda

24 of 31

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.

25 of 31

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)

26 of 31

higher-order functions

27 of 31

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

28 of 31

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

29 of 31

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")

30 of 31

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)))

31 of 31

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