+ All Categories
Home > Documents > Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond...

Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond...

Date post: 31-May-2020
Category:
Upload: others
View: 5 times
Download: 0 times
Share this document with a friend
60
Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1
Transcript
Page 1: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Outline

Helper Functions and Reuse

Conditionals

Evaluation Rules for cond

Design Recipe with cond

Compound Data

1

Page 2: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Designing Programs

Design recipe

As outlined last lecture

2

Page 3: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Designing Programs

Design recipe

As outlined last lecture

Helper functions and reuse

Writing writing a function, consider whether existing functions help

Example: wearing-glasses? uses add-glasses

Look for functions that you wish you had written

Example: same-person-maybe-disguised? needs wearing-beard?

3

Page 4: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Another Example

Write the function bigger-image? which checks whether one image has morepixels than a second image

4

Page 5: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Another Example

Write the function bigger-image? which checks whether one image has morepixels than a second image

; bigger-image? : image image -> bool

5

Page 6: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Another Example

Write the function bigger-image? which checks whether one image has morepixels than a second image

; bigger-image? : image image -> bool

; Returns true if a has more pixels than b

6

Page 7: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Another Example

Write the function bigger-image? which checks whether one image has morepixels than a second image

; bigger-image? : image image -> bool

; Returns true if a has more pixels than b

(define (bigger-image? a b) ...)

7

Page 8: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Another Example

Write the function bigger-image? which checks whether one image has morepixels than a second image

; bigger-image? : image image -> bool

; Returns true if a has more pixels than b

(define (bigger-image? a b) ...)

(bigger-image? ) "should be" true

(bigger-image? ) "should be" false

8

Page 9: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Another Example

Write the function bigger-image? which checks whether one image has morepixels than a second image

; bigger-image? : image image -> bool

; Returns true if a has more pixels than b

(define (bigger-image? a b) (> (* (image-width a) (image-height a))

(* (image-width b) (image-height b))))

(bigger-image? ) "should be" true

(bigger-image? ) "should be" false

9

Page 10: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Another Example

Write the function bigger-image? which checks whether one image has morepixels than a second image

; bigger-image? : image image -> bool

; Returns true if a has more pixels than b

(define (bigger-image? a b) (> (image-size a) (image-size b)))

(bigger-image? ) "should be" true

(bigger-image? ) "should be" false

Wish list: image-size

10

Page 11: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Another Example

Write the function bigger-image? which checks whether one image has morepixels than a second image

; bigger-image? : image image -> bool

; Returns true if a has more pixels than b

(define (bigger-image? a b) (> (image-size a) (image-size b)))

(bigger-image? ) "should be" true

(bigger-image? ) "should be" false

Wish list: image-size

Fullfill wishes by applying the recipe again(exercise for the reader)

11

Page 12: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Reuse

We should be able to use bigger-image? to write the max-image function

12

Page 13: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Reuse

We should be able to use bigger-image? to write the max-image function

; max-image : image image -> image

; Returns a if a has more pixels than b,

; otherwise returns b

(define (max-image a b) ...)

13

Page 14: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Reuse

We should be able to use bigger-image? to write the max-image function

; max-image : image image -> image

; Returns a if a has more pixels than b,

; otherwise returns b

(define (max-image a b) ...)

(max-image ) "should be"

(max-image ) "should be"

14

Page 15: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Reuse

We should be able to use bigger-image? to write the max-image function

; max-image : image image -> image

; Returns a if a has more pixels than b,

; otherwise returns b

(define (max-image a b) ... (bigger-image? a b) ...)

(max-image ) "should be"

(max-image ) "should be"

15

Page 16: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Reuse

We should be able to use bigger-image? to write the max-image function

; max-image : image image -> image

; Returns a if a has more pixels than b,

; otherwise returns b

(define (max-image a b) ... (bigger-image? a b) ...)

(max-image ) "should be"

(max-image ) "should be"

Instead of returning a bool, we need to do one of two things, so we need cond

16

Page 17: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Outline

Helper Functions and Reuse

Conditionals

Evaluation Rules for cond

Design Recipe with cond

Compound Data

17

Page 18: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Conditionals in Algebra

General format of conditionals in algebra:

