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

Using F# language. Full and complete answers in order to get credit please ,than

ID: 3584949 • Letter: U

Question

Using F# language. Full and complete answers in order to get credit please ,thank you

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

is

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

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

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

2) In this problem and the next, I ask you to analyze code, as discussed in the last section of the Checklist. Suppose we wish to define an F# function to sort a list of integers into non-decreasing order. For example, we would want the following behavior:

We might try the following definition:

Explanation / Answer

1. Following function returns the transpose of the given matrix:

let rec transpose matrix =
match matrix with // assuming that the matrix is a list<list<int>>
| row::rows -> // when the matrix in empty
match row with   
| col::cols ->   
// Take first elements from all rows of the matrix
let first = List.map List.head matrix
// Take remaining elements from all rows of the matrix
// and then transpose the resulting matrix
let rest = transpose (List.map List.tail matrix)
first :: rest
| _ -> []
| _ -> []

2. To sort a list, we can use various sorting algorithms. I'm using Bubble Sort for this answer. The code is as follows:

let swap i j (arr : 'a []) =

let tmp = arr.[i]

arr.[i] <- arr.[j]

arr.[j] <- tmp

let bubbleSort arr =

let rec loop (arr : 'a []) =

let mutable swaps = 0

for i = 0 to arr.Length - 2 do

if arr.[i] > arr.[i+1] then

swap i (i+1) arr

swaps <- swaps + 1

if swaps > 0 then loop arr else arr

loop arr

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote