Monday 15 April 2013

Haskell List Monad State Dependance -


I have to write a program in Haskell which will solve some ethical problem. I think I list the list in 75% I understand this because it is an unknowable option but ...

(My problems are filling the NXM board with ships and water. I have given the rows and columns in every part of the ship. Price et al. Its not important)

I want to protect the algorithm as soon as possible, the problem is that the possibility of inserting the vessel depends on what I have given / which I have inserted. And I do not know how to pass it cuz I can not make a new state from the board alone)

My algorithm is: 1. Start the first board 2. The first line is generated I'm trying to implement every possible combination (I can add sheep vertical so I should remember to insert other parts of the sheep in low lines) 3. Solve the problem for small boards (each After generating 2 rows, I check that everything is okay)

But I do not know how to overcome new states, as far as I am in the state of Maud Read area, it generates new state alone old state and I do not want to build when the new state work is impossible for me to value).

I am sorry for Haskell's hatred, but in the necessary languages, after a few years of programming, being compelled to fight those monocs who can write in other languages ​​to work around Immediately makes me crazy (other things well in Haskelley are fine for me and some of them are really great).

Add a monad to get your desired behavior with the StateT list.

The list is a simple example of using monad's non determinism, while still maintaining the history of the previous choice:

  import control. Monad import control Monad Trance Class Import Control Silence Trance Fill out the space: stat [int] [] [int] fill = history & lt; - If received (length history == 3) then return history to the other side & lt; - Enter the elevator [0, 1, 2] guard (option 'Neel Alem' history) (Fill: History) Fill   

Fill for each path a different Keeps the history which he tries if he fills the board, he returns successfully, but if the current option overlaps with the previous choice, then it leaves that solution and tries to do a different path.

You supply it to evalStateT initial empty history:

  gt; & Gt; EvalStateT [[2,1,0], [1,2,0], [2,0,1], [0,2,1], [1,0,2], [0,1, 2]]   

This gives a list of all possible solutions in this case, which lists all permutations, in which we could fill the board.

No comments:

Post a Comment