I am trying to output the first 100 prime numbers and keep getting the error:
application: not a procedure;
expected a procedure that can be applied to arguments
given: (#)
arguments…: [none]
The error is shown in my take$ procedure here:
(if (or (= m 0) (null? st))
'()
(cons (car st) (take$ (- m 1) ((cdr st)))))))
Here it all my code:
(define int-builder$
(lambda (x)
(list x (lambda () (int-builder$ (+ 1 x ))))))
(define take$
(lambda (m st)
(if (or (= m 0) (null? st))
'()
(cons (car st) (take$ (- m 1) ((cdr st)))))))
(define filter-out-mults$
(lambda (num st)
(cond
(( = (remainder (car st) num) 0)
(filter-out-mults$ num ((cadr st))))
(else
(list (car st) (lambda () (filter-out-mults$ num ((cadr st)))))))))
(define sieve$
(lambda (st)
(list (car st)
(lambda() (sieve$ (filter-out-mults$ (car st) ((cadr st))))))))
(define stol$
(lambda (n)
(take$ n (sieve$ (int-builder$ 2)))))
Thanks for any help you can provide.
Your problem is that you have not been consistent in how you have been using your abstract Sieve.
Is a sieve defined like this:
or is it defined like this
In some places in your code, you are extracting the p and invoking it like this:
((cdr st)); in other places, like this:((cadr st))The reason that the commenters on your question were looking askance at each of those individually is that you have not given a high-level definition for what the rules are for forming Sieves and extracting subparts from Sieves. A data definition like the one above would help this.
For me, after I added data-definitions, contracts, and then started testing your functions individually, I quickly found the problem. (Hint: It has something to do with the inconsistency between
((cdr st))and((cadr st))noted above.)Here is my version of your code. It localizes the choice of Sieve representation by hiding it behind an abstract interface; I used a macro to do this since the stream constructor wants to delay evaluation of the expression it receives (though one could work around this by changing the interface so the Sieve constructor was required to take a sieve-producing procedure rather than a direct expression).
Exercise for reader: With the current api, and if someone follows the data definition I have given in this code,
stream-empty?can never return true; how could you prove this?