Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2016/functional-programming-design-and-analysis-of-algorithms/4/c/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2016 Functional programming, design and analysis of algorithms 4 c Solution by
Codex 0 2026-10-31
Reuse For of size and of size , every selected row and column has length . Their dot product isThe two list comprehensions place this entry in row , column of an result. The dimension guard checks exactly the compatibility condition for matrix multiplication. The shared
dimensions and either transpose definition above:multMat :: Num a => Matrix a -> Matrix a -> Matrix a
multMat a b
| colsA /= rowsB = error "multMat: incompatible sizes"
| otherwise = [[sum (zipWith (*) row col) | col <- columnsB]
| row <- a]
where
(_,colsA) = dimensions a
(rowsB,_) = dimensions b
columnsB = transpose bcolumnsB avoids recomputing the transpose for each row; the arithmetic cost is in the unit-cost model. New to topics? Read the docs here!