Skip to main content

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
  • User-defined-procedures

Preliminaries

News / Etc.

  • Happy Labor Day!
  • New places/partners!
  • Quiz 1 returned.

Upcoming Work

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)

  • Women’s Soccer vs. Central, Wednesday at 5: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

  • Les Duke Cross Country Meet, Saturday at 9 a.m., Country Club

Notes on Quiz 1

  • The grade is out of 10.
  • 7 - 1 is not an error. It’s three separate expressions.
  • In contrast, sqrt(9) is an error because 9 is 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 x and 1/2 to the nearest even number.
  • 1/2 and 0.5 are not the same. Why not?
    • One is exact and one is inexact. Decimal points mean “inexact”
  • 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

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.
  • Sam thinks that a writeup should take about 5-10 minutes once you’ve completed the problem.
  • Should we cite the reading?
    It’s nice, but not strictly necessary.

Self checks

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 that decrement, which subtracts one from its parameter, can be found in the library csc151/numbers.
    • (define subtract2 (o decrement decrement))
  • Using section.
    • (define subtract2 (section - <> 2))
  • Using a lambda expression.
    • (define subtract2 (lambda (number) decrement decrement number)) - No
    • (define subtract2 (lambda (number) (decrement (decrement number))) - yes
    • (define subtract2 (lambda (number) (- number 2))) - yes
    • (define subtract2 (lambda (um) (- um 2))) - yes
    • (define subtract2 (lambda (symbol) (- symbol 2))) - yes
    • (define subtract2 (lambda (a) (expt a 2) (- a 2))) - maybe
      • Hmmm … we’re computing the square of a. That seems irrelevant. I don’t know. It seems like it will compute both or give yuou an error or …
      • When you compute a series of values in the body of a lambda, we only get the value of the last one.

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

Why could I not access the eboards over the weekend?
Sam probably broke the site. Send him email next time. If you are afraid to send Sam email, send Anh Thu email. She likes to tell Sam to fix the broken things.
What is the letter grade on the quiz?
Just numeric
Why are there so many results for (reduce - (make-list 5 1))
1 - 1 - 1 - 1 - 1 => 1 - 0 - 1 - 1 => 1 - 0 - 0 => 1
1 - 1 - 1 - 1 - 1 => 0 - 1 - 1 - 1 => -1 -1 -1 => -2 - 1 => -3
1 - 1 - 1 - 1 - 1 => 1 - 0 - 1 -1 => 1 - -1 -1 => 1 - -2 => 3
Help me understand lambda better.
We often say to ourselves “I want a procedure that, given an input, x, computes this formula of x.”
We write the word lambda to mean procedure, the parameter in parens, then the formula. (lambda (x) (formula x))
The lab will help more.

Lab

Do the lab on procedures.

How do I compute the fifth root of x?
(expt x 1/5)
Can I nest within a call to section?
No.
Then how am I supposed to write bound-grade without lambda?
I see three issues in bounding grades, although only two may be obvious.
You compute the maximum of the input value and 0. - One procedure created with section
You compute the minimum of some value and 100. - Another procedure created with section
You apply the second “procedure” to the result of the first. - o
(section max <> 0)
(section min <> 100)
(define bound-grade (o (section max <> 0) (section min <> 100)))
Can we do fancier stuff?
Probably not. I was somewhat blunt in implementing section and o.
section needs the <>’s at the top level
o needs one-parameter procedures.
What about flash cards?
Download Anki onto your computer. (or send me Q/A)
Create flash cards that will help you study for CS.
  • ”(+ 2 3)?” => 5
  • “Add 2 and 3” => (+ 2 3)
  • “Typical lambda form” => (lambda (x) expression)
  • floor vs truncate?” => …
  • Seven parts of algorithms => Basic ops, variables, subroutines, conditionals, etc.
  • Four questions about each new type => …
Pick ten (or more) from the past week (Friday, Monday, Wednesday) and send them to me by Wednesday night at 10:30 p.m.
What’s the lab write up?
Problem 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