[Haskell-cafe] Attoparsec: Limiting Parsers to N Bytes, or Combing Parsers?

Yitzchak Gale gale at sefer.org
Mon Sep 26 18:49:59 CEST 2011


Evan Laforge wrote:
>> hex2 = (+)<$>  ((*16)<$>  higit)<*>  higit
>> higit = subtract (fromEnum '0')<$>  satisfy isHexDigit
>> color = Color<$>  hex2<*>  hex2<*>  hex2

Twan van Laarhoven wrote:
> How is "subtract (fromEnum '0')" supposed to convert a hex digit to an Int
> or Word8? I think you need digitToInt (or an equivalent function) for that.

Right. How about:

color = Color <$> hex2 <*> hex2 <*> hex2
hex2 = mkHex2 <$> hexit <*> hexit
hexit = satisfy isHexDigit
mkHex2 h1 h0 = fst . head $ readHex [h1, h0]

Or, if you are fanatic about safety like I am, you might even
write:

mkHex2 h1 h0 = maybe 0 fst . listToMaybe $ readHex [h1, h0]

Regards,
Yitz



More information about the Haskell-Cafe mailing list