Pattern exhaustion in Haskell

Haskell is supposedly safe and crashing is not possible in a pure environment. But one of the easiest way to crash is by simply passing a arguments to a function that does not implement the arguments. For example, the following function:

func :: Int -> Int
func 1 = 2
func 2 = 3

Just crash it with:

*Main> func 1
*Main> func 2
*Main> func 3
*** Exception: e.hs:(2,1)-(3,10): Non-exhaustive patterns in function func

To avoid this, just use the flag -fwarn-incomplete-patterns in Haskell.

The same can also be done by putting this in your source file:

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

Now Haskell is even more safe!

Related posts: