Numeric underscores

When we write long numbers, we usually group the digits for easier reading:Grouping styles vary by region and language. Groups of three digits separated by commas is the American convention. For example, we would write one million as “1,000,000” rather than “1000000”.

Unfortunately, the standard Haskell language does not allow us to write numbers with any sort of visual grouping of the digits:

oneMillion :: Integer
oneMillion = 1000000

It may be difficult to judge at a glance whether the number we’ve written in the above example is actually one million; we have to carefully count the number of zeros.

The NumericUnderscores extensionGHC documentation for NumericUnderscores provides a remedy for this problem.

{-# LANGUAGE NumericUnderscores #-}

oneMillion :: Integer
oneMillion = 1_000_000

With the extension enabled,Numeric underscores is one of a handful of small number-related extensions. Others are NumDecimals, BinaryLiterals, HexFloatLiterals, and NegativeLiterals. you may insert an underscore at your discretion anywhere within a numeric literal. The compiler ignores the underscores; they are only there to assist human readers.

This extension became available in GHC 8.6.1.

Join Type Classes for courses and projects to get you started and make you an expert in FP with Haskell.