title: Using Scheme, how to keep a variable's value during recursive calls (nested lists)?
body: <p>I'm trying to remove the last occurrence of a value from a list, and my program is failing during the recursive call with nested lists.</p>
  
  <p>I have a function that counts the number of occurrences of the symbol:</p>
  
  <pre><code>(define (countOccurrences lst elem count)
    (cond
      [(empty? lst)            count]
      [(list? (car lst))       (countOccurrences (cdr lst) elem
                                           (countOccurrences (car lst) elem count))]
      [(equal? (car lst) elem) (countOccurrences (cdr lst) elem (add1 count))]
      [else                    (countOccurrences (cdr lst) elem count)]))
  </code></pre>
  
  <p>And the main body is here:</p>
  
  <pre><code>(define (lastLess lst elem)
    (let ((count (countOccurrences lst elem 0)))
      (if (&lt; 0 count)
          (lastLessHelper lst elem count)
          lst)))
  </code></pre>
  
  <p>Helper function:</p>
  
  <pre><code>(define (lastLessHelper lst elem count)
    (cond
      [(empty? lst)         empty]
      [(eq? count 0)        (cdr lst)]
      [(eq? (car lst) elem) (set! count (sub1 count))
                            (cond
                              [(eq? count 0) (cdr lst)]
                              [else          (cons (car lst)
                                                   (lastLessHelper (cdr lst) elem count))])]
      [(list? (car lst))    (cons (lastLessHelper (car lst) elem count)
                                  (lastLessHelper (cdr lst) elem count))]
      [else                 (cons (car lst) (lastLessHelper (cdr lst) elem count))]))
  </code></pre>
  
  <p>The problem is this line:</p>
  
  <pre><code>[(list? (car lst)) (cons (lastLessHelper (car lst) elem count) (lastLessHelper (cdr lst) elem count))]
  </code></pre>
  
  <p>I decrement the 'count' variable whenever the <code>(car lst)</code> is equal to <code>elem</code>, and during the first recursive call <code>(lastLessHelper (car lst) elem count)</code> it decrements correctly, but when that call returns and it recurses on the cdr lst: <code>(lastLessHelper (cdr lst) elem count))]</code> the value of 'count' returns to its original value.</p>
  
  <p>It works for a normal list input, such as <code>(lastLess '(1 2 3 2 4 5) '2))</code> I correctly get <code>(1 2 3 4 5)</code>, but when using nested lists as input, such as <code>(lastLess '(1 (2 3) 4 2 5) '2)</code> it returns <code>(1 (2 3) 4 2 5)</code>.  </p>
  
  <p>How to keep the value of 'count' during the recursive calls?  I should note, there is probably easier ways to do this, but this is a homework exercise and we are forbidden to use the 'reverse' call.</p>
  
  <p>EDIT:  Sylwester's comment helped me understand.  My problem wasn't counting the occurrences of an item, the problem was to remove the last occurrence of an item.  My thought was to first count the occurrences of that item, then traverse the list and decrement that count until it was 0, then I would know to remove that item, and then just <code>cons</code> the rest of the list. </p>