Natural numbers in ReScript?

Before I attempt to implement natural numbers in ReScript (including zero in this case), has anyone already done this, or wrapped an external library for doing it?

Is this for learning/fun or for production?

Are you meaning something like an inductive definition of natural numbers (e.g., type nat = O | S(int))? If so, there is an OCaml tutorial here. It would be similar in ReScript.

If you’re meaning something that you will use for real, is it because you want an abstract/opaque type that once constructed, you can be sure is definitely non-negative? E.g., something like this? If this is the case, this forum post has an example of a functor generating a non-negative int.

This is for learning and experimentation. And thanks for the excellent suggestions!

1 Like