Created
September 7, 2014 19:01
-
-
Save waynr/9286f5b53850376602c2 to your computer and use it in GitHub Desktop.
Literate haskel example file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Monad | |
import System.Environment | |
import Text.ParserCombinators.Parsec hiding (spaces) | |
data LispVal = Atom String | |
| List [LispVal] | |
| DottedList [LispVal] LispVal | |
| Number Integer | |
| String String | |
| Bool Bool | |
symbol :: Parser Char | |
symbol = oneOf "!#$%&|*+-/:<=>?@^_~" | |
parseString :: Parser LispVal | |
parseString = do | |
char '"' | |
x <- many (noneOf "\"") | |
char '"' | |
return $ String x | |
parseAtom :: Parser LispVal | |
parseAtom = do | |
first <- letter <|> symbol | |
rest <- many (letter <|> digit <|> symbol) | |
let atom = first:rest in | |
return $ case atom of | |
"#t" -> Bool True | |
"#f" -> Bool False | |
_ -> Atom atom | |
parseNumber :: Parser LispVal | |
parseNumber = liftM (Number . read) $ many1 digit | |
parseExpr :: Parser LispVal | |
parseExpr = parseAtom | |
<|> parseString | |
<|> parseNumber | |
spaces :: Parser () | |
spaces = skipMany1 space | |
readExpr :: String -> String | |
readExpr input = case parse parseExpr "lisp" input of | |
Left err -> "No match: " ++ show err | |
Right val -> "Found value" | |
main :: IO () | |
main = do | |
args <- getArgs | |
putStrLn (readExpr (args !! 0)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\begin{code} | |
import Control.Monad | |
import System.Environment | |
import Text.ParserCombinators.Parsec hiding (spaces) | |
data LispVal = Atom String | |
| List [LispVal] | |
| DottedList [LispVal] LispVal | |
| Number Integer | |
| String String | |
| Bool Bool | |
symbol :: Parser Char | |
symbol = oneOf "!#$%&|*+-/:<=>?@^_~" | |
parseString :: Parser LispVal | |
parseString = do | |
char '"' | |
x <- many (noneOf "\"") | |
char '"' | |
return $ String x | |
parseAtom :: Parser LispVal | |
parseAtom = do | |
first <- letter <|> symbol | |
rest <- many (letter <|> digit <|> symbol) | |
let atom = first:rest in | |
return $ case atom of | |
"#t" -> Bool True | |
"#f" -> Bool False | |
_ -> Atom atom | |
parseNumber :: Parser LispVal | |
parseNumber = liftM (Number . read) $ many1 digit | |
parseExpr :: Parser LispVal | |
parseExpr = parseAtom | |
<|> parseString | |
<|> parseNumber | |
spaces :: Parser () | |
spaces = skipMany1 space | |
readExpr :: String -> String | |
readExpr input = case parse parseExpr "lisp" input of | |
Left err -> "No match: " ++ show err | |
Right val -> "Found value" | |
main :: IO () | |
main = do | |
args <- getArgs | |
putStrLn (readExpr (args !! 0)) | |
\end{code} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment