if, when, and cond.
a. Do the traditional lab preparation. That is,
csc151 package.csc151 package with (require csc151).b. Load the list of cities arranged by zip codes.
(define zips (read-csv-file "/home/username/Desktop/us-zip-codes.csv"))
c. Add the following undocumented procedure to your definitions pane.
(define zip-ends-with
(lambda (city three-char-suffix)
(string=? (substring (car city) 2) three-char-suffix)))
d. Figure out what zip-ends-with does. You may also want to add a
note to yourself for when you come back to it later.
e. Create a small subset of the zips data using filter and
zip-ends-with.
(define some-zips
(filter ... zips))
Define and test a Scheme procedure, (report-victory score), that takes
one argument, a real number, and returns the symbol won if score is
positive, the symbol lost if it is negative, and the string tied
if it is zero.
a. Use if for all the tests within your procedure.
b. Use cond for all the tests within your procedure.
In the reading, we started to write a procedure that categorized values. Let’s extend that example.
Write a procedure, (type-of val), that returns
'boolean, if val is a boolean;'character, if val is a character;'number, if val is a number;'procedure, if val is a procedure;'string, if val is a string;'symbol, if val is a symbol; or'miscellaneous, if val is anything else.As you may know, one of the famous riddles of the Sphinx goes something like the following:
What is it that walks upon four legs, then two legs, then three legs?
The answer is, of course, humans.
Write a Scheme predicate, legs, that, given someone’s age, tells how
many legs they walk upon. (You get to choose reasonable ages for these
three phases of life.)
You may recall a previous question that asked for the following:
Define and test a Scheme procedure (report-victory score) that takes
one argument, a real number, and returns the symbol won if score is
positive, the symbol lost if it is negative, and the string tied
if it is zero.
In that problem, you wrote one version that used if and one version
that used cond. However, it is also possible to write report-victory
using only and, or, and not. Try doing so.
Write a procedure, (valid-date? month day), that returns #t if
the numbers month and day describe a valid month and day and false
otherwise. For example,
> (valid-date? 1 30)
#t
> (valid-date? 2 30)
#f
> (valid-date? 9 30)
#t
> (valid-date? 9 31)
#f
> (valid-date? 9 -1)
#f
> (valid-date? 9 250)
#f
a. Write this procedure using if as the primary control structure.
b. Write this procedure using cond as the primary control structure.
c. Write this procedure without if and cond (that is, using and,
or, and, possibly, not).
In the corresponding reading, you saw a procedure that returns “North” for a latitude north of 39.72 and “South” for a latitude south of 39.72.
a. Rewrite that procedure so that it takes a full zip-code entry as input and produces
"Unknown" if there is no latitude."Border" if the latitude is exactly 39.72."North" if the latitude is greater than 39.72."South" if the latitude is less than 39.72.For example
> (categorize-city '("09123" "" "" "Apo" "AE" ""))
"Unknown"
> (categorize-city '("02123" 42.338947 -70.919635 "Boston" "MA" "Suffolk"))
"North"
> (categorize-city '("25123" 38.704805 -81.914474 "Leon" "WV" "Mason"))
"South"
b. Using map, apply this procedure to every element of the
some-zips list you created in the preliminaries.
c. Using map and tally, determine how many cities are in
some-zips are classified in each way.
d. Determine how many cities in zips are classified in each way.
Note that we asked you to try the shorter list first because you should generally experiment with small sets of data to make sure that your procedure is correct before running it on a larger list.
You probably noticed that there are no cities exactly on the border between north and south. Rewrite the procedure from exercise 6 so that it takes a full zip-code entry as input and produces
"Unknown" if there is no latitude."Border" if the latitude is between 39.70 and 39.74."North" if the latitude is greater than 39.74."South" if the latitude is less than 39.70.Redo the remaining steps of exercise 6.
Write a procedure, (relative-to row latitude longitude) that
returns
"N" if the row describes a city that is primarily
north of the location described by latitude and longitude."S" if the row describes a city that is primarily
south of that location."E" if the row describes a city that is primarily
east of that location."W" if the row describes a city that is primarily
west of that location."NE" if the row describes a city that is primarily
northeast of that location."NW" if the row describes a city that is primarily
northwest of that location."SE" if the row describes a city that is primarily
southeast of that location."SW" if the row describes a city that is primarily
southwest of that location."*" if the row describes a city that is close to
that location."?" if the row describes a city with no latitude or
longitude.You may determine your own criteria for these different categories, provided that they are reasonable. For example, a city that is 500 miles west and 1 mile north of a location should be classified as “W” and not “NW”.
Wikipedia tells us that the geographic center of the continental US is at 39°50′N 98°35′W. In the notation we’ve been using, that’s a latitude of about 39.83 and a longitude of about -98.58.
a. Find the zip code closest to that location.
b. Find out how many zip codes are in each direction from that location.
c. Write a procedure, (add-relative-position row), that adds the
relative position of a city to end of the entry for that city.
> (add-relative-position '("25123" 38.704805 -81.914474 "Leon" "WV" "Mason"))
'("25123" 38.704805 -81.914474 "Leon" "WV" "Mason" "E")
> (add-relative-position '("09123" "" "" "Apo" "AE" ""))
'("09123" "" "" "Apo" "AE" "" "?"))
To write the procedure report-victory without if and cond, we
need to take advantage of the way and and or behave. Recall that
and either returns false (if it encounters a false) or the last value
(if it fails to encounter a false value) and that or returns the first
non-false value, if one is there.
Hence, we’ll use an and for each test and group them together within an
or.
The first test looks something like this: (and (> score 0) 'won).
Note that this test returns #f if score is less than or equal to 0
and the symbol won otherwise.
We can then put that in an or expression. Here’s a start.
(or (and (> score 0) 'won)
'something-else)
Now, if score is greater than 0, we get won and
otherwise we get something-else.
The remainder is up to the reader to figure out.