[Haskell-cafe] A program which never crashes (even when a
function calls "error")
fis at wiwi.hu-berlin.de
Tue Aug 1 03:29:18 EDT 2006
On Tue, Aug 01, 2006 at 08:52:06AM +0200, Stephane Bortzmeyer wrote:
> To: haskell-cafe at haskell.org
> From: Stephane Bortzmeyer <bortzmeyer at nic.fr>
> 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:
> 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.
looks a little different from python, but should do the trick.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org//pipermail/haskell-cafe/attachments/20060801/a5583846/attachment.bin
More information about the Haskell-Cafe