Argus
Argus

Reputation: 911

Non-exhaustive pattern while iterating matrix

So I'm very new to haskell, and I'm not very sure how to deal with this error when iterating over a matrix. I'm guessing there's a case I'm not considering, but I can't figure out what it is. I've got two functions, one that turns a list into a string and another that turns a matrix into a string. These are my two functions:

listToString :: [Int] -> String 
listToString [] = "\n"
listToString (x:xs) = show x ++ " " ++ listToString xs 

matToString :: [[Int]] -> String 
matToString [[]] = ""
matToString (y:x:xs)) = listToString y ++  matToString (x:xs)

listToString works fine but matToString does not. I was wondering if someone could help me out with this. I've been having a hard time understanding Haskell, since I've never programmed in a functional programming language before, or well at least not one that is purely functional.

Upvotes: 1

Views: 58

Answers (1)

dfeuer
dfeuer

Reputation: 48611

Your recursive case covers every list with at least two arguments, so that's cool. The problem is your base case—it only covers the case of a list with exactly one element, itself the empty list.

Add this to the very top of your file: {-# OPTIONS_GHC -Wall #-}. That should give you a detailed compiler warning indicating which pattern(s) is/are missing.

Upvotes: 1

Related Questions