1

I am writing a parser using megaparsec and want to parse hex number to Char.

The hex number parser should take exactly two chars and return a char that has the same numeric value.

Right now I have something like this.

type Parser = Parsec Void String

pByte :: Parser Char
pByte = chr . read . ("0x" ++) <$> count 2 hexDigitChar

I heard that using read is considered bad practice.

Is there a built-in function that does something similar to this?

Dmitry
  • 11
  • 2
  • 2
    Although `read` is partial, you are taking care of constructing an argument for which `read` *is* defined, so I wouldn't worry too much about it. *Any* function is going to be partial, because practically speaking, there is no base in which an arbitrary `String` corresponds to a valid integer literal. – chepner Jan 11 '22 at 14:19
  • 3
    But, the whole point of the parser is to fail early on an invalid string, so it's providing the "guarantee" that `read` will be defined on its eventual argument. – chepner Jan 11 '22 at 14:28

0 Answers0