Iteration

Course links

Exercise 1

The rotate-vector! procedure presented in today's reading performs a ``circular right shift'' -- every element in the vector is shifted to the next higher-numbered position, except the last, which is moved to position 0. Develop a Scheme procedure that performs a circular left shift instead, moving every element of a given vector to the next lower-numbered position, except the element in position 0, which is moved to the last position.

Exercise 2

Develop a Scheme procedure that takes two vectors of equal length as arguments and returns a vector of Booleans, each element of which should be #t if the corresponding elements of the given vectors are identical (as determined by eqv?) and #f if they are not.

Exercise 3

Rewrite the definition of the vector-map! procedure, iterating with a do-expression instead of using the named let-expression to manage the recursion.

Exercise 4

Define a procedure Cartesian-square that takes any list ls as its argument and returns a list of pairs that contains every pair that can be formed from elements of ls (repetitions allowed).

> (Cartesian-square '(a b))
((a . a) (a . b) (b . a) (b . b))
> (Cartesian-square '(0 1 0))
((0 . 0) (0 . 1) (0 . 0) (1 . 0) (1 . 1) (1 . 0) (0 . 0) (0 . 1) (0 . 0))
> (Cartesian-square '())
()