Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>go m n (i,j) = [ (i+1,j) | i&lt;m ] ++ [ (i-1,j) | i&gt;1 ] ++ [ (i,j+1) | j&lt;n ] ++ [ (i,j-1) | j&gt;1 ] genPath p q acc m n input buf = g p q acc buf -- return all solutions where g p q acc buf | p==q = [(acc,buf)] -- return acc, buf g p q acc buf = [s | r &lt;- go m n q, notElem r buf, notElem r acc, notElem r input, s &lt;- g p r (r:acc) (r:buf)] ++ [s | r &lt;- go m n q, notElem r acc, r==p, s &lt;- g p r (r:acc) (r:buf)] </code></pre> <p>your new code:</p> <pre><code>genAllPaths([],A,A,_,_,_,_). genAllPaths([(I1,J1),(I2,J2)|T],Acc,S,M,N,Input,Bufor) :- genPath((I1,J1),(I2,J2),[(I2,J2)],X,M,N,Input,[(I2,J2)|Bufor],NewBufor), genAllPaths(T,[X|Acc],S,M,N,Input,NewBufor). </code></pre> <p>The direct textual translation into Haskell is:</p> <pre><code>genAllPaths points acc m n input buf = g points acc buf where g [] acc _ = [acc] g (p:q:t) acc buf = let sols = genPath p q [q] m n input (q:buf) -- =&gt; [(x,newbuf)] in concat [g t (x:acc) newbuf | (x,newbuf) &lt;- sols] </code></pre> <p>Another way to write it is</p> <pre><code>genAllPaths points acc m n input buf = g points acc buf where g [] acc _ = [acc] g (p:q:t) acc buf = genPath p q [q] m n input (q:buf) &gt;&gt;= (\ (x,newbuf) -&gt; g t (x:acc) newbuf ) </code></pre> <p>This uses the <em>bind</em> operator <code>&gt;&gt;=</code> from the list monad. There's also the <code>do</code> notation,</p> <pre><code>genAllPaths points acc m n input buf = g points acc buf where g [] acc _ = return acc -- same as writing `[acc]`, for list monad g (p:q:t) acc buf = do (x,newbuf) &lt;- genPath p q [q] m n input (q:buf) g t (x:acc) newbuf </code></pre> <p>which expresses the same computation without the explicit use of <em>bind</em>. List monad expresses non-deterministic computations by representing <em>all</em> possible choices as a list. True representation would use unordered lists with unpredictable order; normal Haskell lists induce order, but Prolog does the same with its left-to-right top-to-bottom strategy.</p> <p>Since Haskell is lazy, producing the resulting solutions one by one is equivalent to the backtracking search of Prolog, and <code>take 1</code> can be used to emulate <code>cut</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload