]> git.rmz.io Git - my-scheme.git/commitdiff
readExpr function to check whether the input matches symbol
authorSamir Benmendil <me@rmz.io>
Sat, 13 Jan 2018 23:05:55 +0000 (23:05 +0000)
committerSamir Benmendil <me@rmz.io>
Sat, 13 Jan 2018 23:05:55 +0000 (23:05 +0000)
`readExpr :: String -> String` defines `readExpr` to be a function
(`->`) from String to a String.

`readExpr input = ...` will assign anything on the rhs to the `readExpr`
function where `input` is mapped to the first parameter of that
function.

`case ... of` is a *filter*.

`parse a b c` is a function from Parsec where `a` is the parser it should
use, `b` is the name of the input which is used for error messages and
`c` is the String to parse.

`parse` returns an `Either` type whose `Left` constructor indicates an
error. If we get a `Right` value, we bind it to `val`, ignore it and
return the String "Found Value".

app/Main.hs

index 015db3ad73adae9dec4ac67b281ac3e57c1d5fc0..a94dc2ce2355835c62533047f14994df94f28319 100644 (file)
@@ -5,7 +5,12 @@ import System.Environment
 symbol :: Parser Char
 symbol = oneOf "!#$%&|*+-/:<=>?@^_~"
 
+readExpr :: String -> String
+readExpr input = case parse symbol "lisp" input of
+    Left err -> "No match: " ++ show err
+    Right val -> "Found value"
+
 main :: IO ()
 main = do
     args <- getLine
-    putStrLn (args)
+    putStrLn (readExpr args)