[Haskell-cafe] Re: [Haskell-beginners] Just how unsafe is unsafe
Roel van Dijk
vandijk.roel at gmail.com
Fri Feb 6 09:40:47 EST 2009
On Fri, Feb 6, 2009 at 1:00 PM, Antoine Latter <aslatter at gmail.com> wrote:
> Tangential to all of this - sometimes my unsafeXXX functions are pure,
> but partial. So I'll have:
>
> foo :: a -> b -> Maybe c
>
> and
>
> unsafeFoo :: a -> b -> c
I use the "unsafe" prefix in the same way. For me it means 'assume
that preconditions hold'. If the preconditions do not hold and you
evaluate an unsafe function anyway I would expect an error, as opposed
to an exception. I have done that in my (tiny) roman numerals package.
-- simplified
toRoman :: Int -> Either String Int
unsafeToRoman :: Int -> String
The first function is very clear about the fact that something can go
wrong. If you provide it with a value of (-3) it will (hopefully)
produce something like 'Left "no negative numbers allowed"'. The
second function hides this fact and will result in a (uncatchable)
runtime error. It is still a pure function, but preventing errors is
now the responsibility of whoever evaluates it.
More information about the Haskell-Cafe
mailing list