I’m going through Write Yourself a Scheme and am struck at the exercise 4 on this page.
How do I go about this? I’ve got this far, but have no idea whatsoever where the readHex is supposed to go, must I liftM it ? Do you case match the parser ?
parseNumber = liftM (Number . read) $ choice [many1 digit, char '#' >> oneOf "hd" >>= a]
where a f = case f of
'h' -> many1 digit
Also, I don’t suppose you can apply <|> on Parser LispVal functions, right?
Yes, since
readHexis most likely a pure function andliftMlifts it into the monadic context ofParser.Since I don’t quite understand what your local function
ais good for, I’m leaving it for now and simply use the functionsparseDecimalandparseHex. In that case, you could writeparseNumberlike so:Of course you can omit the type signatures, I just added them for clarity.
<|>works for everyParser a.I recommend reading some material on parser combinators, i.e. the Parsec User Guide.