{answer question

...answer question

Example:

abs(x) = { x if x > 0-x otherwise

abs(10) = 10

abs(-7) = 7

18

Page 19: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Conditionals

General syntax of cond in our language:

(cond [question answer] ... [question answer])

Any number of cond lines

Each line has one question expression and one answer expression

19

Page 20: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Conditionals

General syntax of cond in our language:

(cond [question answer] ... [question answer])

Any number of cond lines

Each line has one question expression and one answer expression

(define (abs x) (cond

[(> x 0) x] [else (- x)]))

(abs 10) "should be" 10

(abs -7) "should be" 7

20

Page 21: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Completing max-image

Use cond to complete max-image

(define (max-image a b) (cond

[(bigger-image? a b) a] [else b]))

21

Page 22: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Outline

Helper Functions and Reuse

Conditionals

Evaluation Rules for cond

Design Recipe with cond

Compound Data

22

Page 23: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Evaluation Rules for cond

First question is literally true or else

(cond [true answer] ... [question answer])

→ answer

Keep only the first answer

23

Page 24: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Evaluation Rules for cond

First question is literally true or else

(cond [true answer] ... [question answer])

→ answer

Keep only the first answer

Example:

(+ 1 (cond [true 1] [false 0]))

→ (+ 1 1) → 2

24

Page 25: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Evaluation Rules for cond

First question is literally true or else

(cond [true answer] ... [question answer])

→ answer

Keep only the first answer

Example:

(- 1 (cond [true 0] [(< 10 12) 10] [(>= 10 12) 12]))

→ (- 1 0) → 1

25

Page 26: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Evaluation Rules for cond

First question is literally true or else

(cond [true answer] ... [question answer])

→ answer

Keep only the first answer

Example:

(* 1 (cond [true 0]))

→ (* 1 0) → 0

26

Page 27: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Evaluation Rules for cond

First question is literally false

(cond [false answer] [question answer] ... [question answer])

(cond [question answer] ... [question answer])

Throw away the first line

27

Page 28: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Evaluation Rules for cond

First question is literally false

(cond [false answer] [question answer] ... [question answer])

(cond [question answer] ... [question answer])

Throw away the first line

Example:

(+ 1 (cond [false 1] [true 17]))

→ (+ 1 (cond [true 17]))

→ (+ 1 17) → 18

28

Page 29: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Evaluation Rules for cond

First question isn’t a value, yet

(cond [question answer] ... [question answer])

(cond [nextques answer] ... [question answer])

where question → nextques

Evaluate first question as sub-expression

29

Page 30: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Evaluation Rules for cond

First question isn’t a value, yet

(cond [question answer] ... [question answer])

(cond [nextques answer] ... [question answer])

where question → nextques

Evaluate first question as sub-expression

Example:

(+ 1 (cond [(< 1 2) 5] [else 8]))

→ (+ 1 (cond [true 5] [else 8]))

→ (+ 1 5) → 6

30

Page 31: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Evaluation Rules for cond

No true answers

(cond) → error

31

Page 32: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Outline

Helper Functions and Reuse

Conditionals

Evaluation Rules for cond

Design Recipe with cond

Compound Data

32

Page 33: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Design Recipe I

Data

Understand the input data: num, bool, sym, or image

Contract, Purpose, and Header

Describe (but don’t write) the function

Examples

Show what will happen when the function is done

Body

The most creative step: implement the function body

Test

Run the examples

33

Page 34: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Examples

When the problem statement divides the input into several categories, test each one

34

Page 35: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Examples

When the problem statement divides the input into several categories, test each one

Example:

Write the function line-part that determines whether a number ison zero, to the left, or to the right on a number line

0

35

Page 36: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Examples

When the problem statement divides the input into several categories, test each one

Example:

Write the function line-part that determines whether a number ison zero, to the left, or to the right on a number line

0

(line-part 0) "should be" ’zero(line-part -3) "should be" ’left(line-part 3) "should be" ’right

36

Page 37: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Design Recipe I

Data

Understand the input data: num, bool, sym, or image

Contract, Purpose, and Header

Describe (but don’t write) the function

Examples

Show what will happen when the function is done

Body

The most creative step: implement the function body

Test

Run the examples

37

Page 38: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Body

When the problem statement divides the input into N categories:

Start the body with a cond expression and N lines

Formulate a question to recognize each category

38

Page 39: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Body

When the problem statement divides the input into N categories:

Start the body with a cond expression and N lines

Formulate a question to recognize each category

Example:

Write the function line-part that determines whether a number ison zero, to the left, or to the right on a number line

39

Page 40: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Body

When the problem statement divides the input into N categories:

Start the body with a cond expression and N lines

Formulate a question to recognize each category

Example:

Write the function line-part that determines whether a number ison zero, to the left, or to the right on a number line

Three cases, so three lines: (define (line-part n) (cond

[(= n 0) ...] [(< n 0) ...] [(> n 0) ...]))

40

Page 41: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Outline

Helper Functions and Reuse

Conditionals

Evaluation Rules for cond

Design Recipe with cond

Compound Data

41

Page 42: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Finding Images

(image-inside? ) → true

42

Page 43: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Finding Images

(image-inside? ) → true

(image-inside? ) → false

43

Page 44: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Image Tests in Conditionals

Now we can combine such operators with cond:

; detect-person : image image image -> image; Returns a or b, depending on which is in i(define (detect-person i a b) (cond

[(image-inside? i a) a] [(image-inside? i b) b]))

(detect-person )

"should be"

44

Page 45: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Finding and Adjusting Images

Suppose we want to write frame-person:

(frame-person )

"should be"

45

Page 46: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Finding and Adjusting Images

Suppose we want to write frame-person:

(frame-person )

"should be"

Need an operator that reports where an image exists

46

Page 47: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Finding an Image Position

find-image : image image -> num num

47

Page 48: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Finding an Image Position

find-image : image image -> num num

Must return a single value

Correct contract:

find-image : image image -> posn

A posn is a compound value

48

Page 49: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Positions

A posn is

(make-posn X Y)

where X is a num and Y is a num

49

Page 50: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Positions

A posn is

(make-posn X Y)

where X is a num and Y is a num

Examples:

(make-posn 1 2)

(make-posn 17 0)

50

Page 51: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Positions

A posn is

(make-posn X Y)

where X is a num and Y is a num

Examples:

(make-posn 1 2)

(make-posn 17 0)

A posn is a value, just like a number, symbol, or image

51

Page 52: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

posn-x and posn-y

The posn-x and posn-y operators extract numbers from a posn:

(posn-x (make-posn 1 2)) → 1

(posn-y (make-posn 1 2)) → 2

52

Page 53: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

posn-x and posn-y

The posn-x and posn-y operators extract numbers from a posn:

(posn-x (make-posn 1 2)) → 1

(posn-y (make-posn 1 2)) → 2

General evaluation rules for any X and Y:

(posn-x (make-posn X Y)) → X

(posn-y (make-posn X Y)) → Y

53

Page 54: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Positions and Values

Is (make-posn 100 200) a value?

54

Page 55: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Positions and Values

Is (make-posn 100 200) a value?

Yes.

A posn is

(make-posn X Y)

where X is a num and Y is a num

55

Page 56: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Positions and Values

Is (make-posn (+ 1 2) 200) a value?

56

Page 57: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Positions and Values

Is (make-posn (+ 1 2) 200) a value?

No. (+ 1 2) is not a num, yet.

57

Page 58: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Positions and Values

Is (make-posn (+ 1 2) 200) a value?

No. (+ 1 2) is not a num, yet.

Two more evaluation rules:

(make-posn X Y) → (make-posn Z Y)when X → Z

(make-posn X Y) → (make-posn X Z)when Y → Z

58

Page 59: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

Positions and Values

Is (make-posn (+ 1 2) 200) a value?

No. (+ 1 2) is not a num, yet.

Two more evaluation rules:

(make-posn X Y) → (make-posn Z Y)when X → Z

(make-posn X Y) → (make-posn X Z)when Y → Z

Example:

(make-posn (+ 1 2) 200) → (make-posn 3 200)

59

Page 60: Outline - cs.utah.edu · Outline Helper Functions and Reuse Conditionals Evaluation Rules for cond Design Recipe with cond Compound Data 1. Designing Programs Design recipe As outlined

More Examples

Try these in DrScheme’s stepper:

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

(posn-x (make-posn (+ 1 2) (+ 3 4)))

; pixels-from-corner : posn -> num(define (pixels-from-corner p) (+ (posn-x p) (posn-y p)))(pixels-from-corner (make-posn 1 2))

; flip : posn -> posn(define (flip p) (make-posn (posn-y p) (posn-x p)))(flip (make-posn 1 2))

60


Recommended