When to select "Natural" over "Integer" in Haskell?

I recently discovered Natural data type in base . It is assumed that it will be accepted (as it seems to me) when you intend to use a non-negative integer type. But it’s not entirely clear why I prefer Natural Integer . Both types have arbitrary precision, both have a fairly optimized runtime view - Integer view and Natural . But Natural can throw pure exceptions when you subtract natural numbers, and this does not really add more types of security to your code. Although Integer more popular in all packages.

So, when and why should I use Natural ?

+7
integer primitive-types haskell integer-arithmetic
source share
1 answer

I do not understand why you want to use Natural or Integer . Why not use Rational instead? This is arbitrary precision, optimized representation of runtime and works for the natural, wholesome and rational!

My point is that we should choose a type that makes sense semantically. Allows you to count houses on the street with straights, record our next golf game with integers, and share a fresh blueberry pie with rationality.

+4
source share

All Articles