Sunday 15 February 2015

haskell - Find all paths between point on board -


About this topic: I came to know how the paths are created between the two points, and this is the code:

  go mn (i, j) = [(I + 1, j). I & lt; M] ++ [(i-1, j) | I & gt; 1] ++ [(I, J + 1) | Jammu & Lieutenant; N] ++ [(I, J-1) | J & gt; 1] - isEndOfPath p q = (p == q) ginapath p q acc mn input buf = gp cc abf where gp que ac buff | P == q = [[ACC]] - Refund ACC, buf gp q ACC buff = [S | R & lt; - Me mn q, no elm r buff, no elm r ac, no el r input, s and lt; - GenePath PR (R: AC) MN input (R: BF)] ++ [S | R & lt; - MNC go, no elem R ACC, R == P, S & LT; - Genpath PR (R: AC) MN Input (R: BF)] For example, we can find the path on the 2x2 board (2,2) (1,1) Thus, we can call it  
  genpath (2,2) (1,1) [(1,1)] 2 2 [(3,3), (1,1)] Can call in the form. [(1,1)]   

And we have the result

  [[(2,2), (2,1), (1, 1)], [(2,2), (2,1), (1,1)], [(2,2), (1,2), (1,1)], [(2, 2) , (1,2), (1,1)]]   

So we have the right path.

And now I will find all the paths between points of the joints. In Prolol it was very easy and I do not have any problems with it, I show you my algorithm and code:

First Forecast - When we search all the pathways, we return it: / P>

  genAllPaths ([], A, A, _, _, _, _). Otherwise, we will have to generate the path again, therefore, the FIR we search for the path between the first couple, and then we can search for other routes:  
  Servapath ([(I1, J1), (I2, J2) | T], ACC, S, M, N, Input, Buffer): - Genepath ((I1, J1) , (I2, j2)], x, m, n, input, [(i2, j2) | buffer], newfuure) | ACC], S, M, N, Input, Newbieffer).   

If you do not understand anything, please ask me.

So, now I'll do it in Hackel. I tried, but again, I have many problems with it. If you know how to do it and want to help me - I am so grateful.

  go mn (i, j) = [(I + 1, j). I & lt; M] ++ [(i-1, j) | I & gt; 1] ++ [(I, J + 1) | Jammu & Lieutenant; N] ++ [(I, J-1) | J & gt; 1] Genpath PQ ACC MN input buff = GP Q ACC buff - return all solutions where GPQ ACC buff | P == q = [(ACC, BF)] - Return ACC, buf gp q ACC buff = [S | R & lt; - Me mn q, no elm r buff, no elem rac, no el r input, s and lt; - GPR (R: AC) (R: BF)] ++ [S | R & lt; - MNC go, no elem R ACC, R == P, S & LT; - GPR (R: AC) (R: BF)]   

Your new code:

  All the terms of the gene ([], A, A, _ (I1, j1), (i2, j2) | T], ac, s, m, n, input, buffer): - genpath ((i, J1), (I2, J2), [(I2, J2)], X, M, N, Input, [(I2, J2) | Buffer], Newfuar), Gene All Path (T , [X | acc], s, m, n, input, newbuffer).   

Has direct text translation in Haskell:

  All path points acc MN input boof = g points acc buff where g [] acc _ = [acc ] G (P: Q: T) ACC buff = let SLS = GenePath Peak [Q] MN input (Q: buff) - = & gt; [(X, Newbief]] In concert [GT] (X: ACC) NewBuff | (X, NewBuff) & lt; -sols]   

Another way to write it

  Gene all path points acc MN input boof = g digit acc buf where is [] edi _ = [ACC] G (P: Q: T) ACC buff = genpath peak [Q] MN input (Q: buff)> gt; = (\ (X, newbuf) - & gt; gt (x: acc) newbuf)   

This list will be to bind operator & gt; & Gt; = is also used to do notation,

  gene all path digits acc mn input boof = g digit acc buff where g [] acc _ = Return ac - [ACC] as writing, monad g for list (p: q: t) acc buf = do (x, newbuf) & lt; - genPath pq [q] mn input (q: buf) gt (x: ac) newbuf   

which expresses the same calculation without the explicit use of bind The list represents a non-deterministic computation by representing the potential options as all as a list, using unexpected lists will be used with unexpected orders; Normal Haskell inspires lists, but Prologue does this with its left-to-right top-to-bottom strategy.

Since Haskell is lazy, after producing a solution, it is equivalent to searching back and forth, and of take 1 to cut Can be used to simulate.

No comments:

Post a Comment