I am currently struggling to learn Haskell. The following function:
findPivot :: [[Double]] -> Int
findPivot matrixA =
do
let firstCol = (transpose(matrixA)!!0)
let maxColValue = maximum firstCol
let pivotIndex = elemIndex maxColValue firstCol
return (fromJust(pivotIndex))
Is supposed to take a 2D-list of doubles, representing a matrix, and determine which row has the maximum value in the first column. I know there are some inefficient parts, for example using a list for represent the matrix and using transpose, but the problem I am encountering involves the following compiler error:
Couldn't match expected type `Int' with actual type `m0 Int'
In the return type of a call of `return'
In a stmt of a 'do' block: return (fromJust (pivotIndex))
In the expression:
do { let firstCol = (transpose (matrixA) !! 0);
let maxColValue = maximum firstCol;
let pivotIndex = elemIndex maxColValue firstCol;
return (fromJust (pivotIndex)) }
I'm not sure what the m0
means but I assume it means monadic. So, I think this means that the function is returning a monadic int. Any help in understanding this problem and how to solve it would be immensely appreciated.
Thanks.
do
and return
are related to monads. When you use them, you tell the compiler that you intend to use monads.
Your function type is non-monadic. This tells the compiler that you do not intend to use monads. The compiler is just warning you of this discrepancy.
You can use let
outside of do
, but the sytax is a bit different
findPivot matrixA =
let
firstCol = (transpose(matrixA)!!0)
maxColValue = maximum firstCol
pivotIndex = elemIndex maxColValue firstCol
in fromJust(pivotIndex)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments