简体   繁体   English

Haskell类型声明中的列表清单

[英]List of Lists in Type Declaration in Haskell

I have been at this code for almost 2 hours, and I keep getting the same compiler error message. 我使用该代码已经快两个小时了,并且不断收到相同的编译器错误消息。 I have done my research but just cannot find an answer 我已经完成研究,但找不到答案

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

Now I get the same error message no matter what I try. 现在,无论尝试什么,我都会收到相同的错误消息。 The error message is " Couldn't match expected type '[[Int]] -> Int' with actual type [Int]" With other specifications pointing to the first call of max towards the end of the code. 错误消息是“无法将期望的类型'[[[Int]]-> Int'与实际类型[Int]匹配”,其他规范指向在代码末尾首次调用max。 Please help, this is really frustrating 请帮助,这真令人沮丧

It now compiles and runs with my new code. 现在,它将编译并与我的新代码一起运行。 I'll be sure to post it later as it is getting kinda late, and I'm gonna put this down for the night. 我一定会稍后再发布,因为它有点晚了,我要把它放下来过夜。

This is wrong: 这是错误的:

table = buildTable (n1 n2 lcsHelp)

buildTable has type Int -> Int -> (Int -> Int -> a) -> [[a]] . buildTable类型为buildTable Int -> Int -> (Int -> Int -> a) -> [[a]] buildTable Int -> Int -> (Int -> Int -> a) -> [[a]] buildTable Int -> Int -> (Int -> Int -> a) -> [[a]] buildTable (n1 n2 lcsHelp) is applying it to one argument, namely (n1 n2 lcsHelp) . buildTable (n1 n2 lcsHelp)将其应用于一个参数,即(n1 n2 lcsHelp) So table would have type Int -> (Int -> Int -> a) -> [[a]] , which is invalid to pass as the third argument to lookupAns . 因此, table类型为Int -> (Int -> Int -> a) -> [[a]] lookupAns Int -> (Int -> Int -> a) -> [[a]] ,将其作为第三个参数传递给lookupAns是无效的。

Nevermind that (n1 n2 lcsHelp) is trying to apply an integer n1 to two things, which is obvious garbage. 没关系(n1 n2 lcsHelp)试图将整数n1应用于两件事,这显然是垃圾。

I don't get the error message you quote, though. 但是,我没有收到您引用的错误消息。 GHCi gives me: 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)

I'm not sure whether that's because the code you've posted isn't actually the code you compiled to get your error message (which is hinted at by the fact that you had to correct a typo), or just that GHCi is picking up the inconsistency at a different point than the compiler you're using. 我不确定这是否是因为您发布的代码实际上不是您为获取错误消息而编译的代码(这是您必须纠正错字的提示),或者仅仅是GHCi正在选择在与您使用的编译器不同的地方调解不一致的地方。

I'm guessing you probably meant: 我猜你可能是说:

table = buildTable n1 n2 lcsHelp

But that gives me a different error again. 但这又给了我一个不同的错误。

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

I pasted the code on hpaste so that it is easier to find out the issue. 我将代码粘贴到hpaste上,以便更轻松地找出问题所在。 As @Ben already pointed out, the issue is the type of the table . 正如@Ben已经指出的那样,问题在于table的类型。

buildTable function has the type Int -> Int -> (Int -> Int -> a) -> [[a]] . buildTable函数的类型为buildTable Int -> Int -> (Int -> Int -> a) -> [[a]] buildTable Int -> Int -> (Int -> Int -> a) -> [[a]] buildTable Int -> Int -> (Int -> Int -> a) -> [[a]] You are calling it as table = buildTable (n1 n2 lcsHelp) . 您将其称为table = buildTable (n1 n2 lcsHelp) So, the type of table will be Int -> (Int -> Int -> a) -> [[a]] . 因此, table的类型将为Int -> (Int -> Int -> a) -> [[a]] This type is invalid to pass to the lookupAns function which has the type Int -> Int -> [[Int]] -> Int 此类型无效,无法传递给类型为lookupAns Int -> Int -> [[Int]] -> Int lookupAns Int -> Int -> [[Int]] -> IntlookupAns函数

And if you were to do something like this table = buildTable n1 n2 lcsHelp which I believe might be your intent, then the type signature for buildTable has to change, as you will encounter this error 而且,如果您要执行类似于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'

And this happens because now the lcsHelp function, which returns an Int (because of the return value from the if..else statements) doesn't match the actual type of the buildTable function. 之所以会发生这种情况,是因为现在返回一个IntlcsHelp函数(由于if..else语句的返回值)与buildTable函数的实际类型不匹配。

So, if you can explain a bit more on what you are trying to achieve, it will be easier to help you out. 因此,如果您可以对自己要实现的目标进行更多的解释,将更容易为您提供帮助。 Most likely the lcsHelp function's type is what you need to revisit. 您很可能需要重新访问lcsHelp函数的类型。 May be the buildTable function needn't take a function as the input parameter. 可能是buildTable函数不需要采用函数作为输入参数。

couple of comments 1. lcsHelp takes no args 2. lookupAns in else-if-then takes wrong arguments, missing table 一对注释1. lcsHelp不带参数2. else-if-then中的 lookupAns错误的参数,缺少table

I have a little modified to: http://hpaste.org/66862 我做了一些修改: http : //hpaste.org/66862

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM