1

I wrote this program which has to multiply two matrixes but it says (empty list in tail), I guess my recurrence conditions are wrong but as a begginner, I don't realy see the problem, can you help me ? I think someone with enaugh experience will see the problem in 2 secondes.

multVecttt :: [Int]-> [Int] -> Int -- multiply a vector with  a vector
multVecttt [][]=0
multVecttt [] _=0
multVecttt [a] [b]=a*b
multVecttt xs ys= (head xs) * (head ys) + multVecttt (tail xs) (tail ys)

multVectMat :: [Int]-> [[Int]] -> [Int]-- multiply a vector with a matrix using hadamard multiplication of matrixes
multVectMat  [] [[a]]=[a]
multVectMat [][]=[]
multVectMat [] _=[]
multVectMat _ []=[]
multVectMat xs (ys: [[]]) = [multVecttt xs ys]
multVectMat xs yss= [multVecttt xs (head yss)] ++ multVectMat xs (tail yss)

multMatrix :: [[Int]] -> [[Int]] -> [[Int]]-- multiply two matrixes
multMatrix [][]=[]
multMatrix [][[a]]=[[a]]
multMatrix [[a]] [[b]]= [[a*b]]
multMatrix (xs: [[]]) yss = [multVectMat xs yss]
multMatrix xss yss = [multVectMat (head xss) (trans yss)] ++ multMatrix (tail xss) yss


trans:: [[Int]]-> [[Int]]-- return the transpose of a matrix
trans [[]]=[[]]
trans [[a],[b]]=[[a] ++ [b]]
trans xss=[(transHead xss)] ++ trans(transTail xss)

transHead:: [[Int]]->[Int]
transHead []=[]
transHead [[a],[b]]=[a] ++ [b]
transHead xss= [head(head xss)] ++ transHead (tail xss)

transTail:: [[Int]]-> [[Int]]

transTail []=[]
transTail xss= [tail(head xss)] ++ transTail(tail xss)

I wrote comments to make sure tou understand what are the functions doing

it compiles, but I have an execution error which is : Prelude.tail: empty list

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
mimi
  • 11
  • 2
  • Minimize, minimize, minimize. What's the smallest input that gives you a problem? Can you dive into the function that gives you a problem and figure out which of the calls to other functions is the one going wrong? Rinse, repeat. – Daniel Wagner Feb 04 '23 at 17:00
  • Have a look at https://stackoverflow.com/q/75342127/1126841 first; given the similar function names, I suspect this is a class mate. – chepner Feb 04 '23 at 17:24

1 Answers1

2

My recommendation is to eliminate all calls to tail with pattern matching. Then you can ask GHC to tell you where you went wrong. For example:

multMatrix xss yss = [multVectMat (head xss) (trans yss)] ++ multMatrix (tail xss) yss

can become:

multMatrix (xs:xss) yss = [multVectMat xs (trans yss)] ++ multMatrix xss yss

Do similarly for the other calls to tail and you can lean on automated tooling to help you find your mistake. Nice!

Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380