Home > Back-end >  Go through a string and use each char but I get this exception message : " (48,1)-(50,35): Non-
Go through a string and use each char but I get this exception message : " (48,1)-(50,35): Non-

Time:11-05

text :: String -> String
text (x:xs)
          | length xs >= 0 = (func x morse)    " "    text xs
          | otherwise = ""

When I execute this it actually works as I imagined but I get additionally an exception message : "(48,1)-(50,35): Non-exhaustive patterns in function text". I think there is something missing, but don't know what exactly.

CodePudding user response:

text :: String -> String
text [] = ""
text (x:xs) = func x morse    " "    text xs

When you do pattern matching, ideally, the patterns need to be exhaustive. What you are doing in your original code is to pattern match but miss the empty string case. Then you handle the empty string case in guards. This is an anti pattern. Just match on empty string in a pattern and remove the guards.

I also removed the parens around func x morse since has lower precedence than function application, so the parens are redundant.

  • Related