Matthias Fischmann writes ...
> I am now trying to learn Haskell for half a week and I like it a lot.
> But I still did not find out too much about exception handling. Is it
> possible that there is no ml-like mechanism with `raise' and `handle'
> built in? Yes, I know about types like
>
> data Result t = ERROR | Yes t
>
> and I also read the chapter in the report about IO, monads, and
> userError. But this seems all a little clumsy compared to the elegant
> and simple exceptions in standard ml or even ugly languages like java.
>
> Did I miss the point? Are there exceptions libraries? Is there any
> extension to Haskell in some existent implementation that handles my
> needs? How do you handle exceptions in your Haskell programs?
No, there are no extensions for handling exceptions. Some may think
that this is a bug but I think it's a feature ;-). First of all it's
against Haskell's spirit of being a pure functional language. Consider
raise First + raise Second handle First => 1 | Second => 2,
what is the value of this expression? It clearly depends on the order
of evaluation. SML defines this rigorously (right?). However, Haskell
is a lazy language, so even if the language designers would undergo
the task of defining the evaluation order, this is nothing which is
easy for the user to understand.
I usually try to avoid partial functions. That's easier as
one might suppose at first (if one is willing to invent new data types).
Consider operations on priority queues.
> data OptPair a b = Null
> | Pair a b
>
> class PriorityQueue q where
> empty :: (Ord a) => q a
> meld :: (Ord a) => q a -> q a -> q a
> splitMin :: (Ord a) => q a -> OptPair a (q a)
Instead of defining `getMin' and `deleteMin' which are both partial
functions we define `splitMin' which combines both. Here is a simple
application of `splitMin'.
> toOrderedList q = case splitMin q of
> Null -> []
> Pair a q -> a : toOrderedList q
For error reporting and alike I usually use an exception
monad.
Hope this helps, Ralf