20
votes

I am learning Haskell and wanted to write a simple program which just repeats each letter in a string twice.

I came up with this:

repl :: String -> String
repl " " = " "
repl (x:xs) = x:x:repl xs

When compiling, I didnt get any warning but a run-time error occurred when I did repl "abcd":

"abcd*** Exception: repl.hs:(2,1)-(3,23): Non-exhaustive patterns in function repl

Why did the compiler never report this and why is it ignored in Haskell when there are many languages like OCaml which clearly report this at compile time?

1
ghc -fwarn-incomplete-patterns seems appropriate... - recursion.ninja
It should give a warning if compiled with -Wall. Specifically, the flag -fwarn-incomplete-patterns checks for this - Silvio Mayolo

1 Answers

21
votes

The pattern match warning is turned off by default. You can turn it on with the -fwarn-incomplete-patterns or as part of a larger bundle of warnings with -W and -Wall.

You can do this from ghci:

Prelude> :set -W

You can also pass the flag to ghc when you compile or include it as a pragma on top of your module:

{-# OPTIONS_GHC -fwarn-incomplete-patterns #-}

For your specific program, it should give the following warning:

/home/tjelvis/Documents/so/incomplete-patterns.hs:2:1: Warning:
    Pattern match(es) are non-exhaustive
    In an equation for ‘repl’: Patterns not matched: []