Note that there are some explanatory texts on larger screens.

plurals
  1. POBug in while loop
    primarykey
    data
    text
    <p>I can't figure out what the bug is in the toy <code>while</code> loop I wrote below. It works for one input, but hangs for other inputs. Here is the code - the while loop takes a vector, a predicate on vector, and transformation function on vector, and returns another vector:</p> <pre><code>import Data.Vector.Unboxed as U while :: Vector Int -&gt; (Vector Int -&gt; Bool) -&gt; (Vector Int -&gt; Vector Int) -&gt; Vector Int while v p f = go 0 v where go n x = if (p x) then go (n+1) (f x) else x test :: Vector Int -&gt; Vector Int test a = while a (\x -&gt; (x!0) &lt; 2) (\x -&gt; U.map (+1) a) main = print $ test (U.fromList [0]) </code></pre> <p>This hangs on execution of <code>main</code>. On the other hand, if I change <code>test</code> to:</p> <pre><code>test a = while a (\x -&gt; (x!0) &lt; 1) (\x -&gt; U.map (+1) a) </code></pre> <p>I get a termination with result (<code>ghci</code> output below):</p> <pre><code>λ: main fromList [1] </code></pre> <p>I feel I must be missing something. I looked hard at the function, but can't figure out what I am doing wrong. It looks like predicate can't be executed more than twice.</p> <p>BTW, it works with other types like <code>Int</code>.</p> <pre><code> while :: Int -&gt; (Int -&gt; Bool) -&gt; (Int -&gt; Int) -&gt; Int while i p f = go 0 i where go n x = if (p x) then go (n+1) (f x) else x test :: Int -&gt; Int test a = while a (\x -&gt; x &lt; 2) (\x -&gt; x+1) main = print $ test 0 </code></pre> <p>ghci output:</p> <pre><code>λ: main 2 </code></pre> <p><code>GHC version: 7.6.1</code></p> <p><code>Vector version: 0.10.0.1</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.
 

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