On Sun, May 2, 2010 at 9:24 PM, Ivan Miljenovic
> On 3 May 2010 14:17, aditya siram <aditya.siram@xxxxxxxxx> wrote:
>> I'm a little confused about this too. I've seen many functions defined like:
>> f x = (\s -> ...)
>> which is a partial function because it returns a function and is the same as:
>> f x s = ...
> No, that's a partially applied function.
> A partial function is one such as:
> secondElement (_:x:_) = x
> Note that it is only defined for lists with at least two elements, for
> any other list (i.e. singleton or empty) it will throw an error;
> -fwarn-incomplete-patterns (which is included in -Wall) tells you
> about these.
> You can also argue that functions such as head are partial, as they
> explicitly throw an error if the input data isn't correct.
> Partial functions are bad because if you accidentally use one the
> wrong way, your entire program crashes in a flaming wreck. ÂIt's much
> better to do something like this:
> safeSecondElement (_:x:_) = Just x
> safeSecondElement _ Â Â Â Â = Nothing
> This will work with all possible input types.
> For more information, see
> http://en.wikipedia.org/wiki/Partial_function (from the mathematical
> Ivan Lazar Miljenovic
> Haskell-Cafe mailing list
Of course, there are situations where it is really awkward to not use
partial functions, basically because you *know* that an invariant is
satisfied and there is no sane course of action if it isn't. To take a
f ys = let xs = (1:ys) in last xs
uses the partial function "last". Rewriting it in the "non-partial style" gives
f ys = case (1:ys) of
 -> Nothing
xs -> Just (last xs)
but what possible meaning could a caller of the function ascribe to a
"Nothing" result? It just means that there is a bug in f, which is
what an error would tell you anyway. Of course, this particular
function could easily be rewritten in such a way to be total, but I
believe there are more complex examples where it is awkward/impossible
to do so.
Haskell-Cafe mailing list