Fundamentals of Computer Science I (CSC-151.02 2000F)


Lab: Mutators

Exercises

Exercise 0: Preparation

If you have not done so already, please scan the reading on mutators for information on the basic mutators, including

Exercise 1: Changing Lists

a. Create four lists using these commands.

(define list1 (list 'a 'b 'c))
(define list2 (list 'a 'b 'c))
(define list3 list1)
(define list4 (cons 'a (cdr list2)))

Execute the following DrScheme commands. Print out all of the lists after each step (see the note on this exercise). Explain what has happened to the lists after each command. Pay particular attention to the difference between the creation of the various lists. You may find it helpful to draw pictures.

b. (set-car! list1 'd)

c. (set-car! (cdr list1) 'e)

d. (set-cdr! list1 '(f g))

e. (set-cdr! list4 list2)

Exercise 2: Looping Lists

a. Execute the following commands. Display the resulting lists.

(define list5 (list 'i))
(define list6 (cons 'h list5))

b. What values do you expect to get for the following commands? Verify your answers through experimentation.

(list-ref list5 2)
(list-ref list6 1)

c. Execute the following command.

(set-cdr! list5 list6)

d. What values do you now expect to get for the following commands? Verify your answers through experimentation.

(list-ref list5 2)
(list-ref list6 1)

e. Draw the picture of the cons-cell structure you created in step c. Does that explain your answer to d?

f. What do you expect to get when you ask for the length of list5? Of list6? Experiment and see.

g. What do you expect to happen when you try to print out list5 or list6? Experiment and see.

Exercise 3: list-set!

Standard Scheme does not supply a list-set! procedure, for replacing the element in a specified position in a given list. Define one, using set-car!. The method should return nothing.

Here is a simple test of list-set!.

> (define stuff (list 'a 'b 'c))
> (list-set! stuff 0 'x)
> stuff
(x b c)
> (list-set! stuff 2 'y)
> stuff
(x b y)
> (list-set! stuff 3 'z)
Can't count, can you?

Exercise 4: Cleaning Up Numeric Vectors

Using vector-map!, define a Scheme procedure convert-negatives-to-zero! that takes any vector of real numbers as its argument and, as a side effect, replaces any negative value in that vector with 0.

> (define example (vector -3.8 17 0.14 -0.14 -113/4))

> (convert-negatives-to-zero! example)

> example
#(0 17 0.14 0 0)

Exercise 5: A Table of Factorials

Using vector-generator, define a procedure factorial-table that takes any natural number as its argument and produces a vector of the specified length, each element of which is the factorial of its position number.

> (factorial-table 8)
#(1 1 2 6 24 120 720 5040)

Exercise 6: Accumulation

Define a Scheme procedure accumulate! that takes two arguments -- a procedure proc of arity 2 and a ``seed value'' seed -- and returns an accumulated procedure that, when invoked, applies proc across a given vector, starting by applying it to seed and the initial element of the vector, then to the result of the first application and the next element of the vector, and so on. Like the accumulation procedures above, the one returned by accumulate! should, as a side effect, replace all the elements of the vector it is given with the intermediate results.

(define accumulate-sum! (accumulate! + 0))

(define accumulate-strings! (accumulate! string-append ""))

> (define powers (make-vector 12 1))

> ((accumulate! (lambda (x y) (* 2 x y)) 1) powers)

> powers #(2 4 8 16 32 64 128 256 512 1024 2048 4096)

Notes

Exercise 1

You may find the following helper procedure useful.

(define showlists
  (lambda ()
    (display "list1: ") (display list1) (newline)
    (display "list2: ") (display list2) (newline)
    (display "list3: ") (display list3) (newline)
    (display "list4: ") (display list4) (newline)))

History

Friday, 10 November 2000


Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

This page may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2000F/Labs/mutators.html

Source text last modified Fri Nov 10 11:30:38 2000.

This page generated on Fri Nov 10 11:30:53 2000 by Siteweaver. Validate this page's HTML.

Contact our webmaster at rebelsky@grinnell.edu