Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
prog3-coursework
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ik1g19
prog3-coursework
Commits
e75f3acc
Commit
e75f3acc
authored
4 years ago
by
ik1g19
Browse files
Options
Downloads
Patches
Plain Diff
almost finished challenge 4
parent
fd638c3c
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
cw/src/Challenges.hs
+50
-35
50 additions, 35 deletions
cw/src/Challenges.hs
with
50 additions
and
35 deletions
cw/src/Challenges.hs
+
50
−
35
View file @
e75f3acc
...
@@ -278,10 +278,11 @@ prettyPrint (LamDef ms e) = exprBrackets e
...
@@ -278,10 +278,11 @@ 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
e
|
f
st
(
head
(
parse
expr
str
))
==
e
=
str
--omit brackets
exprBrackets
e
|
f
oldl1
(
||
)
$
map
(
e
==
)
parsed
=
str
--omit brackets
|
otherwise
=
"("
++
str
++
")"
--include brackets
|
otherwise
=
"("
++
str
++
")"
--include brackets
where
where
str
=
exprToStr
e
str
=
exprToStr
e
parsed
=
map
fst
(
parse
expr
str
)
--possible parsings of str
--converts expr to string
--converts expr to string
...
@@ -317,11 +318,25 @@ ex3'4 = LamDef [ ("F", LamAbs 1 (LamVar 1) ) ] (LamAbs 2 (LamApp (LamAbs 1 (LamV
...
@@ -317,11 +318,25 @@ ex3'4 = LamDef [ ("F", LamAbs 1 (LamVar 1) ) ] (LamAbs 2 (LamApp (LamAbs 1 (LamV
--Digit ::= “0” | “1” | “2” | “3” | “4” | “5” | “6” | “7” | “8” | “9”
--Digit ::= “0” | “1” | “2” | “3” | “4” | “5” | “6” | “7” | “8” | “9”
--MacroExpr ::= "def" MacroName "=" Expr "in" MacroExpr | Expr
--Expr ::= Var | MacroName | Applicative | Function | Expression
--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”
--Applicative ::= Expr Expr
--Function ::= “\” Var “->” Expr
--Expression ::= “(“ Expr “)”
parseLamMacro
::
String
->
Maybe
LamMacroExpr
parseLamMacro
::
String
->
Maybe
LamMacroExpr
parseLamMacro
str
|
parsed
==
[]
=
Nothing
parseLamMacro
str
|
parsed
==
[]
=
Nothing
|
otherwise
=
Just
parsed
|
otherwise
=
Just
$
fstHead
parsed
where
where
parsed
=
fst
(
head
(
parse
(
macroExpr
[]
)
str
))
--HEAD WILL NOT WORK
parsed
=
parse
(
macroExpr
[]
)
str
fstHead
=
fst
.
head
macroExpr
::
[
(
String
,
LamExpr
)
]
->
Parser
LamMacroExpr
macroExpr
::
[
(
String
,
LamExpr
)
]
->
Parser
LamMacroExpr
...
@@ -337,36 +352,16 @@ macroExpr ms = do string "def"
...
@@ -337,36 +352,16 @@ macroExpr ms = do string "def"
return
$
LamDef
ms
e
return
$
LamDef
ms
e
-- macroExpr :: Parse LamMacroExpr
-- macroExpr = do string "def"
-- name <- token macroName
-- symbol "="
-- e <- token expr
-- token $ string "in"
-- macros <- macroLoop
-- return $ LamDef macros
-- where
-- macroLoop :: Parse [(String,LamExpr)]
-- macroLoop = do string "def"
-- name <- token macroName
-- symbol "="
-- e <- token expr
-- token $ string "in"
-- ms <- many macroExpr
-- return ((name,e):ms) <|>
-- do {e <- token expr;return []}
expr
::
Parser
LamExpr
expr
::
Parser
LamExpr
expr
=
do
{
x
<-
var
;
return
$
LamVar
x
}
expr
=
do
terms
<-
some
(
token
term
)
return
$
foldl1
LamApp
terms
<|>
do
{
name
<-
macroName
;
return
$
LamMacro
name
}
<|>
do
e1
<-
expr
term
::
Parser
LamExpr
space
term
=
do
char
'('
e2
<-
expr
e
<-
token
expr
return
$
LamApp
e1
e2
char
')'
return
e
<|>
do
char
'
\\
'
<|>
do
char
'
\\
'
x
<-
var
x
<-
var
...
@@ -374,10 +369,30 @@ expr = do {x <- var; return $ LamVar x}
...
@@ -374,10 +369,30 @@ expr = do {x <- var; return $ LamVar x}
e
<-
expr
e
<-
expr
return
$
LamAbs
x
e
return
$
LamAbs
x
e
<|>
do
char
'('
<|>
do
{
x
<-
var
;
return
$
LamVar
x
}
e
<-
token
expr
char
')'
<|>
do
{
name
<-
macroName
;
return
$
LamMacro
name
}
return
e
-- expr :: Parser LamExpr
-- expr = do e1 <- expr
-- space
-- e2 <- expr
-- return $ LamApp e1 e2
-- <|> do {x <- var; return $ LamVar x}
-- <|> do {name <- macroName;return $ LamMacro name}
-- <|> do char '\\'
-- x <- var
-- symbol "->"
-- e <- expr
-- return $ LamAbs x e
-- <|> do char '('
-- e <- token expr
-- char ')'
-- return e
macroName
::
Parser
String
macroName
::
Parser
String
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment