Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Note that <code>([0]++)</code> is the same as <code>(0:)</code>, which will make it look tidier and save us a nanosecond or two. (I'm joking with the nanosecond thing - no human can tell when something's a nanosecond faster, but it is nicer this way anyway.)</p> <p>Let's first think about making the lists you need. We want</p> <pre><code>postponeLists [[1,2,3], [7,6,8], [10,20,30,40]] = [[1,2,3], [0,7,6,8], [0,0,10,20,30,40]] = [1,2,3] : ones that should have zero in front of them </code></pre> <p>That's enough information for a definition:</p> <pre><code>postponeLists [] = [] postponeLists (l:ls) = l : map (0:) (postponeLists ls) </code></pre> <p>Now you said </p> <pre><code>foldl (zipWith +) [] [[1,2,3],[0,7,6,8],[0,0,0,3,4]] </code></pre> <p>but you mean</p> <pre><code>foldl (zipWith (+)) [] [[1,2,3],[0,7,6,8],[0,0,0,3,4]] </code></pre> <p>but unfortunately, that gives you <code>[]</code> because <code>zipWith</code> stops as soon as any of the lists run out of elements. We need some way of zipping them that doesn't stop. </p> <p>Solution 1: find the longest one, make them all that <code>maxlength</code> using <code>take maxlength.(++ repeat 0)</code><br> Solution 2: write another zipWith function that doesn't stop.</p> <p>I prefer solution 2. Let's look at the <a href="http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/GHC-List.html#zipWith">definition of <code>zipWith</code></a></p> <pre><code>zipWith :: (a-&gt;b-&gt;c) -&gt; [a]-&gt;[b]-&gt;[c] zipWith f (a:as) (b:bs) = f a b : zipWith f as bs zipWith _ _ _ = [] -- here's the problem - it stops as soon as any list is empty </code></pre> <p>OK, let's not stop then:</p> <pre><code>zipWithMore :: (a -&gt; a -&gt; a) -&gt; [a] -&gt; [a] -&gt; [a] zipWithMore f (a:as) (b:bs) = f a b : zipWithMore f as bs zipWithMore f [] bs = bs -- if there's more in bs, use that zipWithMore f as [] = as -- if there's more in as, use that </code></pre> <p>Now you can replace <code>zipWith (+)</code> with <code>zipWithMore (+)</code>. I'll leave the punchline to you.</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.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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