In the reading, we introduced a score of new functions for processing the basic types of Scheme. Think of them as an essential vocabulary for expressing basic computation in Scheme, similar to the new vocabulary you might encounter when learning a foreign language. However, unlike a foreign language, there isn’t an expectation that you get a deck of flash cards and memorize these function names. Instead, the expectation is that you will eventually memorize these functions by consistently building programs that use these functions, i.e., practice.
To this end, we’ll try to provide concise references to the functions that we introduce in the reading to aid you in your task. Feel free to note the location of these sections and use them to quickly look up the appropriate functions when needed. (Also feel free to write them down on flash cards for easy reference.)
Basic numeric operations: +, -, *, /, quotient, remainder,
expt.
Numeric conversion: floor, ceiling, round, truncate.
Numeric type predicates: number?, integer?.
Constant notation: #\ch (character constants)
Character constants: #\a (lowercase a) … #\z (lowercase z); #\A
(uppercase A) … #\Z (uppercase Z); #\0 (zero) … #\9 (nine);
#\space (space); #\newline (newline); and #\? (question mark).
Character conversion: char->integer, integer->char, char-downcase, char-upcase
Character predicates: char?, char-alphabetic?, char-numeric?,
char-lower-case?, char-upper-case?, char-whitespace?
Character comparison: char<?, char<=?, char=?, char>=?, char>?,
char-ci<?, char-ci<=?, char-ci=?, char-ci>=?, and char-ci>?.
Constant notation: "string" (string constants).
String predicates: string?
String constructors: make-string, string, string-append
String extractors: string-ref, substring
String conversion: number->string, string->number, string->number
String analysis: string-length
String comparison: string<?, string<=?, string=?, string>=?, string>?, string-ci<?, string-ci<=?, string-ci=?, string-ci>=?, string-ci>?
As in the previous lab (and future labs), you will work with a randomly assigned partner, and now we are introducing a starter file.
Download that file and them upload it to scamper. The file itself contains the instructions, so you don’t need to return to the webpage (except for referencing types and names of functions).
Remember our setup for pair programming in this class:
- One person, the driver, is in control of keyboard. They focus on the immediate task of designing and coding a solution, speaking aloud about their design thoughts as they work.
- The other person, the navigator, acts as a reviewer. They observe and think more about strategic architectural issues. They look for potential issues and raise them with the driver. They are also responsible for keeping track of the time spent on the problem.
The first person who arrived at the computer is the A-side. The second person is the B-side.
When you are done, upload your basic-types.scm file to Gradescope.
Here are the ways we tend to think of the four functions:
(floor r) finds the largest integer less than or equal to r. Some would phrase this as “floor rounds down”.
(ceiling r) finds the smallest integer greater than or equal to r. Some would phrase this as “ceiling rounds up”.
(truncate r) removes the fractional portion of r, the portion after the decimal point.
(round r) rounds r to the nearest integer. It rounds up if the decimal portion is greater than 0.5 and it rounds down if the decimal portion is less than 0.5. If the decimal portion equals 0.5, it rounds toward the even number.
> (round 1.5)
2
> (round 2.5)
2
> (round 7.5)
8
> (round 8.5)
8
> (round -1.5)
-2
> (round -2.5)
-2
It’s pretty clear that floor and ceiling differ: If r has a fractional component, then (floor r) is one less than (ceiling r).
It’s also pretty clear that round differs from all of them, since it can round in two different directions.
We can also tell that truncate is different from ceiling, at least for positive numbers, because ceiling always rounds up, and removing the fractional portion of a positive number causes us to round down.
So, how do truncate and floor differ? As the previous paragraph implies, they differ for negative numbers. When you remove the fractional component of a negative number, you effectively round up. (After all, -2 is bigger than -2.2.) However, floor always rounds down.
Why does Scheme include so many ways to convert reals to integers? Because experience suggests that if you leave any of them out, some programmer will need that precise conversion.
This laboratory is based on a similar laboratory from a prior version of CSC 151. At some point, it included problems on lists and files. It no longer does.