0

I'm trying to convert two textfiles into strings, and then adding them together in double-tuples, in a list. like this: [(_,_),(_,_)] This is my function:

testa = do  
    questions <- readFile "questionsQ.txt" 
    category <- readFile "category.txt"
    print (myZip category (lines questions))

myZip :: [a] -> [b] -> [(a, b)]
myZip [] [] = []
myZip _ [] = []
myZip (x:xs) (y:ys) = [(x,y)] ++ myZip xs ys

questions.txt contains one question per row

categories.txt contains a line of 50 numbers in a long row, each one representing one of the 5 categories

(Note – it may work at Mac computers, but I don't know why) This is my error message when I try to run the program (some of it at least):

[("0","I prefer variety to routine"),("0",""),("0","I'm an innovative person with a vivid imagination"),("0",""),("0","I enjoy wild flights of fantasy")....
ghci>
*** Exception: todo.hs:(35,1)-(37,44): Non-exhaustive patterns in function myZip

Why does it combine tuples with empty strings? And why is an error message occuring?

Vincent Savard
  • 34,979
  • 10
  • 68
  • 73
S Kilborn
  • 3
  • 2
  • 3
    What do you expect as a result of zip when the first list is empty and the second is not – epsilonhalbe Mar 01 '16 at 19:11
  • `-Wall` would have warned that your patterns are not exhaustive. I recommend turning that on. – chi Mar 01 '16 at 19:30
  • 1
    I find it surprising that you get this output, as I would have expected the result to be of type `[(Char,String)]`! – bartavelle Mar 01 '16 at 19:40
  • btw if you are interested in efficiency take a look at `:`, it can be used (with slight modifications) in the place of `++`. – epsilonhalbe Mar 01 '16 at 19:46
  • 1
    @epsilonhalbe GHC is smart enough to do the `[x] ++ y => x:y` transformation for you; this is nice, as it means you can use the two different forms to give subtle hints about what the author "meant". For example, `b ++ [m] ++ e` makes the symmetry between `b` and `e` more clear than `b ++ m : e`. However, I agree that the author should mean to use `:` here. =) – Daniel Wagner Mar 01 '16 at 20:36

1 Answers1

4

An exception! How can that be?
Isn't that quite a quip.
But there's a message, telling ye,
a non-exhaustive pattern
in this very matter
was found in your myZip.


You're missing the pattern for the following case:

myZip [] [1] = ???

If you had used -Wall, the compiler would have given the following warning:

Code.hs:2:1: Warning:
    Pattern match(es) are non-exhaustive
    In an equation for `myZip': Patterns not matched: [] (_ : _)

If your function is going to return the same value for almost all patterns except one, it's often easier to define that one first and then match all others:

myZip (x:xs) (y:ys) = [(x,y)] ++ myZip xs ys
myZip _      _      = []

That way you don't miss a pattern by accident too.

Zeta
  • 103,620
  • 13
  • 194
  • 236
  • I suggest your stackoverflow account be linked to https://poetry.stackexchange.com. – leftaroundabout Mar 01 '16 at 21:13
  • you should make a living out of this ... please keep me subscribed for the third week ;) – Random Dev Mar 02 '16 at 07:59
  • @SKilborn: You're welcome. Since you're new, you're probably want to [learn how to accept an answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). Otherwise your question will come up as "unanswered" in some filters. – Zeta Mar 02 '16 at 16:47
  • @Carsten Sure, just let me start a kickstarter campaign and/or a patreon account, some etsy page with Haskell stories like "The boy who cried bottom", "MultiParamTypeclasses and the seven Functional Dependencies", "The Prism and the Lens" and then I wait for the money. That's how it works, right? :D – Zeta Mar 02 '16 at 17:34