|
Homework 2 - Scheme
CptS 355 - Programming Language Design Washington State University |
|
|
Due Date:
Friday, Feb. 9, 2007, in class
Fun with ListsThis homework provides some practice in Scheme programming. You can download MIT Scheme from http://www.gnu.org/software/mit-scheme. The DrScheme environment, which you can search for in google, is another, possibly friendlier, scheme implementation. Either system can be installed on most operating systems (eg,. Linux, Unix, Windows). The MIT software is also installed in the Sloan 353 labs. Turning in your workAll homework should be typed, not handwritten.General rulesUnless directed otherwise, you should implement your functions using recursive definitions built up from the basic built-in functions such as CONS, CAR, CDR. You may use the built-in functionequal? to test equality of lists.
Don't use set! and don't PracticeFor practice, but not to turn in, be sure to do the scheme calisthentics from the course resources page.appendDefine a function to append two lists> (append '(1 2 3) '(4 5 6)) (1 2 3 4 5 6) > (append () '(4 5)) (4 5) > (append '(a b) ()) (a b) > (append '(1 (2 3)) '(4 5)) (1 (2 3) 4 5) intersectionThis function should return the intersection of two lists> (intersection '(1) '(1)) (1) > (intersection '(1 2) '(1)) (1) > (intersection '(2 1 2) '((1) 2)) (2)Each value should appear in the output list only once. zipThis function of two lists pairs up corresponding elements of the two lists. If one list is longer than the other, extra elements of the longer list are ignored.> (zip '(1 2 3) '(a b c)) ((1 a) (2 b) (3 c)) > (zip '(1 2) '(a)) ((1 a)) filterThis higher-order function takes as its first argument one-argument function, called a predicate, which returns #t or #f, and as its second argument a list. It returns a list of all elements in the second argument that satisfy that predicate. The elements must appear in the result in the same order that they appear in the original list.> (filter (lambda (x) (= x 1)) '(1 2)) (1) > (filter (lambda (x) (< x 3)) '(1 2)) (1 2)
foldlIn class we talked about a fold function that was right-associative. Implement the analogous function that is left-associative. That is(foldl - 0 '(1 2 3)) computes (((0-1)-2)-3) while the fold as we defined
it in class computes (1-(2-(3-0))). As another example of the difference consider the difference
between
(foldl cons () '(1 2 3)) and (fold cons () '(1 2 3)). Challenge Based on your observations about the behavior of foldl and foldr on these examples, now define append and reverse using foldl and foldr. Hints on working with MIT SchemeThe following are a few tips to help you get started on working with Scheme on the Windows machines in the Sloan 353 lab.
The '1' in the prompt is the REPL level number and is the topmost level of execution. Whenever an error occurs this level number is incremented (implying that a new version of REPL has been started). For example if you type 'hi' at the prompt you would see the following error messages. ;Unbound variable: hi ;To continue, call RESTART with an option number: ; (RESTART 3) => Specify a value to use instead of hi. ; (RESTART 2) => Define hi to a given value. ; (RESTART 1) => Return to read-eval-print level 1. 2 error> As you can see the earlier prompt has been replaced by a new one '2 error>'. This implies that a new REPL has been started over the old one. Also note that the prompt string displays both the current level number and another string 'error' (implying that this REPL is the result of an error). The original REPL is still running in the background and is waiting for you to return to it. You can return to the original REPL by typing (restart 1). For each error in each REPL different restart methods are available. For example when we typed 'hi' at the prompt we had three restart methods available to us (as shown above), they are (restart 1), (restart 2), (restart 3). The (restart 1) method as explained above returns us to the REPL level 1. If another error occurs while you are executing in the REPL level 2, the level number is incremented to 3 and another REPL (level 3) is started over the current one . This can theoretically go on forever. |
|