Re:Haskell: The Bad Parts, part 2 (2020)
by Athas on Tuesday February 09, 2021 @11:14AM
(#26079215)
Am I missing something subtle? Why would you get a runtime exception if you call this function?
[ Parent
| on HN ]
Re:Haskell: The Bad Parts, part 2 (2020)
by jsmith45 on Tuesday February 09, 2021 @11:21AM
(#26079301)
The parent missed a part. If you call it with 0 you get an exception, because division by zero obviously.
Re:Haskell: The Bad Parts, part 2 (2020)
by yakubin on Tuesday February 09, 2021 @06:42PM
(#26084061)
No, you don't. You get Infinity. That's how floating point works.
Re:Haskell: The Bad Parts, part 2 (2020)
by smabie on Tuesday February 09, 2021 @10:11PM
(#26085357)
In most languages you get an exception
Re:Haskell: The Bad Parts, part 2 (2020)
by yakubin on Tuesday February 09, 2021 @11:24PM
(#26085738)
Which languages would those be?
Re:Haskell: The Bad Parts, part 2 (2020)
by warkdarrior on Wednesday February 10, 2021 @12:26PM
(#26092089)
$ python3
Python 3.9.1 (default, Feb 3 2021, 07:04:15)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 10 / 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>>
Re:Haskell: The Bad Parts, part 2 (2020)
by yakubin on Wednesday February 10, 2021 @09:45PM
(#26097660)
Although Python seems to be the exception to the rule here, your example isn't exactly right. In your example, you're performing integer division, not floating point division. In most languages, integer division by zero is indeed some kind of exception (or UB, which will most likely result in a hardware exception, causing a signal sent to the process). However, floating point division by zero is well-defined, as per definition of floating point arithmetic (in some RFCs), and most languages don't throw exceptions in this case. Python indeed seems to be the exception to the rule.
Re:Haskell: The Bad Parts, part 2 (2020)
by dthul on Thursday February 11, 2021 @09:28AM
(#26101791)
In Python 3, 1/0 is floating point division. 1//0 would be integer division.
Re:Haskell: The Bad Parts, part 2 (2020)
by yakubin on Thursday February 11, 2021 @11:31AM
(#26103539)
Right, Python2 is still the default on Debian, so I didn't notice.
Re:Haskell: The Bad Parts, part 2 (2020)
by smabie on Tuesday February 09, 2021 @11:31PM
(#26085767)
I guess I'm thinking of Python mostly. I thought OCaml did as well but apparently not.
Re:Haskell: The Bad Parts, part 2 (2020)
by the_af on Tuesday February 09, 2021 @11:23AM
(#26079326)
If you call it with argument 0 you get a runtime exception, because it results in a division by zero.
It's "partial" because it's not defined for 0.
Re:Haskell: The Bad Parts, part 2 (2020)
by magicalhippo on Tuesday February 09, 2021 @05:19PM
(#26083170)
If Double is IEEE 754 compatible then it should be perfectly defined for 0, you'd get +/- infinity. There are algorithms which rely on this.
So it should have been: Double -> Maybe Double
That would be the "right way" to fix this.
(or create a type NotNilDouble, lol)
Re:Haskell: The Bad Parts, part 2 (2020)
by bidirectional on Tuesday February 09, 2021 @02:00PM
(#26081258)
Or, controversially, define 1/0 = 0.[1]
Enough controversy in defining std libs as it is :)
Re:Haskell: The Bad Parts, part 2 (2020)
by the_af on Tuesday February 09, 2021 @03:11PM
(#26081897)
The "right" way for division by zero is controversial, though maybe that would be a solution.
It's clearer for partial function "head :: [a] -> a", which takes the first of a list if it exists, and explodes without dignity if the list is empty (this is what makes head partial).
A proposal is "head :: [a] -> Maybe a", so head returns Nothing when the list is empty.
Re:Haskell: The Bad Parts, part 2 (2020)
by cies on Wednesday February 10, 2021 @06:04AM
(#26087721)
Idris does well here:
https://www.idris-lang.org/docs/current/prelude_doc/docs/Pre...'
Re:Haskell: The Bad Parts, part 2 (2020)
by cies on Wednesday February 10, 2021 @06:06AM
(#26087733)
Idris does well here:
https://www.idris-lang.org/docs/current/contrib_doc/docs/Dat...