CS2322

Laboratory Problem 18

Representing Array Structures as Lists

In this problem we wish to design a data structure which will allow us to represent arrays as lists. This is a generalization of some of the ideas in problems 4, 5 and 6 of Exam III. For example, in problem 4, we represented a rank 2 array (two dimensional) array as a list of equal length lists of items which represent the first row, second row, etc. A rank 3 array would be represented as a list of rank 2 arrays, etc.

When we define a reduction operator, such as that of problem 5, we see that the rank of the result array is one less than the rank of the argument (hence the name reduction for the operator).

One suggestion for a data structure is to build a top-level list which contains the following information:

rank a non-negative integer

shape a list of the sizes of each of the axes of the array

array-list the array in list of lists form

For example, the matrix

1 9 2

4 2 4

would be represented as

(2 (2 3) ((1 9 2) (4 2 4)))

The vector

1 2 3 4 5 6

would be represented as

(1 (6) (1 2 3 4 5 6))

Build a Lisp function, reshape, having form

(define reshape

(lambda (shape-list value-list)

...))

reshape should build the above mentioned data structure. For example,

(reshape '(2 3) '((1 9) (2 4 2))) ==>

(2 (2 3) ((1 9 2) (4 2 1)))

(reshape '(4) (count 10)) ==> (1 (4) (1 2 3 4))

(reshape '(10) (count 4)) ==>

(1 (10) (1 2 3 4 1 2 3 4 1 2))

(reshape '(0) '(1 2 3)) ==> (1 (0) ())

(reshape '(5 0) '(1 2 3) ==> (2 (5 0) (() () () () ()))

Notice that the array-list is always built from the flattened value-list.

Lab 18 Scheme Code
Lab 18-0 Scheme Code
Lab 18-1 Scheme Code
Lab 18-2 Scheme Code