Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

F# Programming The transpose of a matrix M is the matrix obtained by reflecting

ID: 3873245 • Letter: F

Question

F# Programming

The transpose of a matrix M is the matrix obtained by reflecting M about its diagonal. For example, the transpose of

/ 1 2 3

4 5 6 /

is

/ 1 4

| 2 5 |

3 6 /

An m-by-n matrix can be represented in F# as a list of m rows, each of which is a list of length n. For example, the first matrix above is represented as the list

[[1;2;3];[4;5;6]]

Write an efficient F# function to compute the transpose of an m-by-n matrix:

> transpose [[1;2;3];[4;5;6]];;

val it : int list list = [[1; 4]; [2; 5]; [3; 6]]

Assume that all the rows in the matrix have the same length.

Hints: transpose [] should give an error message --- Use “failwith”

       transpose [[]] is []                // Input argument is a list with one element, the empty list.

Explanation / Answer

Hint: You can make very good use of map here.

Hint: Use inner, transpose, and map.