[Haskell-cafe] Reading integers

Bertram Felgenhauer bertram.felgenhauer at googlemail.com
Thu Sep 7 10:01:17 EDT 2006


Lennart Augustsson wrote:
> Also, don't use (ord c - ord '0'), it doesn't work with Unicode digits.
> That's why there is a digitToInt function.

FWIW, in Haskell 98, isDigit and digitToInt are defined as

isDigit c                =  c >= '0' && c <= '9'

digitToInt :: Char -> Int
digitToInt c
  | isDigit c            =  fromEnum c - fromEnum '0'
  | c >= 'a' && c <= 'f' =  fromEnum c - fromEnum 'a' + 10
  | c >= 'A' && c <= 'F' =  fromEnum c - fromEnum 'A' + 10
  | otherwise            =  error "Char.digitToInt: not a digit"

making this a bit of a red herring. I can't comment on why Bulat doesn't
like negative numbers.

Bertram


More information about the Haskell-Cafe mailing list