Skip to content
Snippets Groups Projects
Commit 0c4c72c5 authored by ik1g19's avatar ik1g19
Browse files

progress on challenge 4

parent 6816f582
No related branches found
No related tags found
No related merge requests found
...@@ -20,6 +20,8 @@ import Control.DeepSeq ...@@ -20,6 +20,8 @@ import Control.DeepSeq
import System.IO import System.IO
import System.Random import System.Random
import Control.Applicative
-- types for Part I -- types for Part I
type WordSearchGrid = [[ Char ]] type WordSearchGrid = [[ Char ]]
...@@ -271,21 +273,21 @@ printGrid (w:ws) = do putStrLn w ...@@ -271,21 +273,21 @@ printGrid (w:ws) = do putStrLn w
-- LamAbs Int LamExpr | LamVar Int deriving (Eq,Show,Read) -- LamAbs Int LamExpr | LamVar Int deriving (Eq,Show,Read)
prettyPrint :: LamMacroExpr -> String prettyPrint :: LamMacroExpr -> String
prettyPrint (LamDef ms expr) = exprBrackets expr prettyPrint (LamDef ms e) = exprBrackets e
--applies brackets to expr if needed --applies brackets to expr if needed
exprBrackets :: LamExpr -> String exprBrackets :: LamExpr -> String
exprBrackets expr | parseExpr str == expr = str --omit brackets exprBrackets e | expr str == e = str --omit brackets
| otherwise = "(" + str ++ ")" --include brackets | otherwise = "(" + str ++ ")" --include brackets
where where
str = exprToStr expr str = exprToStr e
--converts expr to string --converts expr to string
exprToStr :: LamExpr -> String exprToStr :: LamExpr -> String
exprToStr (LamApp expr1 expr2) = exprBrackets expr1 ++ " " ++ exprBrackets expr2 exprToStr (LamApp e1 e2) = exprBrackets e1 ++ " " ++ exprBrackets e2
exprToStr (LamAbs x expr) = "\\x" ++ show x ++ " -> " ++ exprBrackets expr exprToStr (LamAbs x e) = "\\x" ++ show x ++ " -> " ++ exprBrackets e
exprToStr (LamVar x) = "x" ++ show x exprToStr (LamVar x) = "x" ++ show x
exprToStr (LamMacro m) = m exprToStr (LamMacro m) = m
...@@ -300,10 +302,105 @@ ex3'4 = LamDef [ ("F", LamAbs 1 (LamVar 1) ) ] (LamAbs 2 (LamApp (LamAbs 1 (LamV ...@@ -300,10 +302,105 @@ ex3'4 = LamDef [ ("F", LamAbs 1 (LamVar 1) ) ] (LamAbs 2 (LamApp (LamAbs 1 (LamV
-- Challenge 4 -- -- Challenge 4 --
-- data LamMacroExpr = LamDef [ (String,LamExpr) ] LamExpr deriving (Eq,Show,Read)
-- data LamExpr = LamMacro String | LamApp LamExpr LamExpr |
-- LamAbs Int LamExpr | LamVar Int deriving (Eq,Show,Read)
--MacroExpr ::= "def" MacroName "=" Expr "in" MacroExpr | Expr
--Expr ::= Var | MacroName | Expr Expr | “\” Var “->” Expr | “(“ Expr “)”
--MacroName ::= UChar | UChar MacroName
--UChar ::= "A" | "B" | ... | "Z"
--Var ::= “x” Digits
--Digits ::= Digit | Digit Digits
--Digit ::= “0” | “1” | “2” | “3” | “4” | “5” | “6” | “7” | “8” | “9”
parseLamMacro :: String -> Maybe LamMacroExpr parseLamMacro :: String -> Maybe LamMacroExpr
parseLamMacro _ = Nothing parseLamMacro _ = Nothing
macroExpr :: Parse LamMacroExpr
macroExpr = do string "def"
space
name <- macroName
space
expr :: Parser LamExpr
expr = do {x <- var; return $ LamVar x} <|>
do {name <- macroName;return $ LamMacro name} <|>
do e1 <- expr
space
e2 <- expr
return $ LamApp e1 e2 <|>
do char '\'
x <- var
symbol "->"
e <- expr
return $ LamAbs x e <|>
do char '('
e <- identifier
char ')'
return e
macroName :: Parser String
macroName = do name <- some upper
return $ name
var :: Parser Int
var = do char 'x'
x <- some digit
return $ read x
-- examples in the instructions
--Just (LamDef [] (LamApp (LamVar 1) (LamApp (LamVar 2) (LamVar 3)))) --"x1 (x2 x3)"
--Just (LamDef [] (LamApp (LamApp (LamVar 1) (LamVar 2)) (LamMacro"F"))) --"x1 x2 F"
--Just (LamDef [ ("F", LamAbs 1 (LamVar 1) ) ] (LamAbs 2 (LamApp (LamVar 2) (LamMacro "F")))) --"def F = \x1-> x1 in \x2 -> x2 F"
--Nothing -not in grammar --"def F = \x1 -> x1 (def G = \x1 -> x1 in x1)in \x2 -> x2"
--Nothing -repeated macro definition --"def F = \x1 -> x1 in def F = \x2 -> x2 x1 in x1"
--Nothing -macro body not closed --"def F = x1 in F"
--arithmetic expression examples
-- expr ::= term '+' expr ⏐ term
-- term ::= factor '*' term ⏐ factor
-- factor ::= nat ⏐ '(' expr ')‘
-- nat ::= digit | digit nat
-- digit ::= ’0’ ⏐ '1' ⏐ ... ⏐ '9'
-- expr :: Parser AETree
-- expr = do t ← term
-- char ‘+’
-- e ← expr
-- return (Add t e)
-- <|> term
-- term :: Parser AETree
-- term = do f ← factor
-- char ‘*’
-- t ← term
-- return (Mul f t)
-- <|> factor
-- factor :: Parser AETree
-- factor = nat <|> do char '('
-- e ← expr
-- char ')'
-- return e
-- nat :: Parser AETree
-- nat = do ds ← some digit
-- return (Lit (read ds))
-- Challenge 5 -- Challenge 5
cpsTransform :: LamMacroExpr -> LamMacroExpr cpsTransform :: LamMacroExpr -> LamMacroExpr
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment