Haskell code

In the previous assignment, you defined a stub for a function myMagicSolver that searches for an optimal solution to a Rush Hour puzzle. Your task in this assignment is to implement this function. In principle, that’s it. Go! I suspect it may help to break this task down a bit, to give you an idea of how you may structure your solution. As discussed in the previous assignment, the general strategy is to maintain a frontier, the list of board states on the current level of your breadth-first search. The search is then a repetition of the following process: Given the current frontier, check whether it contains a solution, a configuration where the red car is free. If so, your search should return the path of moves you followed to reach this configuration, so every state in your frontier also needs to store the path of moves you followed to reach this configuration. If there is no solution in the current frontier, then you need to construct the next frontier, the next level in your breadth-first search. To do this, you generate the list of configurations reachable by moving a single car from each configuration in your current frontier. You need to remove duplicates from the resulting list of configurations, and you need to remove all configurations you have already seen on previous levels. The result is the next frontier. You call your function recursively on this new frontier. To check which configurations you have seen already, the set of configurations you have seen already as an argument to each recursive call. As I said in the previous assignment, an efficient implementation should represent this as an actual set type from Data.Set. You can use a list, but checking whether a list contains some element is slow if the list is big. So here are some suggestions for functions you may need. You can structure your code differently or choose to name these function whatever you like. I do not provide type signatures for these functions because that’s part of what you need to figure out.

Assignment 8
CSCI 3136: Principles of Programming Languages
Due April 8, 2021
Assignments are due on the due date before 23:59. Plagiarism in assignment answers will not be tolerated. By submitting
their answers to this assignment, the authors named above declare that its content is their original work and that they did
not use any sources for its preparation other than the class notes, the textbook, and ones explicitly acknowledged in the
answers. Any suspected act of plagiarism will be reported to the Faculty’s Academic Integrity Officer and possibly to the
Senate Discipline Committee. The penalty for academic dishonesty may range from failing the course to expulsion from the
university, in accordance with Dalhousie University’s regulations regarding academic integrity.
Rush Hour
This assignment is a continuation of Assignment 6, where you developed the data types necessary to
implement a solver for the Rush Hour puzzle. See that assignment for a description of the puzzle and
for an overview of a solution strategy.
Submission Instructions
As in the previous assignment, submit your assignment in a single ZIP-file named in the form
Banner_LastName_FirstName.zip, where Banner, Lastname, and FirstName are your banner number,
last name, and first name. The file should contain the follow directory structure:
.
Banner_LastName_FirstName
RushHour.hs
Compiling and Running Your Code
Since I did not teach you how to work in the IO monad, which is necessary to read files and write to
stdout, I am providing a skeleton project within which to embed your code. This skeleton project
is provided as part of this assignment. If you look inside the src folder of this project, it already
contains a file RushHour.hs. This is the file where you need to add your code. To submit it following
the instructions above, copy it into a separate folder Banner_LastName_FirstName outside the skeleton
project and submit it. To compile and run your code, you need the RushHour.hs file to be inside the
src folder of the project I provided.
You compile the code and check for any compilation errors you encounter using
$ stack build
This command works anywhere inside the rush hour project directory and any of its subdirectories.
To run the code, use
$ stack run — <puzzle number>
This again works inside the rush hour project directory. This time, however, you must run it from the
top project directory (the one containing package.yaml, stack.yaml, and rush_no_walls.txt). This
is because the path to rush_no_walls.txt is hardcoded into the skeleton code I provided to you. Also,
this will of course work only once you have implemented a functioning rush hour solver.
1
This Assignment: Implement the Solver
In the previous assignment, you defined a stub for a function myMagicSolver that searches for an
optimal solution to a Rush Hour puzzle. Your task in this assignment is to implement this function. In
principle, that’s it. Go!
I suspect it may help to break this task down a bit, to give you an idea of how you may structure your
solution.
As discussed in the previous assignment, the general strategy is to maintain a frontier, the list of
board states on the current level of your breadth-first search. The search is then a repetition of the
following process: Given the current frontier, check whether it contains a solution, a configuration
where the red car is free. If so, your search should return the path of moves you followed to reach this
configuration, so every state in your frontier also needs to store the path of moves you followed to reach
this configuration. If there is no solution in the current frontier, then you need to construct the next
frontier, the next level in your breadth-first search. To do this, you generate the list of configurations
reachable by moving a single car from each configuration in your current frontier. You need to remove
duplicates from the resulting list of configurations, and you need to remove all configurations you have
already seen on previous levels. The result is the next frontier. You call your function recursively on
this new frontier. To check which configurations you have seen already, you also need to pass the set of
configurations you have seen already as an argument to each recursive call. As I said in the previous
assignment, an efficient implementation should represent this as an actual set type from Data.Set.
You can use a list, but checking whether a list contains some element is slow if the list is big.
So here are some suggestions for functions you may need. You can structure your code differently or
choose to name these function whatever you like. I do not provide type signatures for these functions
because that’s part of what you need to figure out.
The Main “Loop”
search
This function should take the current frontier and the list of configurations seen so far as an argument.
Its return value should be the computed solution. To “iterate” over the frontiers, you generate the next
frontier from the current frontier and then call search recursively with the new frontier and the new
set of seen states as arguments.
Computing The Next Frontier
The search function is easy to implement if you have two helper functions. One is a function that tests
whether a given board state is a solved state (the red car is free). See below. The other one generates
the next frontier from the current frontier:
nextLevel
This function takes the current frontier and the list of seen states as argument and returns the next
frontier and the new set of seen states.
2
To implement the next level, it is useful to have a helper function neighbors that takes a single board state as an argument and returns the list of all states reachable from it in a single move. By collecting the results of applying this function to all states in the current frontier, you get the
list of candidate states for the next frontier. From this candidate list, you need to eliminate duplicates
and states you have seen already.
The implementation of neighbors can be further reduced to a helper function
move pieces
which takes a state, the number of a piece, and the offset by which to move the piece as an argument.
The result is the state obtained by moving this piece by the specified offset. In my implementation, move pieces do not ensure that the move is valid. It may lead to a configuration where pieces overlap
and, even if they don’t, it may be necessary for a piece to “jump” over another piece that is in the
way to reach the new configuration. My implementation filters the list of proposed moves using the
predicates discussed next.
Basic Queries of Board States
In my implementation, I mainly have two predicates to inspect a board state:
solved
takes a board configuration and returns True if and only if the red car is free in this configuration.
Clearly, this predicate is useful to look for a solved configuration in the current frontier.
isValid
Given that my implementation of movePiece can produce invalid configurations, ones where pieces
overlap, it is useful to have a predicate that checks whether a given configuration is valid, whether no
two pieces overlap in this configuration. isValid is this predicate. I also use it to check for invalid
moves between two valid configurations. Assume that I can reach a valid configuration B from some
valid configuration A by moving some piece by 4 positions, but the move is not valid because this move
would make the moved piece “jump” over some piece that is in the way. When that’s the case, then
moving the piece by only 1, 2 or 3 positions must produce an invalid configuration. Thus, I simply
produce moves using movePiece for increasing offsets, and I stop as soon as I encounter the first offset
that produces an invalid configuration.
There are many ways you can check whether a given board position is valid. The natural but sadly
somewhat slow way to do this is to check all pairs of pieces and test whether they overlap. The
implementation I chose is to construct a Set of occupied board positions (the set type implemented in
Data.Set). I can then ask for the size of this set. If this size equals the total size of all pieces, then no
two pieces overlap. If it is less, then two pieces overlap and the configuration is invalid.
3

Place your order
(550 words)

Approximate price: $22

Calculate the price of your order

550 words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total price:
$26
The price is based on these factors:
Academic level
Number of pages
Urgency
Basic features
  • Free title page and bibliography
  • Unlimited revisions
  • Plagiarism-free guarantee
  • Money-back guarantee
  • 24/7 support
On-demand options
  • Writer’s samples
  • Part-by-part delivery
  • Overnight delivery
  • Copies of used sources
  • Expert Proofreading
Paper format
  • 275 words per page
  • 12 pt Arial/Times New Roman
  • Double line spacing
  • Any citation style (APA, MLA, Chicago/Turabian, Harvard)

Our guarantees

Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.

Money-back guarantee

You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.

Read more

Zero-plagiarism guarantee

Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.

Read more

Free-revision policy

Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.

Read more

Privacy policy

Your email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.

Read more

Fair-cooperation guarantee

By sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.

Read more
error: Content is protected !!
Open chat
1
You can contact our live agent via WhatsApp! Via + 1 (929) 473-0077

Feel free to ask questions, clarifications, or discounts available when placing an order.

Order your essay today and save 20% with the discount code SCORE