title: How would you define map and filter using foldr in Haskell?
body: <p>I'm doing a bit of self study on functional languages (currently using Haskell). I came across a Haskell based assignment which requires defining map and filter in terms of foldr. For the life of me I'm not fully understanding how to go about this.</p>
  
  <p>For example when I define a map function like:</p>
  
  <pre><code>map'            :: (a -&gt; b) -&gt; [a] -&gt; [b]
  map' f []       = []
  map' f (x:xs)   = foldr (\x xs -&gt; (f x):xs) [] xs
  </code></pre>
  
  <p>I don't know why the first element of the list is always ignored. Meaning that:</p>
  
  <pre><code>map' (*2) [1,2,3,4]
  </code></pre>
  
  <p>results in [4,6,8] instead of [2,4,6,8]</p>
  
  <p>Similarly, my filter' function:</p>
  
  <pre><code>filter'             :: (a -&gt; Bool) -&gt; [a] -&gt; [a]
  filter' p []        = []
  filter' p (x:xs)    = foldr (\x xs -&gt; if p x then x:xs else xs ) [] xs
  </code></pre>
  
  <p>when run as:</p>
  
  <pre><code>filter' even [2,3,4,5,6]
  </code></pre>
  
  <p>results in [4,6] instead of [2,4,6]</p>
  
  <p>Why would this be the case? And how SHOULD I have defined these functions to get the expected results? I'm assuming something is wrong with my lambda expressions...</p>