
On Tue, Aug 01, 2006 at 08:52:06AM +0200, Stephane Bortzmeyer wrote:
To: haskell-cafe@haskell.org From: Stephane Bortzmeyer
Date: Tue, 1 Aug 2006 08:52:06 +0200 Subject: [Haskell-cafe] A program which never crashes (even when a function calls "error") [It is a philosophical question, not a practical programming problem.]
I'm used, in imperative programming languages with exceptions (like Python) to call any function without fear of stopping the program because I can always catch the exceptions with things like (Python):
while not over: try: code which may raise an exception... except Exception e: do something clever
How to do it in Haskell? How can I call functions like Prelude.head while being sure my program won't stop, even if I call head on an empty list (thus calling "error")?
in haskell98, you can't. if you cannot prove a list will always be non-empty, you should use pattern matching instead of head. one disadvantage of exceptions is that the byte code tends to be slow and ugly and hard to generate, in particular in pure lazy languages. but admittedly sometimes exceptions are cool. therefore ghc comes with a quite sophisticated and mature exception handling library. http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Exception... looks a little different from python, but should do the trick. cheers, matthias