[Haskell-cafe] Maybe won't let me count

Viktor Dukhovni ietf-dane at dukhovni.org
Mon Mar 29 08:22:07 UTC 2021

> On Mar 29, 2021, at 3:36 AM, Antonio Regidor Garcia <chikitosan at gmail.com> wrote:
> succ n is n+1 but faster than the function (+).

Because 'succ' typically does bounds checks, while (+) (for Int) just
does the underlying CPU instruction, that's not particularly plausible.

Indeed running a test (100 million increments) suggests that (+) is
noticeably cheaper:

  (1 +):   MUT     time    0.035s  (  0.035s elapsed)
  (succ):  MUT     time    0.134s  (  0.134s elapsed)

The succ function is however safer against uncaught overflow:

  λ> succ False
  λ> succ True
  *** Exception: Prelude.Enum.Bool.succ: bad argument

If the datatype in question is not bounded (Double, Integer, ...) then
succ performance is closer to that of (+).  I see identical speeds for
Double, but (GHC 8.10 on X86_64) succ seems slightly slower for Integer.


More information about the Haskell-Cafe mailing list