My Function is
import System.IO
import Debug.Trace
main :: IO ()
main = do
datei <- openFile "palindrom.txt" ReadMode
palin <- hGetContents datei
putStrLn $ unlines [ check x | x <- lines palin]
check :: String -> String
check x
| null x = ""
| trace ("call check "++ show x) False = x
| x == (reverse x) = if null x then ""
else do x ++ " Palindrom length " ++ show (length x)
I get the Exception Non-exhaustive patterns in the function `check`.
How can I match the string to complete the pattern, I tried also the empty String ""
or I am even not able to use this kind of pattern on a String in Haskell?
ps: The palindrom.txt is
a
aa
ab
aha
anna
anne
bry
bub
Let's try evaluating it by hand for a few steps (sans the trace
call), shall we?
check "ab"
===
case null "ab" of True -> ""
; _ -> case "ab" == (reverse "ab") of True ->
"ab" ++ " Palindrom length " ++ show (length "ab")
===
case False of True -> ""
; _ -> case "ab" == "ba" of True ->
"ab Palindrom length " ++ show 2
===
case "ab" == "ba" of True ->
"ab Palindrom length " ++ "2"
===
case False of True ->
"ab Palindrom length " ++ "2"
===
ERROR: none of the cases matched.
So all the tests in guards having failed, the pattern match for x | ...
fails a well, which is reported as "non-exhaustive patterns"
.
In GHCi:
> case False of True -> 1
*** Exception: <interactive>:789:1-23: Non-exhaustive patterns in case