title: Scheme: "mcar: contract violation, expected: mpair, given: #<procedure:..."
body: <p>I am trying to use streams to generate a list of prime numbers in Scheme and I am encountering an error that I can't seem to wrap my head around.  I have been spending hours trying different things and feel like I somewhat understand the problem, but I can't quite wrap my head around it.  I have looked at similar posts, but I don't think I'm experienced enough to be able to relate them to my problem.</p>
  
  <pre><code>(define int-builder$
      (lambda (n)
        (cons  n (lambda() (int-builder$ (+ n 1))))))
  
  (define filter-out-mults$
      (lambda (num lst)
            (if (= (modulo (car lst) num) 0)
                (filter-out-mults$ num (cdr lst))
                (cons (car lst) (lambda () (filter-out-mults$ num (cdr lst)))))))
  (define take$
      (lambda (m s)
        (if (or (= m 0) (null? s))
            '()
            (cons (car s) (take$ (- m 1) ((cdr s)))))))
  </code></pre>
  
  <p>int-builder$ generates an infinite stream of integers starting at n.  filter-out-mults$ should filter out any non-prime numbers.  take$ takes the first m primes from the list.</p>
  
  <p>Here is how I am running it, along with the error message:</p>
  
  <pre><code>&gt; (take$ 10 (filter-out-mults$ 2 (int-builder$ 2)))
  
  
  mcar: contract violation
  expected: mpair?
  given: #&lt;procedure:.../prime-stream.ss:10:8&gt;
  </code></pre>
  
  <p>I believe the problem has to do with how filter-out-mults$ is called in the if/else statement, in that a new int/procedure pair is not passed in, but I can't seem to figure out how to fix it.  Any help would be greatly appreciated.</p>