title: Take elements on even positions from the list
body: <p>Problem: using <em>fold</em>, take from the list elements which are on the even positions:</p>
  
  <pre><code>GHCi&gt; evenOnly [1..10]
   [2,4,6,8,10]
  GHCi&gt; evenOnly ['a'..'z']
   "bdfhjlnprtvxz"
  
  evenOnly :: [a] -&gt; [a]
  evenOnly = undefined
  </code></pre>
  
  <p>I decided at first to get a list of alternating <code>0</code>-es and <code>1</code>-s: <code>[0,1,0,1..]</code></p>
  
  <pre><code>Prelude&gt; let g = iterate (\x -&gt; (x + 1) `mod` 2) 0
  Prelude&gt; take 10 $ g
  [0,1,0,1,0,1,0,1,0,1]
  </code></pre>
  
  <p>Then <em>zip</em> it with the original list, getting a list of pairs: <code>[(x1, 0), (x2,1), (x3,0) .. (xn, ?)]</code>:</p>
  
  <pre><code>Prelude&gt; zip g [1,2,3,4,5]
  [(0,1),(1,2),(0,3),(1,4),(0,5)]
  </code></pre>
  
  <p>After that, <code>foldr</code> the list of pairs with a filtering function and an empty list as initialization value.</p>
  
  <p>So I thought that this would work:</p>
  
  <pre><code>evenOnly :: [a] -&gt; [a]
  evenOnly xs = let g = iterate (\x -&gt; (x + 1) `mod` 2) 0
    in
    foldr (\ (x, n) s -&gt; if n == 1 then x : s else s) [] . (zip g xs)
  </code></pre>
  
  <p>But it gives an error, that I don't understand:</p>
  
  <pre><code>foldr.hs:44:59: error:
      • Couldn't match expected type ‘a0 -&gt; t0 (a1, Integer)’
                    with actual type ‘[(Integer, a)]’
      • Possible cause: ‘zip’ is applied to too many arguments
        In the second argument of ‘(.)’, namely ‘(zip g xs)’
        In the expression:
          foldr (\ (x, n) s -&gt; if n == 1 then x : s else s) [] . (zip g xs)
        In the expression:
          let g = iterate (\ x -&gt; (x + 1) `mod` 2) 0
          in
            foldr (\ (x, n) s -&gt; if n == 1 then x : s else s) [] . (zip g xs)
      • Relevant bindings include
          xs :: [a] (bound at foldr.hs:42:10)
          evenOnly :: [a] -&gt; [a] (bound at foldr.hs:42:1)
  </code></pre>
  
  <p>I think my idea is correct, I just did something wrong with the syntax.</p>