]> git.rmz.io Git - my-scheme.git/commitdiff
Show values (except Lists)
authorSamir Benmendil <me@rmz.io>
Tue, 23 Jan 2018 22:27:16 +0000 (22:27 +0000)
committerSamir Benmendil <me@rmz.io>
Tue, 23 Jan 2018 22:27:16 +0000 (22:27 +0000)
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

index fb8294a4df5b4a0e2ef6c6f28f3574b1a2bb61e8..1495605d09602a2ed23717d3b5956402874a6ef4 100644 (file)
@@ -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