3 ms·
Haskell has exceptions and monads. Either can be used for error handling; monads are, admittedly, unusual, but the error-handing type monads (as opposed to side
by cbd1984 10y ago
Haskell has exceptions and monads. Either can be used for error handling; monads are, admittedly, unusual, but the error-handing type monads (as opposed to side-effect-handling type monads) are easy to understand in terms of wrapping values (or the potential for a lack of value) such that the type system can be used to both enforce a certain level of care and enable a certain insensitivity to errors.
For example, the Maybe monad is the simplest way to handle a function which might not return a usable value: A value wrapped in a Maybe monad has a type of either (Just a) (with a being any type at all) or Nothing. Here's a trivial example, straight from the ghci REPL:
> let mdiv x y = if y == 0 then Nothing else (Just (x / y))
> let foo x = (Just (x + 5))
> let bar y = (Just (y / 2))
> mdiv 5 6 >>= foo >== bar
Just 2.9166666666666665
> mdiv 5 0 >>= foo >>= bar
Nothing
See? Complete insensitivity to errors until the last possible moment. Much less noisy than the C equivalent would be, although in this specific example I suppose the Inf semantics of the IEEE floating point standard would be equivalent.