]> git.rmz.io Git - my-scheme.git/commitdiff
Parse Atoms which start with a letter or symbol
authorSamir Benmendil <me@rmz.io>
Sun, 14 Jan 2018 00:19:18 +0000 (00:19 +0000)
committerSamir Benmendil <me@rmz.io>
Sun, 14 Jan 2018 00:19:18 +0000 (00:19 +0000)
`<|>` is a Parser combinator, the choice operator. It will try the first
parser, if it doesn't match anything, try the second parser, and so on.

`a:b` creates a list from `a` and `b`. Another possibility would be to
use `[a] ++ b`, where a new list with a single element is created `[a]`
and concatenated with `++`

app/Main.hs

index 22de7480421b03aaa552b541b8266a4a9bd41135..7c987cee50d96d97e445cc1b18756f845a47a24f 100644 (file)
@@ -22,6 +22,12 @@ parseString = do
     char '"'
     return $ String x
 
+parseAtom :: Parser LispVal
+parseAtom = do
+    a <- letter <|> symbol
+    b <- many (letter <|> digit <|> symbol)
+    return $ Atom (a:b)
+
 readExpr :: String -> String
 readExpr input = case parse (spaces >> symbol) "lisp" input of
     Left err -> "No match: " ++ show err