show value

This commit is contained in:
Yann Esposito (Yogsototh) 2013-07-18 18:59:36 +02:00
parent 7b9efe9a3e
commit 231275f473

10
y.hs
View file

@ -14,7 +14,6 @@ data LispVal = Atom String
| Character Char | Character Char
| String String | String String
| Bool Bool | Bool Bool
deriving (Show)
-- The program (in IO) -- The program (in IO)
-- execute the arguments given in parameters -- execute the arguments given in parameters
@ -28,7 +27,7 @@ main = do
readExpr :: String -> String readExpr :: String -> String
readExpr input = case parse parseExpr "lisp" input of readExpr input = case parse parseExpr "lisp" input of
Left err -> "No match: " ++ show err Left err -> "No match: " ++ show err
Right val -> showVal val Right val -> show val
showVal :: LispVal -> String showVal :: LispVal -> String
showVal (String contents) = "\"" ++ contents ++ "\"" showVal (String contents) = "\"" ++ contents ++ "\""
@ -38,6 +37,13 @@ showVal (Float contents) = show contents
showVal (Character c) = '\'':c:'\'':[] showVal (Character c) = '\'':c:'\'':[]
showVal (Bool True) = "#t" showVal (Bool True) = "#t"
showVal (Bool False) = "#f" showVal (Bool False) = "#f"
showVal (List contents) = "(" ++ unwordsList contents ++ ")"
showVal (DottedList head tail) = "(" ++ unwordsList head ++ " . " ++ showVal tail ++ ")"
unwordsList :: [LispVal] -> String
unwordsList = unwords . map showVal
instance Show LispVal where show = showVal
-- parseExpr will parse the Expression -- parseExpr will parse the Expression
parseExpr :: Parser LispVal parseExpr :: Parser LispVal