【问题标题】:Couldn't match expected type `Int' with actual type `m0 Int'无法将预期类型“Int”与实际类型“m0 Int”匹配
【发布时间】:2014-11-15 20:29:46
【问题描述】:

我目前正在努力学习 Haskell。以下函数:

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

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

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)) }

我不确定m0 是什么意思,但我认为它是单子的。所以,我认为这意味着该函数正在返回一个单子 int。对于理解这个问题以及如何解决它的任何帮助,我们将不胜感激。

谢谢。

【问题讨论】:

  • 这不是您要找的return。搜索return 并研究这些类型应该很有启发性。

标签: haskell types compiler-errors maybe


【解决方案1】:

do 和 return 与 monad 相关。当你使用它们时,你告诉编译器你打算使用 monad。

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

您可以在do 之外使用let,但语法有点不同

findPivot matrixA = 
            let 
                firstCol = (transpose(matrixA)!!0)
                maxColValue = maximum firstCol
                pivotIndex = elemIndex maxColValue firstCol
            in fromJust(pivotIndex)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-14
    • 1970-01-01
    • 2012-08-29
    • 2019-04-23
    • 1970-01-01
    • 2017-04-07
    • 2015-06-08
    相关资源
    最近更新 更多