From 7495ece1a44cd1a8cccae2bdbc61fa6873f106d2 Mon Sep 17 00:00:00 2001 From: Samir Benmendil Date: Sun, 14 Jan 2018 00:41:31 +0000 Subject: [PATCH] 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`. --- app/Main.hs | 4 ++++ 1 file changed, 4 insertions(+) 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 -- 2.48.1