yusuf
yusuf

Reputation: 3781

haskell: type mismatch error in recursion

I have such a recursive function;

elim_all :: Idx -> Idx -> Idx -> Idx -> Mat El -> Mat El
elim_all c r1b r1e r2 m
     | r1b == r1e = elim_one c r1b r2 m
     | otherwise = elim_one c r1b r2 m : elim_all c (r1b+1) r1e r2 m

elim_one function is;

elim_one :: Idx -> Idx -> Idx ->  Mat El -> Mat El
elim_one c r1 r2 m = let val1 = ((m!!r1)!!c)
                         val2 = ((m!!r2)!!c)
                         row1 = (mulr r1 val2 m)!!r1
                         row2 = (mulr r2 val1 m)!!r2
                         nrow = zipWith (-) row1 row2
                         matr = if r1 == r2
                                     then m
                                     else replacer r1 nrow m
                      in matr

When I run it, I get the following error:

    Couldn't match type ‘[El]’ with ‘Int’
    Expected type: [El]
      Actual type: Mat El
    In the first argument of ‘(:)’, namely ‘elim_one c r1b r2 m’
    In the expression:
      elim_one c r1b r2 m : elim_all c (r1b + 1) r1e r2 m

error still doesn't make sense to me. How can I fix the problem?

Upvotes: 0

Views: 95

Answers (2)

Ingo
Ingo

Reputation: 36339

Both elim_one and elim_all compute something of type Mat E1. But whatever this might be, since

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

and for all types x, it holds that x is not the same as [x] you can never relate the results of evaluation of elim_one and elim_all with the (:) operator.

Upvotes: 2

John F. Miller
John F. Miller

Reputation: 27217

So here is the line in question:

| otherwise = elim_one c r1b r2 m : elim_all c (r1b+1) r1e r2 m

Now you have said in your type signature that the result* of elim_all will be a Mat El, but in this line the result is a list (that is what the (:) operator forms).

Without knowing more about what the Mat type does, my best guess is that you need to wrap the output of this case in a Type Constructor of Mat.


* When the function is fully applied.

Upvotes: 2

Related Questions