Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You want to transform each element in the input list into a sequence of as many equal <code>Bool</code>s as the element says, and you want the <code>Bool</code> be <code>True</code> if the index of the number in the input list is even, and <code>False</code> if the index is odd.</p> <p>For that, you don't need the index, and it is better avoided - that gives simpler and usually more efficient code. The point is that the value alternates, it has a periodic pattern. To construct such periodic patterns, the Prelude offers the useful</p> <pre><code>cycle :: [a] -&gt; [a] Prelude&gt; take 10 $ cycle [1,2,3] [1,2,3,1,2,3,1,2,3,1] Prelude&gt; take 10 $ cycle [True,False] [True,False,True,False,True,False,True,False,True,False] </code></pre> <p>Neat, that's exactly what we need.</p> <p>Now, we can pair up each element of the input list with the corresponding <code>Bool</code>:</p> <pre><code>[ 2, 2, 4, 3] [True,False,True,False,... </code></pre> <p>We could use <code>zip</code> to produce pairs, <code>[(2,True), (2,False), ...]</code> and then use a function that transforms the pair into the appropriate sequence of <code>Bool</code>s.</p> <p>But that pattern is so common that we have a special higher-order function for that, <code>zipWith</code>.</p> <p>So if the type of list elements is <code>Int</code>, we can write</p> <pre><code>row :: [Int] -&gt; [Bool] row xs = concat $ zipWith replicate xs (cycle [True,False]) </code></pre> <p>For the type <code>Integer</code>, we can't use <code>replicate</code>, but we can use <code>genericReplicate</code> from <code>Data.List</code>.</p>
 

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