0

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?

4
  • 2
    What's the type of elim_one? What's Mat El? Commented Feb 28, 2017 at 15:21
  • type of elim_one is elim_one :: Idx -> Idx -> Idx -> Mat El -> Mat El. Type of Mat El is [[]]. Commented Feb 28, 2017 at 15:22
  • 1
    Then shouldn't the type of elim_all be elem_all :: Idx -> Idx -> Idx -> Idx -> Mat El -> [Mat El]? Commented Feb 28, 2017 at 15:26
  • 1
    Well, then the cons operator expects one element (a list), buy you supply a list of lists? Commented Feb 28, 2017 at 15:26

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.