CSC 151.01, Class 05: Writing your own procedures
Overview
- Preliminaries
- Notes and news
- Upcoming work
- Extra credit
- Sample writeup
- Notes on quiz 1
- Self checks
- Questions
- Lab: User-defined-procedures
Preliminaries
News / Etc.
- Happy Labor Day!
- New places/partners!
- Quiz 1 returned.
- Congratulations to our volleyball team for achieving their first win under our new coach.
Upcoming Work
- Writeup for class 4 due tonight at 10:30 p.m.
- Writeup for class 5 due Wednesday at 10:30 p.m.
- Exercise 5
- To: csc151-01-grader@grinnell.edu
- Subject: CSC 151.01 Writeup 4 (YOUR NAMES)
- Assignment 2 due Tuesday.
- Read: How Pair Programming Really Works
Extra Credit
Extra credit (Academic)
- Rosenfield symposium, this week. (Lots of different events)
- CS Table, Tuesday at noon: Doxxing. (Readings should be outside Prof. Curtsinger’s office. I also brought a few copies.)
Extra credit (Peer)
- Men’s Soccer vs. Westminster, Tuesday at 4:30 p.m., Springer Field
- Women’s Soccer vs. Central, Wednesday at 5:00 p.m., Springer Field
- Women’s Tennis vs. Beloit and Lake Forest, Saturday at 9 am Fieldhouse
- Les Duke Cross Country Meet, Saturday at 9 a.m., Country Club
- Men’s Soccer vs. North Central College, Saturday at 1:00 p.m., Springer Field
- Women’s Soccer vs. University of Wisconsin-Oshkosh, Sunday at 1:00 p.m., Springer Field
Extra Credit (Misc)
- Community Hour (Dialogues Across Difference), Tuesday at 11 a.m. in JRC 101.
- CLS Kick-Off Event, Tuesday at 11 a.m. in “North Campus Grove”.
Other Good Things
Notes on Quiz 1
7 - 1is not an error. It’s three separate expressions.- In contrast,
sqrt(9)is an error because9is not a procedure. - I would call the flour an input to the algorithm and the dough something we name (it’s a value we make along the way).
- Round rounds 1/2 to the nearest even number.
- 1/2 and 0.5 are not the same. Why not?
- 1/2 is exact, 0.5 is inexact
- We distinguish the two by the decimal point.
- Scheme will let you precisely represent some fairly large exact numbers; they are imprecise as soon as you add the decimal point.
- Round rounds “half values” to even numbers to “fairly” distribute the rounding. If you have a large data set, you don’t bias up or down (or at least you hope you don’t).
Sample Lab Writeup
2h
This definition determines if the numerical value of “val” is between
the values of “lower” and “upper.” If “val” is between them,
then the value of “val” is reported. If “val’s” value is greater
than “upper,” the value of “upper” is reported. If “val’s”
value is less than “lower,” then the value of “lower” is reported.
10
> (substring "Department" 0 6)
"Depart"
> (substring "Department" 2 6)
"part"
> (substring "Department" 6 10)
"ment"
> (substring "Department" 3 4)
"a"
> (substring "Department" 0 0)
""
> (string-append (substring "Department" 0 1) (substring "Department" 7 10))
"Dent"
> (string-append (substring "Department" 3 4) (substring "Department" 2 10))
"apartment"
We referenced the Racket Language Manual, section 4.3 “Strings”
(https://docs.racket-lang.org/reference/strings.html), and section 13.5
“Writing” (https://docs.racket-lang.org/reference/Writing.html).
- The output was not necessary, but is helpful.
- There are many ways to describe the bounding. One I also liked was “This definition gives the number closest to the defined value within the defined upper and lower limits.”
Self checks
Questions
- I did the self check (or not)
- I feel bad that I did not do the self check
- I will do better next time
Check 1: Subtracting two
Give three ways to define a procedure, subtract2, that takes a number
as input and subtracts 2 from that number.
- Using
o. Note thatdecrement, which subtracts one from its parameter, can be found in the librarycsc151/numbers.(require csc151/numbers)(require csc151/hop)(define subtract2 (o decrement decrement))
- Using
section.(define subtract2 (section - <> 2))
- Using a lambda expression.
(define subtract2 (lambda (number) (- number 2)))(define subtract2 (lambda (um) (- um 2)))
- When choosing names, we should um choose names that make sense to the human reader. We should um also choose different names for different things.
- Comments also help the human reader.
Check 2: Bounding values
You may recall that we used the following expression to bound
a value between lower and upper.
(min (max val lower) upper)
Write a procedure, bound-grade, that takes a real number as input
and bounds it between 0 and 100.
Questions
Please explain what is happening in the half a complex number example.
(define half (section / <> 2))
> (half 4)
2
> (half 4+5i)
2+5/2i
> (half 5i+4)
; same thing
; error? <--- This one
The issue is that Complex numbers in scheme must be written a+bi
Lab
Lab on procedures. “Let’s do this!”
- How do I compute, say, the cube root of 5?
(expt 5 1/3)- Why can’t I write nested definitions with
section? - Because
sectionis stupid. Accept the limitations of the system. - How do I generalize my instructions to compute the geometric mean?
- See
average. - Help! I can’t figure out how to write
bound-grade. - There are two steps in the main procedure: compute the max of the value and 0, then compute the min of that result and 100.
- Write a procedure (or expression) that takes one input and computes the maximum of that input and 0. You can do that with section.
- Write a procedure that takes one input and computes the minium of its input and 100. You can also do that with section.
- Tie those things together so that the output of the first procedure
is the input to the second procedure. Use
o. - Does it matter which order we do the two operations in bounding?
- Nope.
- What is our lab writeup?
- Exercise 5
- Hints for filtering out negative numbers?
- Add a zero to the list
- Put the list in order
- Find the index of the zero
- Remove everything before and including that index