Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your first definition, <code>kart xs ys = [(x,y) | x &lt;- xs, y &lt;- ys]</code>, is equivalent to </p> <pre><code>kart xs ys = xs &gt;&gt;= (\x -&gt; ys &gt;&gt;= (\y -&gt; [(x,y)])) </code></pre> <p>where</p> <pre><code>(x:xs) &gt;&gt;= g = g x ++ (xs &gt;&gt;= g) (x:xs) ++ ys = x : (xs ++ ys) </code></pre> <p>are sequential operations. Redefine them as alternating operations,</p> <pre><code>(x:xs) &gt;&gt;/ g = g x +/ (xs &gt;&gt;/ g) (x:xs) +/ ys = x : (ys +/ xs) [] +/ ys = ys </code></pre> <p>and your definition should be good to go for infinite lists as well:</p> <pre><code>kart_i xs ys = xs &gt;&gt;/ (\x -&gt; ys &gt;&gt;/ (\y -&gt; [(x,y)])) </code></pre> <p>testing,</p> <pre><code>Prelude&gt; take 20 $ kart_i [1..] [100..] [(1,100),(2,100),(1,101),(3,100),(1,102),(2,101),(1,103),(4,100),(1,104),(2,102) ,(1,105),(3,101),(1,106),(2,103),(1,107),(5,100),(1,108),(2,104),(1,109),(3,102)] </code></pre> <p>courtesy of <a href="http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&amp;tid=10663" rel="nofollow noreferrer">"The Reasoned Schemer"</a>. (see also <a href="https://stackoverflow.com/questions/10843563/conda-condi-conde-condu/10848902#10848902">conda, condi, conde, condu</a>).</p> <hr> <p>another way, more explicit, is to create separate sub-streams and combine them:</p> <pre><code>kart_i2 xs ys = foldr g [] [map (x,) ys | x &lt;- xs] where g a b = head a : head b : g (tail a) (tail b) </code></pre> <p>this actually produces exactly the same results. But now we have more control over how we combine the sub-streams. We can <a href="https://stackoverflow.com/a/15793597/849891">be more diagonal</a>:</p> <pre><code>kart_i3 xs ys = g [] [map (x,) ys | x &lt;- xs] where -- works both for finite g [] [] = [] -- and infinite lists g a b = concatMap (take 1) a ++ g (filter (not.null) (take 1 b ++ map (drop 1) a)) (drop 1 b) </code></pre> <p>so that now we get</p> <pre><code>Prelude&gt; take 20 $ kart_i3 [1..] [100..] [(1,100),(2,100),(1,101),(3,100),(2,101),(1,102),(4,100),(3,101),(2,102),(1,103) ,(5,100),(4,101),(3,102),(2,103),(1,104),(6,100),(5,101),(4,102),(3,103),(2,104)] </code></pre> <p>With some <a href="https://stackoverflow.com/search?q=%5Bhaskell%5D+pairs+diagonal">searching on SO</a> I've also found an <a href="https://stackoverflow.com/a/762806/849891">answer by Norman Ramsey</a> with seemingly yet another way to generate the sequence, splitting these sub-streams into four areas - top-left tip, top row, left column, and the rest. His <code>merge</code> there is the same as our <code>+/</code> here.</p> <hr> <p>Your second definition, </p> <pre><code>genFromPair (e1, e2) = [x*e1 + y*e2 | x &lt;- [0..], y &lt;- [0..]] </code></pre> <p>is equivalent to just</p> <pre><code>genFromPair (e1, e2) = [0*e1 + y*e2 | y &lt;- [0..]] </code></pre> <p>Because the list <code>[0..]</code> is infinite there's no chance for any other value of <code>x</code> to come into play. <em>This</em> is the problem that the above definitions all try to avoid.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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