I’m having a problem with my program and I was able to isolate the problem. I managed to reduce it to this simpler problem. Lets say I have the function
fn:: String -> String
fn (x:xs)
| null (x:xs) = "empty"
| otherwise = "hello"
Typing in random stuff returns "hello" but if I do,
fn ""
I get the non-exhaustive pattern error. Since “” is suppose to be an empty list, [], shouldn’t it match to my first pattern and return "empty"?
A
Stringin Haskell is a list of characters. So to match the emptyStringyou need to match an empty list ([]). Your pattern(x:xs)will only match lists orStrings with at least one element because it consists of one element (x) and the rest (xs), which could be empty or non-empty.A working version of your function would look like this:
This will return
"empty"forfn "".