Skip to main content

Class 8: Writing Your Own Procedures

Held: Friday, 3 February 2017

We consider new ways to write your own procedures and why you might do so.

Preliminaries

Overview

  • {“Review”=>”Algorithm components”}
  • {“Detour”=>”Sequencing operations”}
  • {“Review”=>”Types”}
  • User-defined-procedures

Updates

News / Etc.

  • Continue partners!
  • Sam’s grading of HW1 is now done! (Our graders get to work on the lab writeups and HW2. They should be done on Sunday.)
  • If there are things I do in class that don’t seem to end up in the eboard, and you’d like them there, please let me know.
  • Lots of talking today (prelims, topics, etc.). That’s okay, we’ve reserved two days for today’s lab.

Reminders

  • I’m always happy to (try to) answer questions via email. There is no need to apologize when sending me questions.
  • We have tutors available Sunday through Thursday evening from 7-10 p.m. in Science 3813/15.
  • Starting this week, we will have mentor sessions on Wednesday and Thursday evenings from 8:00-9:00 p.m. in the CS Commons. Wednesdays will be more Q&A, Thursdays will include sample quizzes.
  • I run review sessions on Thursdays at 9am in this room.
  • We have individual tutors available for those who take advantage of the above and find that it’s not enough.

Upcoming Work

Extra credit (Academic/Artistic)

  • CS Table, Tuesday, 7 Feb 2017. Something on privacy.
  • Thursday extras, Thursday, 9 Feb 2017, 4:15 p.m., Science 3821: Something on computer graphics (visitor from UMN).
  • Scholars Convocation, next Thursday, 9 Feb 2017, 11:00 a.m., JRC 101.

Extra credit (Peer)

  • Swimming and Diving meet 1pm on Saturday.
  • Ritalin Test Squad Improv Troupe is looking for new members and will be holding try outs this weekend. Auditions will be on Saturday Feb 4th, 1-3 pm in The Wall (Bucksbaum 152).
  • Pun Club, Saturday at 4pm in (private).

Good things to do

  • Divest rally Friday at 3pm in ARH 302.

Friday PSA

  • Take care of yourselves.
  • I’ll have new data next week.

Review: Algorithm components

  • What are the components of algorithms?
  • Which of those have we learned so far?
  • What things don’t make sense?

Detour: sequencing operations

  • We noted that the sequence of operations is an important aspect of an algorithms. (You want a jar open before you put the knife in the jar.)
  • Different languages have different approaches to sequencing operations.
  • In Scheme, we often write compound expressions, expressions that include subexpressions.

      (sqrt (+ (square x) (square y)))
    
  • Which is done first, square, +, or sqrt?
    • Scheme (usually) works from the inside-out, so we do the square operations first, then the addition, then the sqrt.
  • Which of the square operations is done first?
    • The Scheme standard says that the interpreter can do them in either order.
  • Do we have other ways of sequencing operations?
    • A series of definitions provides one mechanism for doing so.

Review: Types

  • When we work with data, we should consider the types of the data.
  • You can think of the type of the datum as “what the datum” is (to be distinguished from “what value the datum has”).
  • We’ve already seen a variety of types.
    • Symbols
    • Strings
    • Numbers (and various kinds of numbers)
    • RGB colors
    • Images
    • And more …
  • What type is 3? (Or what types might 3 be?)
  • Yes, some types are contextual.
  • When we think about procedures, we also consider their input types and output types.
  • What type does sqrt take as input?
  • What type does sqrt give as output?
  • What types does compose take as input?
  • What type does compose return?
  • What types does section take as input?
  • What type does section return?

User-defined procedures

  • It’s clear that programmers often want to (and need to) define their own procedures.
  • By “their own procedures”, we mean collections of Scheme commands that are parameterized and referred to be a single name, just like the built-in procedures, such as square and +, or the Mediascript procedures, such as irgb-darker.
  • Procedures take inputs (which we call parameters) and may produce a result.
  • Some procedures modify their parameters:
    • An “open jar” procedure changes the state of the jar.
    • (We’ll see some in Scheme a bit later.*
  • Some procedures create new values, without modifying their parameters:
    • sqrt
  • We will focus on the latter.
  • User-defined procedures can add clarity to a program.
    • Rather than looking at how code does something, the user of a procedure can focus on what the code does.
    • A reader of the program is much more likely to understand a procedure call than the body of the procedures.
  • Programmers can avoid repetitive (and, therefore, error prone) code.
    • Rather than retyping the same code again and again, just changing a few values, a programmer can give a name to the same code.
  • How do you define your own procedures? Using the following template:

    (define your-procedure (lambda (param1paramn) expression1expressionm))

  • For example,

    (define square (lambda (val) (* val val)))

  • You can (and should) document your procedures so that others can understand what they are supposed to do. We’ll come back to this issue soon.
  • When the body of a procedure has multiple expressions (as in the template), only the value of the last expression is returned.
  • When you call a procedure, it’s like you do a define for each parameter as the corresponding sent value.
  • So (square 5) is a lot like

    (define val 5) (* 5 5)

Labs

  • Begin the lab on procedures
  • Be prepared to reflect (e.g., to describe the most important or most confusing thing you dealt with today). (And no, you can’t say “Sam is the most confusing thing I dealt with today.”)