CS2322

Laboratory Problem 26

Working with Matrices

Define a procedure matrix that takes two arguments, m and n, and returns a procedure that takes as its m * m arguments the elements of the m by n matrix it creates. (Hint: Use an unrestricted lambda).

For example:

((matrix 3 4) 5 2 3 7

1 0 4 5

8 3 1 2)

produces a 3 row 4 column matrix.

Next, define a procedure mat+ that takes matrices a and b as arguments and returns their sum a + b . a and b must have the same number of rows and same number of columns. If aij is the element of a with indices i,j and bij is the element of b with indices i,j, then the element of a + b with indices i,j is aij + bij.

Finally, define a procedure matrix-view that takes an m by n matrix as its argument and prints the matrix in the format shown below.

(matrix-view ((matrix 3 4) 5 2 3 7

1 0 4 5

8 3 1 2)) ==>

5 2 3 7

1 0 4 5

8 3 1 2

Next, include a tag, matrix-tag, and generalize the procedure view to display a vector, set, or matrix depending on the type of its argument.

Lab 26 Scheme Code