Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>change [] = 1 : repeat 0 change (d : ds) = c where (a, b) = splitAt d (change ds) c = a ++ zipWith (+) b c </code></pre> <p>Then,</p> <pre><code>result = (!! 100) $ xs where xs = change [1, 5, 10, 15, 20, 25, 50] = let -- g = (\(a,b)-&gt; fix ((a++) . zipWith (+) b)) g (a,b) = let c = a ++ zipWith (+) b c in c in g . splitAt 1 . change $ [5, 10, 15, 20, 25, 50] = g . splitAt 1 . g . splitAt 5 . change $ [10, 15, 20, 25, 50] = .... = let h n = g . splitAt n in h 1 . h 5 . h 10 . h 15 . h 20 . h 25 . h 50 . (1:) . repeat $ 0 </code></pre> <p>or, simpler,</p> <pre><code>Prelude&gt; (!! 100) $ foldr h (1:cycle [0]) [1, 5, 10, 15, 20, 25, 50] 1239 </code></pre> <p>(which is a correct answer, BTW). This is arguably easier to comprehend. Your question is thus localized to the <code>g</code> definition,</p> <pre><code> g (a,b) = let c = a ++ zipWith (+) b c in c </code></pre> <p>The thing about Haskell's definitions is that they are <em>recursive</em> (they are equivalent to Scheme's <code>letrec</code>, not <code>let</code>). </p> <p>Here it works, because when <code>c</code> is <em>lazily</em> consumed, it's definition says it's built from two parts, <code>a ++ ...</code> and so first <code>a</code> is consumed. And this works because <code>a</code> does not depend on <code>c</code>. Calculating <code>a</code> does not demand any knowledge of <code>c</code>.</p> <p>In <code>zipWith (+) b c</code>, <code>c</code> is essentially a <strong><em>pointer</em></strong> into the sequence being defined, <code>length a</code> notches <strong><em>back</em></strong> from the production point, <code>rest</code>, in this re-write:</p> <pre><code> g (a,b) = let c = a ++ rest rest = zipWith (+) b c in c </code></pre> <p>We have <code>h n xs = g (splitAt n xs)</code> and this is describing then the sum of the input list with the result, moved <code>n</code> notches forward:</p> <pre><code> x1 x2 x3 x4 x5 x6 x7 x8 ................ xs A y1 y2 y3 y4 y5 .......... ys B -------- y1 y2 y3 y4 y5 y6 y7.................... ys == A + B </code></pre> <p>This suggests <code>h</code> can be re-written with improved locality of access,</p> <pre><code>change ds n = foldr h (1:cycle [0]) ds !! n -- [1, 5, 10, 15, 20, 25, 50] 100 where h n xs = ys where ys = zipWith (+) xs (replicate n 0 ++ ys) -- = fix (zipWith (+) xs . (replicate n 0 ++)) </code></pre>
    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