I have a function qpFunc that takes two strings. s:os and p:ps. I call this function recursivley and traverese thourgh a list. When s:os is empty, I want to call a new function itWilcard (p:ps). However, when trying to put it as a pattern-match, it complains that
"Variable not in scope: ps :: [Char]".
When used inside the function call it never reaches the condition it seems like since i get
"Non-exhaustive patterns in function qpFunc"
when calling it with "" "?".
How can I check if the lists used as arguments are empty and if so, apply a new function? In the question posted here question stackoverflow it says that
"x:xs is never empty: it always has element x. The canonical pattern for matching empty lists is []"
However, in my case I want to apply a function if the pattern is met.
This is my code so far:
qpFunc :: String -> String -> (String, String)
qpFunc [] [] = ("", "")
qpFunc [] _ = itWilcard (p : ps) -- does not work
qpFunc (s : os) (p : ps)
| (s : os) == [] = itWilcard (p : ps) -- never reached ?
| p == '*' = (os, p : ps)
| p == '?' = qpFunc os ps
| s == p = (os, ps)
| otherwise = ([], "#")
CodePudding user response:
(s : os) == []
can never be true, because it's by construction a list that has s
as its head (i.e., has at least this one element, and therefore isn't empty).
qpFunc [] _ = itWilcard (p : ps)
can't work, because, well, what are p
and ps
? This clause can't know that in another clause, you match p : ps
. But nothing stops you from doing it in this clause too:
qpFunc [] [] = ("", "")
qpFunc [] (p : ps) = itWilcard (p : ps)
qpFunc (s : os) (p : ps)
| ...
It's unnecessary to use a head : tail
pattern though, because you're anyway just passing the whole thing on to itWildcard
. So just make it
qpFunc [] [] = ("", "")
qpFunc [] ps = itWilcard ps
qpFunc (s : os) (p : ps)
| ...