Functions and procedures Rules of Processing Announcements In - - PowerPoint PPT Presentation

functions and procedures rules of processing announcements
SMART_READER_LITE
LIVE PREVIEW

Functions and procedures Rules of Processing Announcements In - - PowerPoint PPT Presentation

Functions and procedures Rules of Processing Announcements In Gradescope, change your name to be your Banner ID Without this, we cannot grade your homework anonymously, so we will not grade it at all. Ways to describe sets: Restriction


slide-1
SLIDE 1
slide-2
SLIDE 2

Functions and procedures Rules of Processing

slide-3
SLIDE 3

Announcements

  • In Gradescope, change your name to be your Banner ID
  • Without this, we cannot grade your homework anonymously,

so we will not grade it at all.

slide-4
SLIDE 4

Ways to describe sets: Restriction

  • Restriction: You say “set consists of all elements of set that

have such-and-such a property.”

  • Digression: We often use the name to denote the set of

natural numbers, i.e.,

  • Example of restriction:
  • Fancy-pants math way of writing that:

The vertical bar is read “such that.”

slide-5
SLIDE 5

Restriction: why we care

slide-6
SLIDE 6

Lecture recorded; see course website.

slide-7
SLIDE 7

Functions

From teacherspayteachers.com, mathinsight.org,

  • Key idea: it’s machine-like!
  • Put in the same input twice in a row, you’ll get the same output!
slide-8
SLIDE 8

T ypical high-school math description

  • f a function
slide-9
SLIDE 9

More sophisticated function- descriptions (math, then Racket)

slide-10
SLIDE 10

Naming of parts

slide-11
SLIDE 11

Good habits

slide-12
SLIDE 12

Ways to describe functions

  • Algebra
  • “By cases”
  • “tabular”
slide-13
SLIDE 13

Algebraic description examples

slide-14
SLIDE 14

“by cases” example

  • A classic example:
  • A silly example
  • Notes
  • Both of these have multi-letter names, unusual in math,

common in programming

  • The ”cases” should be mutually exclusive (nothing should fall

into more than one)

  • In the rare cases where something fjts in two cases, the answers

produced must be the same

slide-15
SLIDE 15

T abular example

  • T

abular just means “expressed using a table”

  • It’s really an example of a by-cases function

In tabular form:

z q(z) 1 11 2 5 4

slide-16
SLIDE 16

Activity

slide-17
SLIDE 17

Lecture recorded; see course website.

slide-18
SLIDE 18

“Functions” in Racket

  • I’ll call these “procedures” --- the computational analog of functions
  • Created (for now) with a new kind of defjnition

(define (f x) (* x x))

  • You can tell it’s difgerent from the defjnitions we’ve seen before
  • the second item isn’t a name
  • The overall ”shape” is

<fdef> := (define (<name> <name>) <expr>)

  • The fjrst <name> (f, in our example) becomes the name of a new

function.

  • The “argument” of that function is given by the second name (x, in our

example)

  • The “body” of the function is the <expr> part
slide-19
SLIDE 19

Revised: “Functions” in Racket

  • The overall ”shape” is

<fdef> := (define (<name> <name>*) <expr>)

  • The fjrst <name> becomes the name of a new function.
  • The “arguments” of that function are given by the

second, third, fourth… names

  • A function is allowed to have no arguments at all
  • we’ll never need such a thing in Racket; useful in ReasonML
  • The “body” of the function is the <expr> part
slide-20
SLIDE 20

What to do with a function when you’ve defjned it

(define (f x) (* x x)) (f 12) => 144

  • Roughly speaking, you say “make x be 12; then evaluate

the body, (* x x), to get 144”

  • More formally, you apply the “rules of processing” – later

today or Friday.

  • Quick sanity check: In (define (add x y) (+ x y)),

what is the name of the function we’ve defjned? What are the names of its arguments?

  • Function-name: add; argument names: x, y.
slide-21
SLIDE 21

How we defjne functions in CS17

  • A very specifjc procedure, in a particular order
  • T

akes about 2 minutes per function, total

  • Helps you get started when you haven’t got a clue what

to do!

  • This “Design Recipe” is due to Felleisen et al (including

my colleague Shriram Krishnamurthi), and has been tested on thousands of students learning Racket

  • I use it every time I write a program
  • T
  • day: simple version; will evolve slightly during

semester

slide-22
SLIDE 22
  • Missing picture of rectangular fjeld surrounded by

fenceposts

slide-23
SLIDE 23

Problem statement (short form)

The total number of posts is . Write a procedure, count-posts that takes in two positive integers and , and produces the integer number of fenceposts required to surround a property that's posts wide and posts deep.

slide-24
SLIDE 24

Design recipe, step 1: Data defjnition

  • What kind of data does this procedure work on?
  • Pieces of property?
  • integers?
  • positive integers?
  • For this part, for CS17, for now, the answer will always be one
  • f a very few data types:
  • num
  • string
  • bool
  • This part of recipe will change somewhat, soon.
slide-25
SLIDE 25

;; Data Definition ;; num:

slide-26
SLIDE 26

Design recipe, step 2: Example data

  • For the data type chosen, give some examples.
  • If your actual data is limited in some way, it’s wise to

stick to examples in that limited set, but you don't have to if that’s inconvenient

  • Example: social security numbers: maybe you don’t know a

