From: Samir Benmendil Date: Sun, 14 Jan 2018 00:41:31 +0000 (+0000) Subject: Parse Numbers X-Git-Url: https://git.rmz.io/my-scheme.git/commitdiff_plain/7495ece1a44cd1a8cccae2bdbc61fa6873f106d2?ds=inline Parse Numbers `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`. --- diff --git a/app/Main.hs b/app/Main.hs index 6fcf667..6daf522 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,4 +1,5 @@ module Main where +import Control.Monad import Text.ParserCombinators.Parsec hiding (spaces) import System.Environment @@ -32,6 +33,9 @@ parseAtom = do "#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