CSci 4651 - Lab 1.

September 5. Due Monday, September 8 at 11:59pm

What to submit and when:

Lab assignment

Problem 1

Define and test function swap. It takes a single argument. If it is an atom (i.e. not a pair), the function just returns the argument. If it's a pair, the function switches the car and the cdr. For instance:


(swap (cons 5 8))
>(8 . 5)
(swap '(2 3 4))
>((3 4) . 2)   ; Why?
(swap 5)
> 5

Problem 2

deep-swap is a recursive version of swap: it recursively swaps all cons-cells in the arguments. For instance:


(deep-swap (cons (cons 3 4) (cons 5 6)))
> ((6 . 5) 4 . 3)

Test this function carefully. What would happen if it's applied to a list? Please test your finidngs and explain the result by drawing the resulting structure.

Try to guess what happens if the function is applied as follows:


(define x (cons 3 4))
(define y (cons x x))
(deep-swap y)

Now try it. Is that what you expected? Can you explain the results?

Problem 3

Type in the following definition:


(define z (cons + '(2 3)))
What is z? Test it. How can we get the result of the expression, given z (i.e. to get the result 5)? Please submit your Scheme code that computes the value of z.


This page is a part of CSci 4651 course web site.