particular sequence of nine digits that IS an SSN!

  • Example: you’re planning working with positive

integers, but the data type is ”num". You might pick 2, 11, 42 as examples.

slide-27
SLIDE 27

;; Data Definition ;; Example data: ;; num: 0, 6, 41, 7.2

slide-28
SLIDE 28

Design recipe, step 3: T ype- signature

  • Just as in math we write things like , we do the red part

in our procedures.

  • In this case, we take in two numbers, and produce an

number.

slide-29
SLIDE 29

;; Data Definition ;; Example data: ;; num: 0, -6, 41, 7.2 ;; ;; count-posts: num * num -> num

slide-30
SLIDE 30
  • In num * num -> num , the "*" means there are two

arguments, both nums.

  • If we had num * string * bool, there would be three

arguments: a num, a string, and a bool.

  • The only things allowed in this section right now are

num, string, bool, although this list will grow

  • These are the “data types” used in Racket
slide-31
SLIDE 31

Design recipe, step 4: Call structure

  • The "call structure" is the part of a procedure defjnition

before the body.

  • In (define (f x) (* x x) ), the red part is the "call

structure"

  • When we say to write out the call-structure, we actually

mean to write

(define (f x) ...)

  • That's not legal Racket, but we'll soon replace the "…"

part.

slide-32
SLIDE 32

;; Data Definition ;; Example data: ;; num: 0, -6, 41, 7.2 ;; ;; count-posts: num * num -> num (define (count-posts width-count depth-count) . . .)

slide-33
SLIDE 33

Call structure

  • The procedure name (count-posts) was given in the

problem description.

  • The argument-names were given in the problem

description.

  • This step is almost mechanical!
slide-34
SLIDE 34

Design recipe, step 5: Input-output specifjcation

  • Write a comment describing the inputs and outputs of the

procedure.

  • One line for each argument, using the name of the

argument, and describing its role

  • An opportunity to restrict the inputs to a smaller set than the

stated domain

  • Example: input: count, a positive integer indicating how many cars there

are

  • Example: input: state-name, a string containing the two-letter

abbreviation of some US state, written in capital letters, such as "MA" or "RI" or "AK".

  • One line for the result that's computed
slide-35
SLIDE 35

;; Data Definition ;; Example data: ;; num: 0, -6, 41, 7.2 ;; ;; count-posts: num * num -> num ;; inputs: ;; width-count, an integer, the number of posts along one ;; side of the property, at least 2. ;; depth-count, an integer, the number of posts along the perpendicular ;; side of the property, also at least 2. ;; output: the total number of posts needed to fence in the ;; property, an integer. (define (count-posts width-count depth-count) . . .)

slide-36
SLIDE 36

Design recipe, step 6: test-cases/examples

  • Label a section for test-cases
  • Write several tests cases
  • Explore "edge cases" of the domain
  • Explore "generic cases" of the domain
  • For "positive integers", an edge-case is "1", because if you move one

step further left, you're at 0, which is no longer positive.

  • For "integers between 1 and 100, inclusive", both 1 and 100 are

edge cases.

  • For small fjnite sets, like "four amino acids in DNA, A, C, T, G", test

all of them.

  • For this problem: width and depth 2 are edge cases.
slide-37
SLIDE 37

;; Data Definition ;; Example data: ;; num: 0, -6, 41, 7.2 ;; ;; count-posts: num * num -> num ;; inputs: ;; width, an integer, the number of posts along one ;; side of the property, at least 2. ;; depth, an integer, the number of posts along the perpendicular ;; side of the property, also at least 2. ;; output: the total number of posts needed to fence in the ;; property, an integer. (define (count-posts width depth) . . .) ;; test cases for count-posts (check-expect (count-posts 2 2) 4) (check-expect (count-posts 2 5) 10) (check-expect (count-posts 7 2) 14) (check-expect (count-posts 5 8) 22)

slide-38
SLIDE 38

(check-expect (count-posts 2 2) 4)

  • Special feature of CS17 Racket/DrRacket
  • Says “If I process with (count-posts 2 2) DrRacket, I expect the result to be

4

  • If that turns out to be the case, then check-expect does nothing – it produces

no output

  • If the two things don’t match, then check-expect produces a warning

message saying so!

  • That gives you a failure case that you can use to debug (i.e., fjx) your

program.

slide-39
SLIDE 39

Design recipe, step 7: write the program

slide-40
SLIDE 40

;; Data Definition ;; Example data: ;; num: 0, -6, 41, 7.2 ;; ;; count-posts: num * num -> num ;; inputs: ;; width, an integer, the number of posts along one ;; side of the property, at least 2. ;; depth, an integer, the number of posts along the perpendicular ;; side of the property, also at least 2. ;; output: the total number of posts needed to fence in the ;; property, an integer. (define (count-posts width-count depth-count) (- (+ (* 2 width-count) (* 2 depth-count)) 4) ) ;; test cases for count-posts (check-expect (count-posts 2 2) 4) (check-expect (count-posts 2 5) 10) (check-expect (count-posts 7 2) 14) (check-expect (count-posts 5 8) 22)

slide-41
SLIDE 41

Design recipe, step 8: run the program!

  • If all goes well, you'll see "All 4 tests passed!"