Here is my function.
toResult :: [SqlValue] -> IO Course
toResult [ fromSql -> courseid, fromSql -> title,
fromSql -> name, fromSql -> version,
fromSql -> cdate, fromSql -> fid ] = do
let courseXML = show (fid :: Int)
xml <- B.readFile courseXML
let Right doc = parseXML courseXML xml
let passing = read $ T.unpack $ fromJust
$ lookup (T.pack "PassingScore")
$ elementAttrs $ head $ docContent doc
return (Course courseid title name version cdate passing)
Any call to this function that does not pass a parameter that is a list of exactly six SqlValue values is an error.
Compiling this returns the non-exhaustive pattern matching warning:
Pattern match(es) are non-exhaustive
In an equation for `toResult':
Patterns not matched:
[]
[_]
[_, _]
[_, _, _]
...
I know that I can just ignore the warning, but I am trying to learn how to do this Haskell stuff correctly.
Any and all suggestions are appreciated.
Dave
Update...
Based upon the suggestions presented here, I have changed my code to the present form.
toResult :: (SqlValue, SqlValue, SqlValue, SqlValue, SqlValue, SqlValue) -> IO Course
toResult ( fromSql -> courseid, fromSql -> title,
fromSql -> name, fromSql -> version,
fromSql -> cdate, fromSql -> fid ) = do
let courseXML = show (fid :: Int)
xml <- B.readFile courseXML
let Right doc = parseXML courseXML xml
let passing = read $ T.unpack $ fromJust
$ lookup (T.pack "PassingScore")
$ elementAttrs $ head $ docContent doc
return (Course courseid title name version cdate passing)
listToTuple :: [SqlValue] -> (SqlValue, SqlValue, SqlValue, SqlValue, SqlValue, SqlValue)
listToTuple [a,b,c,d,e,f] = (a,b,c,d,e,f)
listToTuple xs = error "Wrong number of values"
getCourses :: Connection -> IO [Course]
getCourses conn = do
let query = "{Actual query deleted to protect the innocent.}"
res <- quickQuery conn query []
mapM toResult (listToTuple res)
However, this won't compile with the following error. What simple thing am I missing this time?
src\CDCQuarterly.hs:122:20:
Couldn't match type `(,,,,,)
SqlValue SqlValue SqlValue SqlValue SqlValue'
with `[]'
Expected type: [(SqlValue,
SqlValue,
SqlValue,
SqlValue,
SqlValue,
SqlValue)]
Actual type: (SqlValue,
SqlValue,
SqlValue,
SqlValue,
SqlValue,
SqlValue)
In the second argument of `mapM', namely `(listToTuple res)'
In a stmt of a 'do' block: mapM toResult (listToTuple res)
src\CDCQuarterly.hs:122:32:
Couldn't match type `[SqlValue]' with `SqlValue'
Expected type: [SqlValue]
Actual type: [[SqlValue]]
In the first argument of `listToTuple', namely `res'
In the second argument of `mapM', namely `(listToTuple res)'