Haskell Functions and Mapping

This is homework, and I'm really struggling with the last bit. We are writing Armadillo in Haskell, and apart from a few general lectures on functional languages, we don’t have many. No tutorial, etc. I was referred to LYAH several times, and there were good things there, but it does not seem to have suggestions for this particular problem.

In context, I have put all my Battleship code so you can see which definitions I will mention later.

--A data Row = A | B | C | D | E | F | G | H | I | J deriving (Enum, Ord, Show, Bounded, Eq, Read) data Column = One | Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten deriving (Enum, Ord, Show, Bounded, Eq, Read) --B data Address = Address Row Column deriving (Show, Read, Eq) --C data Cell = Cell Address Bool deriving (Show, Read, Eq) --D data Ship = Ship [Cell] deriving (Show, Eq) --E data Ships = Ships [Ship] deriving (Show, Eq) --F toAddress rc = Address (toEnum r) (toEnum c) --G toRowColumn (Address aRow aColumn) = (fromEnum aRow, fromEnum aColumn) --H allAddressesA = [ Address row column | row <- [A .. J], column <- [One .. Ten]] --I allAddressesB = [toAddress row column | row <- [0 .. 9], column <- [0 .. 9]] --J targetShip (Ship ship) (Address row column) | Address == map (\list -> (head Address Bool)) s --K targetShips [(Ships)] toAddress //TODO --L isSunk Ship ship |ship [] = true |otherwise foldl --M areSunk [(Ships)] //TODO 

And now I will also publish descriptions of specific appointments so that there are no communication errors with what I need help with:

(j) Write a function targetShip that takes the ship and target address as two of its parameters. If the ship has a cell with a destination address, this cell is marked sunk. An updated version of the ship is back. If the ship is empty, an empty ship is returned. (1 map, 116 characters)

(k) Write a function targetShips that takes a list of ships and the target address as two parameters. He tries to submerge each ship in the list of ships. It returns a list of updated ships. If there are no ships, an empty list is returned. (1 map, 86 characters)

(l) Write an isSunk function that takes the ship as its parameter. If the ship is an empty ship, return it. Otherwise, return true if all cells are marked sunk. (1 time, 78 characters)

(m) Write an isSunk function that takes a list of ships as its parameter. If there are no ships, return the truth. Otherwise, return true if all ships are marked sunk. (1 time, 78 characters)

So, what is my specific struggle here, these are the last 4. The solutions (A - I) that I tested and they work fine.

We need to know how to refer to cell values ​​... Address and Bool separately for starters.

We cannot write more functions.

I suggest that you need more explanation on specific issues, so I will follow closely. Any tips on where to start the last four problems will be greatly appreciated. Thanks in advance ... and now I will return to the text editor and try to do it.

+4
source share
2 answers

(j) Write a function targetShip that takes Ship and the destination address as two parameters. If the ship has a cell with a destination address, this cell is marked. The updated version of the ship is returned. If the ship is empty, an empty ship is returned. (1 map, 116 characters)

targetShip function,
Ship and a target address as its two parameters
produce Ship is returned

This data is enough to write a type annotation for your function.

 targetShip :: Ship -> Address -> Ship targetShip (Ship ship) (Address row column) 

If the ship is empty, the empty Ship is returned
And the ship is a list or list - either an empty list [], or a head with a tail, and then

 targetShip :: Ship -> Address -> Ship targetShip (Ship []) (Address row column) = Ship [] targetShip (Ship (x:xs)) (Address row column) = 

Now the hard part.
If the ship has a cell with the target address, that cell is marked sunk
A ship is a list of cells, and then I'm sure that for each cell (specific ship) I need to check whether the address of the current cell matches the one indicated by the function argument.
Well, let's do it for head x , which appeared in our pattern comparison.

 targetShip :: Ship -> Address -> Ship targetShip (Ship []) (Address row column) = Ship [] targetShip (Ship (x:xs)) (Address row column) = if x ... 

OMG, I can’t do this, since no helper function allows, I need more information. I need to create an x ​​structure according to my pattern, e.g.

 targetShip (Ship ((Cell address bool):xs)) (Address row column) = 

OMG again, I can no longer do, I repeat the same thing well, being more expressive in my comparison with the sample.

 targetShip (Ship ((Cell (Address shipRow shipColumn) bool):xs)) (Address row column) = 

Now everything is all right, I can do it.
And I will let you finish it.

Good luck.

+3
source

J: You must return the ship with a new list, which depends on the address.

 targetShip (Ship oldCells) address = Ship newCells where newCells = ... f cell = 

How do you define new cells? The task involves using a map. That way you can map the function f over oldCells . What to do f ? If he receives a cell that has the same address as the targetShip argument, it should return a new cell with the same address, but with a sunken status. Otherwise, it should just return the cell specified as an argument.

K: It should be very easy once you get your destination. Follow the recommendations for using map .

L and J: very similar. Tasks involve the use of folds. Have you tried the crease before? There are two main types: foldr and foldl , they both work by taking a function that takes two arguments, the battery and the current item, and folding the list into one battery item at a time using this battery. Let me show you some examples of foldl in action .

 foldl (\acc x -> acc + x) 0 [1,2,3] -> 6 -- starting accumulator of 0 foldl (+) 0 [1,2,3] -> 6 -- same thing as above foldl (&&) True [True, True, True] -> True -- folding booleans foldl (&&) True [True, False, True] -> False -- more boolean folding foldl (&&) True [] -> True -- folding an empty list yields the initial accumulator foldl (\acc x -> acc && (x > 5)) True [6,7,8] -> True -- complex folding function. 

With the last function, I hope you can see how you can solve the problems of L and J. Otherwise, look at pattern matching in anonymous functions and look at bending in LYAH.

+1
source

Source: https://habr.com/ru/post/1479061/


All Articles