无法将预期类型"Int"与实际类型"m0 Int"匹配

W. *_*ins 4 haskell types compiler-errors maybe

我目前正在努力学习Haskell.以下功能:

findPivot :: [[Double]] -> Int
findPivot matrixA =
    do
        let firstCol = (transpose(matrixA)!!0)
        let maxColValue = maximum firstCol
        let pivotIndex = elemIndex maxColValue firstCol
        return (fromJust(pivotIndex))
Run Code Online (Sandbox Code Playgroud)

应该采用表示矩阵的二维2D列表,并确定哪一行在第一列中具有最大值.我知道有一些低效的部分,例如使用列表来表示矩阵和使用转置,但我遇到的问题涉及以下编译器错误:

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)) }
Run Code Online (Sandbox Code Playgroud)

我不确定是什么m0意思,但我认为这意味着monadic.所以,我认为这意味着该函数返回一个monadic int.任何帮助理解这个问题以及如何解决它将非常感激.

谢谢.

jam*_*idh 6

doreturn与monads有关.当您使用它们时,您告诉编译器您打算使用monad.

您的函数类型是非monadic.这告诉编译器您不打算使用monad.编译器只是警告您这种差异.

你可以let在外面使用do,但sytax有点不同

findPivot matrixA = 
            let 
                firstCol = (transpose(matrixA)!!0)
                maxColValue = maximum firstCol
                pivotIndex = elemIndex maxColValue firstCol
            in fromJust(pivotIndex)
Run Code Online (Sandbox Code Playgroud)