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
6816f582
Commit
6816f582
authored
4 years ago
by
ik1g19
Browse files
Options
Downloads
Patches
Plain Diff
started part 2
parent
0a67b288
Branches
Branches containing commit
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
+52
-31
52 additions, 31 deletions
cw/src/Challenges.hs
with
52 additions
and
31 deletions
cw/src/Challenges.hs
+
52
−
31
View file @
6816f582
...
@@ -38,6 +38,7 @@ data LamExpr = LamMacro String | LamApp LamExpr LamExpr |
...
@@ -38,6 +38,7 @@ data LamExpr = LamMacro String | LamApp LamExpr LamExpr |
-- Challenge 1 --
-- Challenge 1 --
solveWordSearch
::
[
String
]
->
WordSearchGrid
->
[
(
String
,
Maybe
Placement
)
]
solveWordSearch
::
[
String
]
->
WordSearchGrid
->
[
(
String
,
Maybe
Placement
)
]
solveWordSearch
ss
css
=
map
(
findString
css
)
ss
solveWordSearch
ss
css
=
map
(
findString
css
)
ss
...
@@ -94,12 +95,13 @@ nextPos DownBack (x,y) = (x-1,y+1)
...
@@ -94,12 +95,13 @@ nextPos DownBack (x,y) = (x-1,y+1)
elemAt
::
[[
a
]]
->
Posn
->
a
elemAt
::
[[
a
]]
->
Posn
->
a
elemAt
ass
(
x
,
y
)
=
(
ass
!!
y
)
!!
x
--ass means list of list of a's,
not associated with any other meaning
elemAt
ass
(
x
,
y
)
=
(
ass
!!
y
)
!!
x
--ass means list of list of a's,
--not associated with any other meaning
--returns specified adjacent element in grid, relative to given position
--returns specified adjacent element in grid, relative to given position
nextElem
::
[[
a
]]
->
Posn
->
Orientation
->
Maybe
a
nextElem
::
[[
a
]]
->
Posn
->
Orientation
->
Maybe
a
nextElem
css
(
x
,
y
)
dir
|
x'
<
0
||
y'
<
0
||
x'
>
length
css
-
1
||
y'
>
length
css
-
1
=
Nothing
nextElem
css
(
x
,
y
)
dir
|
x'
<
0
||
y'
<
0
||
x'
>
length
css
-
1
||
y'
>
length
css
-
1
=
Nothing
|
otherwise
=
Just
(
elemAt
css
(
x'
,
y'
))
|
otherwise
=
Just
(
elemAt
css
(
x'
,
y'
))
where
where
(
x'
,
y'
)
=
nextPos
dir
(
x
,
y
)
(
x'
,
y'
)
=
nextPos
dir
(
x
,
y
)
...
@@ -107,19 +109,28 @@ nextElem css (x,y) dir | x' < 0 || y' < 0 || x' > length css - 1 || y' > length
...
@@ -107,19 +109,28 @@ nextElem css (x,y) dir | x' < 0 || y' < 0 || x' > length css - 1 || y' > length
-- Two examples for you to try out, the first of which is in the instructions
-- Two examples for you to try out, the first of which is in the instructions
exGrid1'1
=
[
"HAGNIRTSH"
,
"SACAGETAK"
,
"GCSTACKEL"
,
"MGHKMILKI"
,
"EKNLETGCN"
,
"TNIRTLETE"
,
"IRAAHCLSR"
,
"MAMROSAGD"
,
"GIZKDDNRG"
]
exGrid1'1
=
[
"HAGNIRTSH"
,
"SACAGETAK"
,
"GCSTACKEL"
,
"MGHKMILKI"
,
"EKNLETGCN"
,
"TNIRTLETE"
,
"IRAAHCLSR"
,
"MAMROSAGD"
,
"GIZKDDNRG"
]
exWords1'1
=
[
"HASKELL"
,
"STRING"
,
"STACK"
,
"MAIN"
,
"METHOD"
]
exWords1'1
=
[
"HASKELL"
,
"STRING"
,
"STACK"
,
"MAIN"
,
"METHOD"
]
exGrid1'2
=
[
"ROBREUMBR"
,
"AURPEPSAN"
,
"UNLALMSEE"
,
"YGAUNPYYP"
,
"NLMNBGENA"
,
"NBLEALEOR"
,
"ALRYPBBLG"
,
"NREPBEBEP"
,
"YGAYAROMR"
]
exGrid1'2
=
[
"ROBREUMBR"
,
"AURPEPSAN"
,
"UNLALMSEE"
,
exWords1'2
=
[
"BANANA"
,
"ORANGE"
,
"MELON"
,
"RASPBERRY"
,
"APPLE"
,
"PLUM"
,
"GRAPE"
]
"YGAUNPYYP"
,
"NLMNBGENA"
,
"NBLEALEOR"
,
"ALRYPBBLG"
,
"NREPBEBEP"
,
"YGAYAROMR"
]
exWords1'2
=
[
"BANANA"
,
"ORANGE"
,
"MELON"
,
"RASPBERRY"
,
"APPLE"
,
"PLUM"
,
"GRAPE"
]
-- Challenge 2 --
-- Challenge 2 --
--internal grid values are either a character or a placeholder for a random letter
--internal grid values are either a character or a placeholder for a random letter
data
GridVal
=
Letter
Char
|
Rand
deriving
Eq
data
GridVal
=
Letter
Char
|
Rand
deriving
Eq
type
RandGrid
=
[[
GridVal
]]
type
RandGrid
=
[[
GridVal
]]
createWordSearch
::
[
String
]
->
Double
->
IO
WordSearchGrid
createWordSearch
::
[
String
]
->
Double
->
IO
WordSearchGrid
createWordSearch
ss
den
=
do
gen
<-
newStdGen
--initial generator
createWordSearch
ss
den
=
do
gen
<-
newStdGen
--initial generator
return
(
createGrid
dim
gen
ss
)
return
(
createGrid
dim
gen
ss
)
...
@@ -238,6 +249,7 @@ generatePos gen dim = let (x,gen') = randomR (0,dim - 1) gen :: (Int,StdGen)
...
@@ -238,6 +249,7 @@ generatePos gen dim = let (x,gen') = randomR (0,dim - 1) gen :: (Int,StdGen)
in
((
x
,
y
),
gen''
)
in
((
x
,
y
),
gen''
)
--- Convenience functions supplied for testing purposes
--- Convenience functions supplied for testing purposes
createAndSolve
::
[
String
]
->
Double
->
IO
[
(
String
,
Maybe
Placement
)
]
createAndSolve
::
[
String
]
->
Double
->
IO
[
(
String
,
Maybe
Placement
)
]
createAndSolve
words
maxDensity
=
do
g
<-
createWordSearch
words
maxDensity
createAndSolve
words
maxDensity
=
do
g
<-
createWordSearch
words
maxDensity
...
@@ -250,31 +262,40 @@ printGrid [] = return ()
...
@@ -250,31 +262,40 @@ printGrid [] = return ()
printGrid
(
w
:
ws
)
=
do
putStrLn
w
printGrid
(
w
:
ws
)
=
do
putStrLn
w
printGrid
ws
printGrid
ws
-- createWordSearch :: [ String ] -> Double -> IO WordSearchGrid
-- createWordSearch ss den = return []
-- generatePos :: Int -> StdGen -> IO (Int,Int)
-- Challenge 3 --
-- generatePos dim gen = do let (x,newGen) = randomR (1,dim) gen :: (Int,StdGen)
-- let (y,_) = randomR (1,dim) newGen :: (Int,StdGen)
-- return (x,y)
-- generatePos :: StdGen -> Int -> (Int,Int)
-- data LamMacroExpr = LamDef [ (String,LamExpr) ] LamExpr deriving (Eq,Show,Read)
-- generatePos gen dim = let (x,gen') = randomR (1,dim) gen :: (Int,StdGen)
-- data LamExpr = LamMacro String | LamApp LamExpr LamExpr |
-- (y,gen'') = randomR (1,dim) gen' :: (Int,StdGen)
-- LamAbs Int LamExpr | LamVar Int deriving (Eq,Show,Read)
-- in (x,y)
prettyPrint
::
LamMacroExpr
->
String
prettyPrint
(
LamDef
ms
expr
)
=
exprBrackets
expr
-- Challenge 3 --
--applies brackets to expr if needed
exprBrackets
::
LamExpr
->
String
exprBrackets
expr
|
parseExpr
str
==
expr
=
str
--omit brackets
|
otherwise
=
"("
+
str
++
")"
--include brackets
where
str
=
exprToStr
expr
--converts expr to string
exprToStr
::
LamExpr
->
String
exprToStr
(
LamApp
expr1
expr2
)
=
exprBrackets
expr1
++
" "
++
exprBrackets
expr2
exprToStr
(
LamAbs
x
expr
)
=
"
\\
x"
++
show
x
++
" -> "
++
exprBrackets
expr
exprToStr
(
LamVar
x
)
=
"x"
++
show
x
exprToStr
(
LamMacro
m
)
=
m
prettyPrint
::
LamMacroExpr
->
String
prettyPrint
_
=
""
-- examples in the instructions
-- examples in the instructions
ex3'1
=
LamDef
[]
(
LamApp
(
LamAbs
1
(
LamVar
1
))
(
LamAbs
1
(
LamVar
1
)))
ex3'1
=
LamDef
[]
(
LamApp
(
LamAbs
1
(
LamVar
1
))
(
LamAbs
1
(
LamVar
1
)))
--"(\x1 -> x1) \x1 -> x1"
ex3'2
=
LamDef
[]
(
LamAbs
1
(
LamApp
(
LamVar
1
)
(
LamAbs
1
(
LamVar
1
))))
ex3'2
=
LamDef
[]
(
LamAbs
1
(
LamApp
(
LamVar
1
)
(
LamAbs
1
(
LamVar
1
))))
--"\x1 -> x1 \x1 -> x1"
ex3'3
=
LamDef
[
(
"F"
,
LamAbs
1
(
LamVar
1
)
)
]
(
LamAbs
2
(
LamApp
(
LamVar
2
)
(
LamMacro
"F"
)))
ex3'3
=
LamDef
[
(
"F"
,
LamAbs
1
(
LamVar
1
)
)
]
(
LamAbs
2
(
LamApp
(
LamVar
2
)
(
LamMacro
"F"
)))
--"def F = \x1-> x1 in \x2 -> x2 F"
ex3'4
=
LamDef
[
(
"F"
,
LamAbs
1
(
LamVar
1
)
)
]
(
LamAbs
2
(
LamApp
(
LamAbs
1
(
LamVar
1
))
(
LamVar
2
)))
ex3'4
=
LamDef
[
(
"F"
,
LamAbs
1
(
LamVar
1
)
)
]
(
LamAbs
2
(
LamApp
(
LamAbs
1
(
LamVar
1
))
(
LamVar
2
)))
--"def F = \x1-> x1 in \x2-> F x2"
-- Challenge 4 --
-- Challenge 4 --
...
...
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