Haskell中类型声明中的列表列表

时间:2012-04-12 02:04:50

标签: haskell

我已经在这段代码中工作了将近2个小时,并且我一直收到相同的编译器错误消息。我做了我的研究但却找不到答案

buildTable :: Int -> Int -> (Int -> Int -> a) -> [[a]]

buildTable n m fun = [[ fun x y 
                    | x <- [0..n-1]]
                    | y <- [0..m-1]]


lookupAns :: Int -> Int -> [[Int]] -> Int
lookupAns len1 len2 theArray = 
    theArray !! len1 !! len2


lcsLength :: String -> String -> Int
lcsLength s1 s2 = 
  let 
    n1 = (length s1)
    n2 = (length s2)
    table = buildTable (n1 n2 lcsHelp)

    lcsHelp = if ( n1 == 0 || n2 == 0 )
                then 0

                else if ( last s1 == last s2 )

                then                    
                    (lookupAns
                        (n1 - 1)
                        n2
                        table)
                        + 1
                else
                    max 
                        (lookupAns 
                            n1
                            (n2-1)
                            table)
                        (lookupAns
                            (n1-1)
                            n2
                            table)





    in lookupAns
        (length s1)
        (length s2)
        table

现在无论我尝试什么,我都会收到相同的错误消息。错误消息“无法匹配预期类型'[[Int]] - &gt; Int'与实际类型[Int]”其他规范指向代码末尾的第一次调用max。请帮忙,这真是令人沮丧

它现在编译并运行我的新代码。我肯定会稍后发布它,因为它有点晚了,我要把它放到晚上。

4 个答案:

答案 0 :(得分:4)

这是错误的:

table = buildTable (n1 n2 lcsHelp)

buildTable的类型为Int -> Int -> (Int -> Int -> a) -> [[a]]buildTable (n1 n2 lcsHelp)正在将其应用于一个参数,即(n1 n2 lcsHelp)。因此,table的类型为Int -> (Int -> Int -> a) -> [[a]],无法将第三个参数作为lookupAns传递。

没关系(n1 n2 lcsHelp)试图将整数n1应用于两件事,这显然是垃圾。

但是,我没有收到您引用的错误消息。 GHCi给了我:

Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling Main             ( bar.hs, interpreted )

bar.hs:18:13:
    Couldn't match expected type `[[Int]]'
                with actual type `Int -> (Int -> Int -> a0) -> [[a0]]'
    In the return type of a call of `buildTable'
    In the expression: buildTable (n1 n2 lcsHelp)
    In an equation for `table': table = buildTable (n1 n2 lcsHelp)

我不确定是不是因为您发布的代码实际上并不是您编译的代码以获取您的错误消息(这是由于您必须纠正错误而暗示的),或者只是GHCi在与您正在使用的编译器不同的点上发现不一致。

我猜你可能意味着:

table = buildTable n1 n2 lcsHelp

但是这又给我一个不同的错误。

答案 1 :(得分:1)

lcslength中的第一个lookupAns应用于太少的参数。

答案 2 :(得分:0)

我将代码粘贴到hpaste上,以便更容易找到问题。正如@Ben已经指出的那样,问题是table的类型。

buildTable函数的类型为Int -> Int -> (Int -> Int -> a) -> [[a]]。您将其称为table = buildTable (n1 n2 lcsHelp)。因此,table的类型将为Int -> (Int -> Int -> a) -> [[a]]。传递给类型为lookupAns

Int -> Int -> [[Int]] -> Int函数时,此类型无效

如果您要执行此类table = buildTable n1 n2 lcsHelp我认为可能是您的意图,那么buildTable的类型签名必须更改,因为您将遇到此错误

Couldn't match expected type `Int -> Int -> Int'
            with actual type `Int'
In the third argument of `buildTable', namely `lcsHelp'

发生这种情况是因为现在lcsHelp函数返回Int(因为if..else语句的返回值)与{{的实际类型不匹配1}}功能。

所以,如果你能解释一下你想要实现的目标,那么你会更容易帮到你。很可能buildTable函数的类型是您需要重新访问的类型。可能是lcsHelp函数不需要将函数作为输入参数。

答案 3 :(得分:0)

几条评论 1. lcsHelp不采取任何行动 2. {strong> else-if-then 中的lookupAns使用了错误的参数,缺少table

我有一点修改为:http://hpaste.org/66862