user1327964
user1327964

Reputation: 1

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. 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.

Upvotes: 0

Views: 1992

Answers (4)

Simon
Simon

Reputation: 3110

couple of comments 1. lcsHelp takes no args 2. lookupAns in else-if-then takes wrong arguments, missing table

I have a little modified to: http://hpaste.org/66862

Upvotes: 0

Gangadhar
Gangadhar

Reputation: 1903

I pasted the code on hpaste so that it is easier to find out the issue. As @Ben already pointed out, the issue is the type of the table.

buildTable function has the type Int -> Int -> (Int -> Int -> a) -> [[a]]. You are calling it as table = buildTable (n1 n2 lcsHelp). So, the type of table will be Int -> (Int -> Int -> a) -> [[a]]. This type is invalid to pass to the lookupAns function which has the type Int -> Int -> [[Int]] -> Int

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

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.

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. May be the buildTable function needn't take a function as the input parameter.

Upvotes: 0

Ben
Ben

Reputation: 71590

This is wrong:

table = buildTable (n1 n2 lcsHelp)

buildTable has type Int -> Int -> (Int -> Int -> a) -> [[a]]. buildTable (n1 n2 lcsHelp) is applying it to one argument, namely (n1 n2 lcsHelp). So table would have type Int -> (Int -> Int -> a) -> [[a]], which is invalid to pass as the third argument to lookupAns.

Nevermind that (n1 n2 lcsHelp) is trying to apply an integer n1 to two things, which is obvious garbage.

I don't get the error message you quote, though. GHCi gives me:

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.

I'm guessing you probably meant:

table = buildTable n1 n2 lcsHelp

But that gives me a different error again.

Upvotes: 4

Gabriella Gonzalez
Gabriella Gonzalez

Reputation: 35099

The first lookupAns in lcslength is applied to too few arguments.

Upvotes: 1

Related Questions