From fabc3d8bdaef310b2f18bc53735ad7ee53518822 Mon Sep 17 00:00:00 2001 From: Samir Benmendil Date: Tue, 23 Jan 2018 22:27:16 +0000 Subject: [PATCH] Show values (except Lists) We introduce a `showVal :: LispVal -> String` function, to turn a LispVal into a printable string. This function uses pattern matching to "specialize" on various types. The declaration that matches the specific type is chosen. --- app/Main.hs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/Main.hs b/app/Main.hs index fb8294a..1495605 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -94,7 +94,15 @@ parseExpr = parseString readExpr :: String -> String readExpr input = case parse parseExpr "lisp" input of Left err -> "No match: " ++ show err - Right val -> "Found value: " ++ show val + Right val -> "Found value: " ++ showVal val + +showVal :: LispVal -> String +showVal (Atom atom) = atom +showVal (Number n) = show n +showVal (String str) = "\"" ++ str ++ "\"" +showVal (Character c) = "'" ++ [c] ++ "'" +showVal (Bool True) = "#t" +showVal (Bool False) = "#f" main :: IO () main = do -- 2.48.1