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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ik1g19
prog3-coursework
Commits
5966762c
Commit
5966762c
authored
4 years ago
by
ik1g19
Browse files
Options
Downloads
Patches
Plain Diff
attempted fixing 6. it didnt work.
parent
cd38cc6f
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
cw/src/Challenges.hs
+34
-14
34 additions, 14 deletions
cw/src/Challenges.hs
with
34 additions
and
14 deletions
cw/src/Challenges.hs
+
34
−
14
View file @
5966762c
...
@@ -41,6 +41,11 @@ data LamExpr = LamMacro String | LamApp LamExpr LamExpr |
...
@@ -41,6 +41,11 @@ data LamExpr = LamMacro String | LamApp LamExpr LamExpr |
-- ADD YOUR OWN CODE HERE
-- ADD YOUR OWN CODE HERE
{--| The following code is copyright of the University of Southampton |--
--| The author of this code is Isaac Klugman |--}
------------------------------------------Challenge 1------------------------------------------------- -- | inline comments
------------------------------------------Challenge 1------------------------------------------------- -- | inline comments
...
@@ -271,13 +276,23 @@ macroDef [] = ""
...
@@ -271,13 +276,23 @@ macroDef [] = ""
macroDef
(
(
name
,
expr
)
:
ms
)
=
foldl1
(
++
)
[
"def "
,
name
,
" = "
,
exprToStr
[]
expr
,
" in "
,
macroDef
ms
]
macroDef
(
(
name
,
expr
)
:
ms
)
=
foldl1
(
++
)
[
"def "
,
name
,
" = "
,
exprToStr
[]
expr
,
" in "
,
macroDef
ms
]
-- {--| replaces a macro with its definition |--}
-- catchMacroStr :: [(String,LamExpr)] -> LamExpr -> String
-- catchMacro ms e | macros == [] = exprToStr ms e
-- | otherwise = fst $ head macros
-- where
-- macros = filter ( (e==) . snd ) ms
{--| replaces a macro with its definition |--}
{--| replaces a macro with its definition |--}
catchMacro
::
[(
String
,
LamExpr
)]
->
LamExpr
->
String
catchMacro
Expr
::
[(
String
,
LamExpr
)]
->
LamExpr
->
LamExpr
catchMacro
ms
e
|
macros
==
[]
=
e
xprToStr
ms
e
catchMacro
Expr
ms
e
|
macros
==
[]
=
e
|
otherwise
=
fst
$
head
macros
|
otherwise
=
LamMacro
$
(
fst
.
head
)
macros
where
where
macros
=
filter
(
(
e
==
)
.
snd
)
ms
macros
=
filter
(
(
e
==
)
.
snd
)
ms
catchAndConvert
::
[(
String
,
LamExpr
)]
->
LamExpr
->
String
catchAndConvert
ms
e
=
exprToStr
ms
$
catchMacroExpr
ms
e
{--| converts expr to str |--
{--| converts expr to str |--
--| params: |--
--| params: |--
...
@@ -285,22 +300,26 @@ catchMacro ms e | macros == [] = exprToStr ms e
...
@@ -285,22 +300,26 @@ catchMacro ms e | macros == [] = exprToStr ms e
--| -expr to convert |--
--| -expr to convert |--
--| returns expr in string form |--}
--| returns expr in string form |--}
exprToStr
::
[(
String
,
LamExpr
)]
->
LamExpr
->
String
exprToStr
::
[(
String
,
LamExpr
)]
->
LamExpr
->
String
exprToStr
ms
e
@
(
LamApp
e1
e2
)
|
e
==
eNone
=
none
exprToStr
ms
e
@
(
LamApp
e1
e2
)
|
eMacros
==
eNone
=
none
|
e
==
eLeft
=
left
|
eMacros
==
eLeft
=
left
|
e
==
eRight
=
right
|
eMacros
==
eRight
=
right
|
e
==
eBoth
=
both
|
eMacros
==
eBoth
=
both
|
otherwise
=
"fuck"
where
where
none
=
foldl1
(
++
)
[
catchMacro
ms
e1
,
" "
,
catchMacro
ms
e2
]
-- | applying different uses of parenthese to
eMacros
=
LamApp
(
catchMacroExpr
ms
e1
)
(
catchMacroExpr
ms
e2
)
-- | when testing for parentheses, macros will be replaced
left
=
foldl1
(
++
)
[
"("
,
catchMacro
ms
e1
,
") "
,
catchMacro
ms
e2
]
-- | determine when they are necessary
-- | so macros must be replaced in the original expr
right
=
foldl1
(
++
)
[
catchMacro
ms
e1
,
" ("
,
catchMacro
ms
e2
,
")"
]
both
=
foldl1
(
++
)
[
"("
,
catchMacro
ms
e1
,
") ("
,
catchMacro
ms
e2
,
")"
]
none
=
foldl1
(
++
)
[
catchAndConvert
ms
e1
,
" "
,
catchAndConvert
ms
e2
]
-- | applying different uses of parenthese to
left
=
foldl1
(
++
)
[
"("
,
catchAndConvert
ms
e1
,
") "
,
catchAndConvert
ms
e2
]
-- | determine when they are necessary
right
=
foldl1
(
++
)
[
catchAndConvert
ms
e1
,
" ("
,
catchAndConvert
ms
e2
,
")"
]
both
=
foldl1
(
++
)
[
"("
,
catchAndConvert
ms
e1
,
") ("
,
catchAndConvert
ms
e2
,
")"
]
Just
(
LamDef
_
eNone
)
=
parseLamMacro
none
Just
(
LamDef
_
eNone
)
=
parseLamMacro
none
Just
(
LamDef
_
eLeft
)
=
parseLamMacro
left
Just
(
LamDef
_
eLeft
)
=
parseLamMacro
left
Just
(
LamDef
_
eRight
)
=
parseLamMacro
right
Just
(
LamDef
_
eRight
)
=
parseLamMacro
right
Just
(
LamDef
_
eBoth
)
=
parseLamMacro
both
Just
(
LamDef
_
eBoth
)
=
parseLamMacro
both
exprToStr
ms
(
LamAbs
x
e
)
=
"
\\
x"
++
show
x
++
" -> "
++
catch
Macro
ms
e
exprToStr
ms
(
LamAbs
x
e
)
=
"
\\
x"
++
show
x
++
" -> "
++
catch
AndConvert
ms
e
exprToStr
ms
(
LamVar
x
)
=
"x"
++
show
x
exprToStr
ms
(
LamVar
x
)
=
"x"
++
show
x
exprToStr
ms
(
LamMacro
m
)
=
m
exprToStr
ms
(
LamMacro
m
)
=
m
...
@@ -569,10 +588,11 @@ redex (LamApp e1 e2) = redex e1 || redex e2
...
@@ -569,10 +588,11 @@ redex (LamApp e1 e2) = redex e1 || redex e2
eval1cbv
::
[
(
String
,
LamExpr
)
]
->
LamExpr
->
LamExpr
eval1cbv
::
[
(
String
,
LamExpr
)
]
->
LamExpr
->
LamExpr
eval1cbv
ms
(
LamAbs
x
e
)
=
LamAbs
x
e
eval1cbv
ms
(
LamAbs
x
e
)
=
LamAbs
x
e
eval1cbv
ms
(
LamMacro
name
)
=
(
snd
.
head
)
$
filter
((
name
==
)
.
fst
)
ms
eval1cbv
ms
(
LamApp
(
LamAbs
x
e1
)
e
@
(
LamAbs
y
e2
))
=
subst
e1
x
e
eval1cbv
ms
(
LamApp
(
LamAbs
x
e1
)
e
@
(
LamAbs
y
e2
))
=
subst
e1
x
e
eval1cbv
ms
(
LamApp
e1
@
(
LamAbs
x
e
)
e2
)
=
LamApp
e1
$
eval1cbv
ms
e2
eval1cbv
ms
(
LamApp
e1
@
(
LamAbs
x
e
)
e2
)
=
LamApp
e1
(
eval1cbv
ms
e2
)
eval1cbv
ms
(
LamApp
e1
e2
)
=
LamApp
(
eval1cbv
ms
e1
)
e2
eval1cbv
ms
(
LamApp
e1
e2
)
=
LamApp
(
eval1cbv
ms
e1
)
e2
eval1cbv
ms
(
LamMacro
name
)
=
(
snd
.
head
)
$
filter
((
name
==
)
.
fst
)
ms
eval1cbv
ms
(
LamVar
x
)
=
LamVar
x
eval1cbn
::
[
(
String
,
LamExpr
)
]
->
LamExpr
->
LamExpr
eval1cbn
::
[
(
String
,
LamExpr
)
]
->
LamExpr
->
LamExpr
...
...
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