`many1 digit` matches one or more digits. But it's result is actually a
`Parser String`. We need a way to operate on the value inside the `Parser
String` monad. This is where `liftM` comes in.
Another new thing here is the `.` operator which applies the function on
the right to any input of the function on the left. Or in other words it
applies the right function and passes the result to the function on the
left. In this case, `read` is applied to the parsed digits and the
result (of type Integer) is passed to the `Number` constructor of
`LispVal`.
module Main where
+import Control.Monad
import Text.ParserCombinators.Parsec hiding (spaces)
import System.Environment
"#f" -> Bool False
_ -> Atom atom
+parseNumber :: Parser LispVal
+parseNumber = liftM (Number . read) $ many1 digit
+
readExpr :: String -> String
readExpr input = case parse (spaces >> symbol) "lisp" input of
Left err -> "No match: " ++ show err