Functions and procedures Rules of Processing Announcements In - - PowerPoint PPT Presentation
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
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
- 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.”
Restriction: why we care
Lecture recorded; see course website.
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!
T ypical high-school math description
- f a function
More sophisticated function- descriptions (math, then Racket)
Naming of parts
Good habits
Ways to describe functions
- Algebra
- “By cases”
- “tabular”
Algebraic description examples
“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
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
Activity
Lecture recorded; see course website.
“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
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
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.
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
- Missing picture of rectangular fjeld surrounded by
fenceposts
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.
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.
;; Data Definition ;; num:
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.
;; Data Definition ;; Example data: ;; num: 0, 6, 41, 7.2
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.
;; Data Definition ;; Example data: ;; num: 0, -6, 41, 7.2 ;; ;; count-posts: num * num -> num
- 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
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.
;; Data Definition ;; Example data: ;; num: 0, -6, 41, 7.2 ;; ;; count-posts: num * num -> num (define (count-posts width-count depth-count) . . .)
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!
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
;; 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) . . .)
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.
;; 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)
(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.
Design recipe, step 7: write the program
;; 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)
Design recipe, step 8: run the program!
- If all goes well, you'll see "All 4 tests passed!"