ICS H21 • UC IRVINE • DAVID G. KAY • FALL 2008

Sixth Homework


This assignment is due in lab on Monday, November 3. If you haven't already, change your language level to Intermediate Student with Lambda before you begin.

(1) Chapter 19 starts the book's discussion of higher-order functions (functions as arguments). Look at exercises 19.1.1 and 19.1.2; they should be easy for you now, so you don't have to write down the answers.

(2) Chapter 20 continues with higher-order functions. Do exercises 20.1.3, 20.2.1, 20.2.2, 20.2.3, and 20.2.4.

(3) Chapter 21 of the HtDP text starts with an unusually clear design recipe for abstracting, or refactoring, functions. Do exercises 21.1.1., 21.1.2, 21.2.2 (pick just one of the three parts), 21.2.3.

(4) Chapter 22 of HtDP has a section that introduces graphical user interfaces (GUIs). We may see an example of web interfaces later on; we won't be doing GUIs explicitly. But if you're intrigued with the GUI-building tools in DrScheme, note that the gui.ss teachpack works differently from the description in section 22.3 of the printed text. You can look up the current gui.ss teachpack documentation in the Help Desk. (DrScheme also has a full-blown set of GUI operations, but they're not in the student languages.)

(5) We're not doing Chapter 23. It's interesting, and functional programming is beautifully suited to mathematical problems, but there are only so many hours in the quarter. If you're interested, come back to this chapter over the winter break.

(6) Intermezzo 4 (Chapter 24) talks about lambda, which you know already. Do exercises 24.0.8 and 24.0.9.

Take a look back at the discussion of the function reduce in section 21.5. (Reduce is sometimes called accumulate or foldr, sometimes with the arguments in a different order. In DrScheme, the predefined function is named foldr.) Using foldr/reduce, map, and filter, you can define many powerful operations very compactly, without explicit recursion. For example, suppose we have a list of restaurant (rrant) structures as we've used in class before; call that list RL. To produce a list of the names of the cheap Thai restaurants in RL, we only need to say

(map rrant-name (filter cheap? (filter Thai? RL))).

To calculate the average price of the cheap Thai restaurants, we can say

(local ((define cheap-thai-restaurant-prices
                (map rrant-price (filter cheap? (filter Thai? RL)))))
  (/ (foldr + 0 cheap-thai-restaurant-prices)
     (length cheap-thai-restaurant-prices)))

(In the above example, note that using the local expression saves us from computing the list of prices two separate times.) If you have trouble figuring out how these expressions work, first make sure you understand map, filter, and foldr individually (look at their "contracts") and then look at what each part of the expression returns, starting from the inside out.

Do each of the following problems. Be aware, also, that problems like these are likely to show up on exams.

(6.1) Write the function convert-to-1 that takes one argument (of any type) and returns the number 1, no matter what the argument is. Next, use map and convert-to-1 to define the function list-of-ones that takes a list of items and returns a list of 1s that's the same length as its argument. Finally, use foldr and list-of-ones to rewrite the last line of the average-price code above without using length. We did this in class; now you should be able to do it, too.

(6.2) What is the result of evaluating each of these expressions?

(6.3) Assume you have a function (interval a b) that returns a list of all the integers between a and b, inclusive (so that (interval 5 10) would return (5 6 7 8 9 10)). (Re-)write the function factorial using foldr (and interval), without any explicit recursion.

(6.4) Now, think back to the restaurant collection program and assume we have a list (called RL) of the restaurant objects as described there. For each of the following expressions, describe in one English sentence what value it returns. Don't just say, "It does a foldr of plus and zero to a map of ... ;" give a description of what the expression means, something you could put in a software catalog so that a prospective buyer could find what he or she wanted.

(6.5) Using map, filter, and foldr, write an expression to return each of the following values without using explicit recursion:

(7) Chapter 25 talks about recursive algorithms that don't follow a conventional template or formula. Chapters 26, 27, and 28 continue this theme. You can certainly skip sections 27.3, 27.4, and 27.5. The discussion of backtracking in Chapter 28 is something you'll see in ICS H23.

(8) All the programs we've written so far have done their work, displayed their results, and quit without leaving a trace. Now it's time to learn how to read data from a file and write results back to a file so we can maintain information more permanently, even when our program isn't running. For a programming language to enable file input and output, it must work with the operating system (or multiple operating systems) to handle a variety of issues involving different user interfaces, different ways of storing data in files, different naming conventions for files, and different hardware devices. Because there are many options, there are details to consider when we work with files that we haven't had to worry about so far.

There are three main approaches to reading from and writing to files of text. (Reading and writing data directly in the internal form the computer uses to store it in memory is yet another approach, one we'll skip over for now.)


Based in part on ICS H21assignments and exams by David G. Kay from Fall 2001; modified by David G. Kay with material from Informatics 41, Fall 2004- Fall 2008.


David G. Kay, kay@uci.edu
Friday, October 31, 2008 1:08 PM