title: Scheme fold map and filter functions
body: <p>I am learning how to use higher-order functions in scheme. I get the idea of using higher-order functions, however I am having trouble using them. For this learning exercise, I would prefer to only use some combination of filter, fold, and/or map.</p>
  
  <p>For example, I want to construct the set difference between two lists call them A and B. I am defining set difference as x such that x is an element of A but x is not an element of B. I only want to use the functions map, filter and fold. For example:</p>
  
  <p>Let A = (1 8 6 2)</p>
  
  <p>Let B = (5 7 9 1 6)</p>
  
  <p>The set difference of A and B would be (8 2)</p>
  
  <p>The idea is to construct a new list by iterating over the elements of A and seeing if an element of A equals an element of B, if so don't add a to the new list; otherwise add a to the new list.</p>
  
  <p>My algorithm idea goes something like this:</p>
  
  <p>Let neq be "not equal to"</p>
  
  <ol>
  <li><p>For each a in A and b in B evaluate the expression: <code>(neq? a b)</code></p>
  
  <p>For a = 1 we have:</p>
  
  <p>(and (neq? 1 5) (neq? 1 7) (neq? 1 9) (neq? 1 1) (neq ? 1 6))</p></li>
  <li><p>If this expression is true then a goes in the new list; otherwise don't add a to the new list. In our example <code>(neq? 1 1)</code> evaluates to false and so we do not add 1 to the new list.</p></li>
  </ol>
  
  <p>Pretty much my entire procedure relies on 1, and this is where I have a trouble. </p>
  
  <ol>
  <li>How do I do step 1?</li>
  <li>I see that in step 1 I need some combination of the <code>map</code> and <code>fold</code> functions, but how do I get the <code>and a neq b</code> distributed?</li>
  </ol>
  
  <p><strong>EDIT</strong> This is the closest sample I have:</p>
  
  <pre><code>(fold-right (trace-lambda buggy (a b c) (and (neq? a b))) #t A B)
  |(buggy 3 5 #t)
  |#t
  |(buggy 2 4 #t)
  |#t
  |(buggy 1 1 #t)
  |#f
  #f
  </code></pre>
  
  <p>The above shows a trace of my anonymous function attempting to perform the (and (neq? a b)) chain. However, it only performs this on elements in A and B at the same position/index. </p>
  
  <p>All help is greatly appreciated!</p>