A recursive transpose removes the first column, then transposes the remaining columns:The internal recursive calls eventually reach rows of length zero, even though the original input has positive dimensions; the second equation handles this terminating case. Rectangularity ensures that the rows become empty together, so no
transpose :: Matrix a -> Matrix a
transpose [] = []
transpose ([] : _) = []
transpose rows = map head rows : transpose (map tail rows)head or tail is applied to an empty row in the third equation. The output is an matrix with entry . The element type is unrestricted.Build columns by prepending the first row's entries to the columns obtained from the remaining rows:The singleton case turns each entry into a one-element column. In the recursive case,
transpose :: Matrix a -> Matrix a
transpose [] = []
transpose [row] = map (:[]) row
transpose (row : rows) = zipWith (:) row (transpose rows)transpose rows has exactly as many columns as row has entries, so zipWith (:) extends every column once. Structural induction on the number of rows proves that each output column contains the corresponding input entries in order. This uses list zip to implement the transpose, without requiring Num a. Articles by others on the same topic
There are currently no matching articles.