Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is avoiding partial functions any easier in Haskell than other languages?

Tags:

haskell

We're urged to avoid partial functions with seemingly more emphasis in Haskell than other languages.

Is this because partial functions are a more frequent risk in Haskell than other languages (c.f. this question), or is it that avoiding them in other languages is impractical to the point of little consideration?

like image 859
kostmo Avatar asked Dec 03 '22 21:12

kostmo


1 Answers

Is this because partial functions are a more frequent risk in Haskell than other languages (c.f. this question), or is it that avoiding them in other languages is impractical to the point of little consideration?

Certainly the latter. The most commonly used languages all have some notion of the null value as an inhabitant of every type, the practical effect being that every value is akin to haskell's Maybe a.

You can argue that in haskell we have the same issue: bottoms can hide anywhere, e.g.

uhoh :: String
uhoh = error "oops"

But this isn't really the case. In haskell all bottom are morally equivalent and we can reason about code as if they didn't exist. If we could catch exceptions in pure code, this would no longer be the case. Here's an interesting discussion.

And just a subjective addendum, I think intermediate haskell developers tend to be aware of whether a function is partial, and to complain loudly when they are surprised to find they were wrong. At the same time a fair portion of the Prelude contains partial functions, such as tail and / and these haven't changed in spite of much attention and many alternative preludes, which I think is evidence that the language and standard lib probably struck a pretty decent balance.

EDIT I agree that Alexey Romanov's answer is an important part of the picture as well.

like image 118
jberryman Avatar answered May 01 '23 01:05

jberryman