首页
网站开发
桌面应用
管理软件
微信开发
App开发
嵌入式软件
工具软件
数据采集与分析
其他
首页
>
> 详细
代做COMP2209、代写Java,Python程序
项目预算:
开发周期:
发布时间:
要求地区:
COMP2209 Assignment Instructions
Learning Outcomes (LOs)
Understand the concept of functional programming and be able to write programs in this style,
Reason about evaluation mechanisms.
Introduction
This assignment asks you to tackle some functional programming challenges to further improve your
functional programming skills. Four of these challenges are associated with interpreting, translating,
analysing and parsing a variation of the lambda calculus. It is hoped these challenges will give you
additional insights into the mechanisms used to implement functional languages, as well as
practising some advanced functional programming techniques such as pattern matching over
recursive data types, complex recursion, and the use of monads in parsing. Your solutions need not
be much longer than a page or two, but more thought is required than with previous Haskell
programming tasks you have worked on. There are three parts to this coursework and each of them
has two challenges. Each part can be solved independently of the others and they are of varying
difficulty, thus, it is recommended that you attempt them in the order that you find easiest.
For ease of comprehension, the examples in these instructions are given in a human readable format
you may wish to code these as tests in Haskell. To assist with a semi-automated assessment of this
coursework we will provide a file called Challenges.hs. This contains Haskell code with signatures for
the methods that you are asked to develop and submit. You should edit and submit this file to
incorporate the code you have developed as solutions. However, feel free to take advantage of
Haskell development tools such as Stack or Cabal as you wish. You may and indeed should define
auxiliary or helper functions to ensure your code is easy to read and understand. You must not,
however, change the signatures of the functions that are exported for testing in Challenges.hs.
Likewise, you may not add any third-party import statements, so that you may only import modules
from the standard Haskell distribution. If you make such changes, your code may fail to compile on
the server used for automatic marking, and you will lose a significant number of marks.
There will be no published test cases for this coursework beyond the simple examples given here - as
part of the development we expect you to develop your own test cases and report on them. We will
apply our own testing code as part of the marking process. To prevent anyone from gaining
advantage from special case code, this test suite will only be published after all marking has been
completed.
It is your responsibility to adhere to the instructions specifying the behaviour of each function, and
your work will not receive full marks if you fail to do so. Your code will be tested only on values
satisfying the assumptions stated in the description of each challenge, so you can implement any
error handling you wish, including none at all. Where the specification allows more than one
possible result, any such result will be accepted. When applying our tests for marking it is possible
Module: Programming III Examiners
:
Julian Rathke,
Nick Gibbins
Assignment: Haskell Programming Challenges Effort: 30 to 60 hours
Deadline: 16:00 on 11/1/2024 Feedback: 2/2/2024 Weighting: 40%
your code will run out of space or time. A solution which fails to complete a test suite for one
exercise within 15 seconds on the test server will be deemed to have failed that exercise and will
only be eligible for partial credit. Any reasonably efficient solution should take significantly less time
than this to terminate on the actual test data that will be supplied.
Depending on your proficiency with functional programming, the time required for you to implement
and test your code is expected to be 5 to 10 hours per challenge. If you are spending much longer
than this, you are advised to consult the teaching team for advice on coding practices.
Note that this assignment involves slightly more challenging programming compared to the previous
functional programming exercises. You may benefit, therefore, from the following advice on
debugging and testing Haskell code:
https://wiki.haskell.org/Debugging
https://www.quora.com/How-do-Haskell-programmers-debug
http://book.realworldhaskell.org/read/testing-and-quality-assurance.html
It is possible you will find samples of code on the web providing similar behaviour to these
challenges. Within reason, you may incorporate, adapt and extend such code in your own
implementation. Warning: where you make use of code from elsewhere, you must acknowledge and
cite the source(s) both in your code and in the bibliography of your report. Note also that you
cannot expect to gain full credit for code you did not write yourself, and that it remains your
responsibility to ensure the correctness of your solution with respect to these instructions.
The Challenges
Part I C Circuit Puzzles
In these two challenges we will introduce a type of circuit puzzle in which the solver is presented
with a grid of "tiles" each with "wires" printed on them. The solver is then expected to rotate each
tile in the grid so that all of the wires connect together to form a complete circuit. Moreover, each
puzzle will contain at least one tile that is a "source" tile for the circuit, and at least one tile that is a
"sink" tile. A completed circuit will ensure that every sink is reachable from a source and vice-versa.
There may however be multiple sources and multiple sinks.
The grid may be of any rectangular size and will be given as a list of non-empty lists of Tile values. A
Tile value is value of the data type given by:
data Edge = North | East | South | West deriving (Eq,Ord,Show,Read)
data Tile = Source [ Edge ] | Sink [ Edge ] | Wire [ Edge ] deriving (Eq,Show,Read)
type Puzzle = [ [ Tile ] ]
where a Tile simply lists which of its edges offer connection of wires. The Source and Sink tiles must
contain at least one connector edge and Wire tiles must contain either zero (an empty Tile) or at
least two connector edges. Duplicate entries in the edges list are ignored and order does not matter.
Connector edges are considered to connect across two Tiles if they share a connector edge. For
example, a Tile offering a West connector placed to the right of a Tile offering an East connector
would have a connecting wire. A Wire Tile is connected if all of its connector edges are connected.
Similarly Source and Sink tiles are connected if, all of their connector edges are connected.
Example tiles are as follows :
Source [ West ] could be represented visually as
Sink [ North, West ] could be represented visually as
Wire [ East, South ] could be represented visually as
and finally
Wire [ North, East , West ] could be represented visually as
An example 3x3 puzzle is given below followed by a visual representation of the puzzle:
[ [ Wire [North,West] , Wire [North,South] , Source [North] ],
[ Wire [North,West], Wire [East,West], Wire [North,East] ],
[ Sink [West] , Wire [North,South] , Wire [North,West] ] ]
The following image shows a solution to the above puzzle obtained by rotating each of the Tiles.
Note the completed circuit in the solution.
Challenge 1: Completedness of circuits.
The first challenge requires you to define a function
isPuzzleComplete :: Puzzle -> Bool
that, given a list of list of tiles, simply returns whether the puzzle is completed. That is, this function
returns True if and only if all Tiles are connected, for every Source tile, there exists a path following
the wires to at least one Sink tile and for every Sink tile, there is a path following the wires to at least
one Source tile.
Challenge 2: Solve a Circuit Puzzle
This challenge requires you to define a function
solveCircuit :: Puzzle -> Maybe [ [ Rotation ] ]
where data Rotation = R0 | R90 | R180 | R270 deriving (Eq,Show,Read)
This function should, given a circuit puzzle, return Just of a grid of rotations such that, if the rotations
were applied to the corresponding Tile in the input grid, the resulting Puzzle will be a completed
circuit. Where this is not possible, the function should return the Nothing value.
The values of Rotation represent
R0 no rotation
R90 rotate Tile clockwise 90 degrees around the centre of the tile
R180 rotate Tile clockwise 180 degrees around the centre of the tile
R270 rotate Tile clockwise 270 degrees around the centre of the tile
For example,
solveCircuit [ [ Wire [North,West] , Wire [North,South] , Source [North] ], [ Wire [North,West], Wire
[East,West], Wire [North,East] ], [ Sink [West] , Wire [North,South] , Wire [North,West] ] ]
could return
Just [[R180,R90,R270],[R90,R0,R180],[R180,R90,R0]]
note that this solution is not unique.
Part II C Parsing and Printing
You should start by reviewing the material on the lambda calculus given in the lectures. You may
also review the Wikipedia article, https://en.wikipedia.org/wiki/Lambda_calculus, or Selinger's
notes http://www.mscs.dal.ca/~selinger/papers/papers/lambdanotes.pdf, or both.
For the remaining challenges we will be working with a variant of the Lambda calculus that support
let-blocks, discard binders and pairing. We call this variant Let_x and the BNF grammar for this
language is as follows
Expr ::= Var | Expr Expr | "let" Eqn "in" Expr | "(" Expr ")"
| "(" Expr "," Expr ")" | "fst" "("Expr")" | "snd" "("Expr")" | "\" VarList "->" Expr
Eqn ::= VarList "=" Expr
VarList ::= VarB | VarB VarList
VarB ::= "x" Digits | "_"
Var ::= "x" Digits
Digits ::= Digit | Digit Digits
Digit ::= "0" | "1" | "2" | "3" | "4" | "5" | "6 " | "7" | "8" | "9"
The syntax "let x1 x2 ... xN = e1 in e2" is syntactic sugar for "let x1 = \x2 -> ... -> \xN -> e1 in e2" and
the syntax "\x1 x2 ... xN e" is syntactic sugar for "\x1 -> \x2 -> ... -> xN -> e".
We can use the underscore "_" character to represent a discard binder that can be used in place of a
variable where no binding is required. Pairing of expressions is represented as "(e1,e2)" and there is
no pattern matching in this language so we use "fst" and "snd" to extract the respective components
of a paired expression. For the purposes of this coursework we limit the use of variable names in the
lambda calculus to those drawn from the set "x0 , x1, x2, x3, ... ", that is "x" followed by a natural
number. An example expression in the language is
let x2 x3 _ = x0 in fst ((x2 x4 x5 , x2 x5 x4)) snd ((x2 x4 x5 , x2 x5 x4))
Application binds tightly here and is left associative so "let x = e1 in e2 e3 e4" is to be understood as
"let x = e1 in ((e2 e3) e4)".
Challenge 3: Pretty Printing a Let_x Expression
Consider the datatypes
data LExpr = Var Int | App LExpr LExpr | Let Bind LExpr LExpr | Pair LExpr LExpr | Fst LExpr | Snd LExpr | Abs Bind LExpr
deriving (Eq,Show,Read)
data Bind = Discard | V Int
deriving (Eq,Show,Read)
We use LExpr to represent Abstract Syntax Trees (AST) of the Let_x language.
This challenge requires you to write a function that takes the AST of a Let_x expression and to "pretty
print" it by returning a string representation the expression. That is, define a function
prettyPrint :: LExpr -> String
that outputs a syntactically correct expression of Let_x. Your solution should omit brackets where
these are not required and the output string should parse to the same abstract syntax tree as the
given input. Finally, your solution should print using sugared syntax where possible. For example, an
expression given as Let (V 1) (Abs (V 2) (Abs Discard e1)) e2 should be printed as "let x1 x2 _ =
in
" where e1 and e2 are expressions and
and
are their pretty print strings.
Beyond that you are free to format and lay out the expression as you choose in order to make it
shorter or easier to read or both.
Example usages of pretty printing (showing the single \ escaped using \\) are:
Challenge 4: Parsing Let_x Expressions
In this Challenge we will write a parser for the Let_x language using the datatype LExpr given above.
Your challenge is to define a function:
parseLetx :: String -> Maybe LExpr
that returns Nothing if the given string does not parse correctly according to the rules of the
concrete grammar for Let_x and returns a valid Abstract Syntax Tree otherwise.
You are recommended to adapt the monadic parser examples published by Hutton and Meijer. You
should start by following the COMP2209 lecture on Parsing, reading the monadic parser tutorial by
Hutton in Chapter 13 of his Haskell textbook, and/or the on-line tutorial below:
http://www.cs.nott.ac.uk/~pszgmh/pearl.pdf on-line tutorial
Example usages of the parsing function are:
App (Abs (V 1) (Var 1)) (Abs (V 1) (Var 1))"(\\x1 -> x1) \\x1 -> x1"
Let Discard (Var 0) (Abs (V 1) (App (Var 1) (Abs (V 1) (Var 1)))) "let _ = x0 in \\x1 -> x1 \\x1 -> x1"
Abs (V 1) (Abs Discard (Abs (V 2) (App (Var 2 ) (Var 1 ) ) ) ) "\\x1 _ x2 -> x2 x1"
App (Var 2) (Abs (V 1) (Abs Discard (Var 1))) "x2 \\x1 _ -> x1"
parseLetx "x1 (x2 x3)" Just (App (Var 1) (App (Var 2) (Var 3)))
parseLetx "x1 x2 x3" Just (App (App (Var 1) (Var 2)) (Var 3))
parseLetx "let x1 x3 = x2 in x1 x2" Just (Let (V 1) (Abs (V 3) (Var 2)) (App (Var 1) (Var 2)))
parseLetx "let x1 _ x3 = x3 in \\x3 ->
x1 x3 x3"
Just (Let (V 1) (Abs Discard (Abs (V 3) (Var 3)))
(Abs (V 3) (App (App (Var 1) (Var 3)) (Var 3))))
Part III C Encoding Let_x in Lambda Calculus
It is well known that the Lambda Calculus can be used to encode many programming constructs. In
particular, to encode a let blocks we simply use application
is encoded as (\x0 ->
)
where
and
are the encodings of
e1 and e2 respectively.
To encode the discard binder we simply need to choose a suitable variable with which to replace it:
<\ _ -> e1 > is encoded as (\xN ->
) where xN is chosen so as to not interfere with
Finally, pairing can be encoded as follows:
< (e1 , e2)> is encoded as (\xN- > xN
) where xN does not interfere with
and
and
is encoded as
(\x0 - > \x1 - > x0)
is encoded as
(\x0 - > \x1 - > x1)
Challenge 5: Converting Arithmetic Expressions to Lambda Calculus
Given the datatype
data LamExpr = LamVar Int | LamApp LamExpr LamExpr | LamAbs Int LamExpr
deriving (Eq,Show,Read)
Write a function
letEnc :: LExpr -> LamExpr
that translates an arithmetic expression in to a lambda calculus expression according to the above
translation rules. The lambda expression returned by your function may use any naming of the
bound variables provided the given expression is alpha-equivalent to the intended output.
Usage of the letEnc function on the examples show above is as follows:
letEnc (Let Discard (Abs (V 1) (Var 1)) (Abs (V 1) (Var 1)) LamApp (LamAbs 0 (LamAbs 2
(LamVar 2))) (LamAbs 2 (LamVar
2))
letEnc (Fst (Pair (Abs (V 1) (Var 1)) (Abs Discard (Var 2)))) LamApp (LamAbs 0 (LamApp
(LamApp (LamVar 0) (LamAbs 2
(LamVar 2))) (LamAbs 0 (LamVar
2)))) (LamAbs 0 (LamAbs 1
(LamVar 0)))
Challenge 6: Counting and Comparing Let_x Reductions
For this challenge you will define functions to perform reduction of Let_x expressions. We will
implement both a call-by-value and a call-by-name reduction strategy. A good starting point is to
remind yourself of the definitions of call-by-value and call-by-name evaluation in Lecture 34 -
Evaluation Strategies.
We are going to compare the differences between the lengths of reduction sequences to
terminated for both call-by-value and call-by-name reduction for a given Let_x expression and the
lambda expression obtained by converting the Let_x expression to a lambda expression as defined in
Challenge 5. For the purposes of this coursework, we will consider an expression to have terminated
for a given strategy if it simply has no further reduction steps according to that strategy. For example,
blocked terms such as "x1 x2" are considered as terminated.
In order to understand evaluation in the language of Let_x expressions, we need to identify the
redexes of that language. The relevant reduction rules are as follows:
also note that, in the expressions "let x1 = e1 in e2" or "let _ = e1 in e2" the expression "e2" occurs
underneath a binding operation and therefore, similarly to "\x1 -> e2", according to both call-by-
value and call-by-name strategies, reduction in "e2" is suspended until the binder is resolved.
Define a function:
compareRedn :: LExpr -> Int -> ( Int, Int , Int, Int )
that takes a Let_x expression and upper bound for the number of steps to be counted and returns a
4-tuple containing the length of four reduction sequences. In each case the number returned should
be the minimum of the upper bound and the number of steps needed for the expression to
terminate. Given an input Let_x expression E, the pair should contain lengths of reduction
sequences for (in this order) :
1. termination using call-by-value reduction on E
2. termination using call-by-value reduction on the lambda calculus translation of E
3. termination using call-by-name reduction on E
4. termination using call-by-name reduction on the lambda calculus translation of E
Example usages of the compareRedn function are:
compareRedn (Let (V 3) (Pair (App (Abs (V 1) (App (Var 1) (Var
1))) (Abs (V 2) (Var 2))) (App (Abs (V 1) (App (Var 1) (Var 1))) (Abs
(V 2) (Var 2)))) (Fst (Var 3))) 10
(6,8,4,6)
compareRedn (Let Discard (App (Abs (V 1) (Var 1)) (App (Abs
(V 1) (Var 1)) (Abs (V 1) (Var 1)))) (Snd (Pair (App (Abs (V 1)
(Var 1)) (Abs (V 1) (Var 1))) (Abs (V 1) (Var 1))))) 10
(5,7,2,4)
compareRedn (Let (V 2) (Let (V 1) (Abs (V 0) (App (Var 0) (Var
0))) (App (Var 1) (Var 1))) (Snd (Pair (Var 2) (Abs (V 1) (Var
1))))) 100
(100,100,2,4)
Implementation, Test File and Report
In addition to your solutions to these programming challenges, you are asked to submit an additional
Tests.hs file with your own tests, and a report:
You are expected to test your code carefully before submitting it and we ask that you write a report
on your development strategy. Your report should include an explanation of how you implemented
and tested your solutions. Your report should be up to 1 page (400 words). Note that this report is
not expected to explain how your code works, as this should be evident from your commented code
itself. Instead you should cover the development and testing tools and techniques you used, and
comment on their effectiveness.
Your report should include a second page with a bibliography listing the source(s) for any fragments
of code written by other people that you have adapted or included directly in your submission.
Submission and Marking
Your Haskell solutions should be submitted as a single plain text file Challenges.hs. Your tests should
also be submitted as a plain text file Tests.hs. Finally, your report should be submitted as a PDF file,
Report.pdf.
The marking scheme is given in the appendix below. There are up to 5 marks for your solution to
each of the programming challenges, up to 5 for your explanation of how you implemented and
tested these, and up to 5 for your coding style. This gives a maximum of 40 marks for this
assignment, which is worth 40% of the module.
Your solutions to these challenges will be subject to automated testing so it is important that you
adhere to the type definitions and type signatures given in the supplied dummy code file
Challenges.hs. Do not change the list of functions and types exported by this file. Your code will be
run using a command line such as ghc Ce main CW2TestSuite.hs, where CW2TestSuite.hs is my test
harness that imports Challenge.hs. You should check before you submit that your solution compiles
and runs as expected.
The supplied Parsing.hs file will be present so it is safe to import this and any library included in the
standard Haskell distribution (Version 8.10.7). Third party libraries will not be present so do not
import these. We will not compile and execute your Tests.hs file when marking.
Appendix: Marking Scheme
Guidance on Coding Style and Readability
Grade Functional Correctness Readability and Coding Style Development & Testing
软件开发、广告设计客服
QQ:99515681
邮箱:99515681@qq.com
工作时间:8:00-23:00
微信:codinghelp
热点项目
更多
urba6006代写、java/c++编程语...
2024-12-26
代做program、代写python编程语...
2024-12-26
代写dts207tc、sql编程语言代做
2024-12-25
cs209a代做、java程序设计代写
2024-12-25
cs305程序代做、代写python程序...
2024-12-25
代写csc1001、代做python设计程...
2024-12-24
代写practice test preparatio...
2024-12-24
代写bre2031 – environmental...
2024-12-24
代写ece5550: applied kalman ...
2024-12-24
代做conmgnt 7049 – measurem...
2024-12-24
代写ece3700j introduction to...
2024-12-24
代做adad9311 designing the e...
2024-12-24
代做comp5618 - applied cyber...
2024-12-24
热点标签
mktg2509
csci 2600
38170
lng302
csse3010
phas3226
77938
arch1162
engn4536/engn6536
acx5903
comp151101
phl245
cse12
comp9312
stat3016/6016
phas0038
comp2140
6qqmb312
xjco3011
rest0005
ematm0051
5qqmn219
lubs5062m
eee8155
cege0100
eap033
artd1109
mat246
etc3430
ecmm462
mis102
inft6800
ddes9903
comp6521
comp9517
comp3331/9331
comp4337
comp6008
comp9414
bu.231.790.81
man00150m
csb352h
math1041
eengm4100
isys1002
08
6057cem
mktg3504
mthm036
mtrx1701
mth3241
eeee3086
cmp-7038b
cmp-7000a
ints4010
econ2151
infs5710
fins5516
fin3309
fins5510
gsoe9340
math2007
math2036
soee5010
mark3088
infs3605
elec9714
comp2271
ma214
comp2211
infs3604
600426
sit254
acct3091
bbt405
msin0116
com107/com113
mark5826
sit120
comp9021
eco2101
eeen40700
cs253
ece3114
ecmm447
chns3000
math377
itd102
comp9444
comp(2041|9044)
econ0060
econ7230
mgt001371
ecs-323
cs6250
mgdi60012
mdia2012
comm221001
comm5000
ma1008
engl642
econ241
com333
math367
mis201
nbs-7041x
meek16104
econ2003
comm1190
mbas902
comp-1027
dpst1091
comp7315
eppd1033
m06
ee3025
msci231
bb113/bbs1063
fc709
comp3425
comp9417
econ42915
cb9101
math1102e
chme0017
fc307
mkt60104
5522usst
litr1-uc6201.200
ee1102
cosc2803
math39512
omp9727
int2067/int5051
bsb151
mgt253
fc021
babs2202
mis2002s
phya21
18-213
cege0012
mdia1002
math38032
mech5125
07
cisc102
mgx3110
cs240
11175
fin3020s
eco3420
ictten622
comp9727
cpt111
de114102d
mgm320h5s
bafi1019
math21112
efim20036
mn-3503
fins5568
110.807
bcpm000028
info6030
bma0092
bcpm0054
math20212
ce335
cs365
cenv6141
ftec5580
math2010
ec3450
comm1170
ecmt1010
csci-ua.0480-003
econ12-200
ib3960
ectb60h3f
cs247—assignment
tk3163
ics3u
ib3j80
comp20008
comp9334
eppd1063
acct2343
cct109
isys1055/3412
math350-real
math2014
eec180
stat141b
econ2101
msinm014/msing014/msing014b
fit2004
comp643
bu1002
cm2030
联系我们
- QQ: 9951568
© 2021
www.rj363.com
软件定制开发网!