Conditional expressions

signum :: Int -> Int
signum n = if n < 0 then -1 else
              if n == 0 then 0 else 1

And a safetail function, where an empty list is returned instead of an error when given an empty list.

safetail :: [a] -> [a]
safetail xs = if length xs > 0 then tail xs else []

Guarded equations

An alternative to conditional expressions, functions can be defined with guarded equations.

An example of the signum function:

signum :: Int -> Int
signum n | n < 0     = -1
         | n == 0    = 0
         | otherwise = 1

Here is safetail with guarded equations:

safetail :: [a] -> [a]
safetail xs | length xs > 0 = tail xs
            | otherwise     = []