Algorithm 3n+;1使用Haskell实现,编译错误

Algorithm 3n+;1使用Haskell实现,编译错误,algorithm,haskell,Algorithm,Haskell,各位。我是Haskell的新手,刚刚用它实现了“3n+1”问题。我检查了很多,但是类型错误似乎很奇怪,你能帮我找到问题所在吗 import qualified Data.Vector as V import qualified Data.Matrix as M nMax = 1000000 table = V.fromList $ 0 : 1 : [cycleLength x | x <- [2 .. nMax]] where cycleLength x = if x' <

各位。我是Haskell的新手,刚刚用它实现了“3n+1”问题。我检查了很多,但是类型错误似乎很奇怪,你能帮我找到问题所在吗

import qualified Data.Vector as V
import qualified Data.Matrix as M

nMax = 1000000

table = V.fromList $ 0 : 1 : [cycleLength x | x <- [2 .. nMax]] where
    cycleLength x = if x' <= nMax then table V.! x' + 1 else cycleLength x' + 1 where 
        x' = if even x then x `div` 2 else 3 * x + 1

sparseTable = M.fromLists $ [] : [[f i j | j <- [0 .. ceiling $ logBase 2 nMax]] | i <- [1 .. nMax]] where
    f i 0 = table V.! i
    f i j = maxValue i j

maxValue i j = max $ (leftValue i j) (rightValue i j) where
    leftValue i j = sparseTable M.! (i, j - 1)
    rightValue i j = sparseTable M.! (i + 2 ^ (j - 1), j - 1)

问题在于
max$(leftValue i j)(rightValue i j)
中的
$

($)
操作符绑定的紧密程度低于任何其他操作符,包括“仅使用空格时获得的普通函数应用程序”

因此,对于
$
,它解析为

max ((leftvalue i j) (rightValue i j))
如果您删除它,它应该按照您的意图进行解析,这大概是

max (leftValue i j) (rightValue i j)
您可以从错误消息中得到提示,其中讨论了“leftValue的第三个参数
leftValue


中有一些关于
($)
的更多信息问题在于
max$(leftValue i j)(rightValue i j)
中的
$

($)
操作符绑定的紧密程度低于任何其他操作符,包括“仅使用空格时获得的普通函数应用程序”

因此,对于
$
,它解析为

max ((leftvalue i j) (rightValue i j))
如果您删除它,它应该按照您的意图进行解析,这大概是

max (leftValue i j) (rightValue i j)
您可以从错误消息中得到提示,其中讨论了“leftValue的第三个参数
leftValue


maxValue
函数中,有一些关于
($)
的详细信息。在
maxValue
函数中,它会导致显示的错误。在
maxValue
函数中,它会导致显示的错误。值得注意的是,这可以写成
max(leftValue i j)$rightValue i j
,如果确实需要
$
。或者,您可以使用
leftValue i j`max`rightValue i j
完全避免括号,但我发现这很难阅读。非常感谢您,问题解决了。无论出现了什么新问题,我认为我应该在Haskell中进行更多练习……)值得注意的是,如果确实需要
$
,可以将其写成
max(leftValue i j)$rightValue i j
。或者,您可以使用
leftValue i j`max`rightValue i j
完全避免使用括号,但我发现这很难阅读。非常感谢,问题解决了。不管出现了什么新问题,我想我应该在Haskell多练习……)