Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9296323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:48:24+00:00 2026-06-18T21:48:24+00:00

I am trying to output the first 100 prime numbers and keep getting the

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T21:48:25+00:00Added an answer on June 18, 2026 at 9:48 pm

    Your problem is that you have not been consistent in how you have been using your abstract Sieve.

    Is a sieve defined like this:

    ;; A Sieve is a (cons n p), where
    ;;    n is a Natural Number
    ;;    p is a Procedure that takes no arguments and returns a Sieve
    

    or is it defined like this

    ;; A Sieve is a (list n p), where
    ;;    n is a Natural Number
    ;;    p is a Procedure that takes no arguments and returns a Sieve
    

    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?

    ;; A Stream is a (list Nat (-> () Stream))
    ;; but this knowledge should not be used anywhere but in the
    ;; procedures (and special form) stream-rest, stream-first, stream,
    ;; and stream-empty?.
    
    ;; stream-rest: Stream -> Stream
    (define (stream-rest st) ((cadr st)))
    
    ;; stream-first: Stream -> Nat
    (define (stream-first st) (car st))
    
    ;; Special Form: (stream <natural-number> <stream-expr>) is a Stream
    (define-syntax stream
      (syntax-rules ()
        ((stream n expr) (list n (lambda () expr)))))
    
    ;; Stream -> Boolean
    (define (stream-empty? st) (null? st))
    
    
    ;; Nat -> Stream
    (define (int-builder$ x)
      (stream x (int-builder$ (+ 1 x))))
    
    ;; Nat Stream -> [Listof Nat]
    (define (take$ m st)
      (if (or (= m 0) (stream-empty? st))
          '()
          (cons (stream-first st) (take$ (- m 1) (stream-rest st)))))
    
    ;; Nat Stream -> Stream
    (define (filter-out-mults$ num st)
      (cond
       (( = (remainder (stream-first st) num) 0)
        (filter-out-mults$ num (stream-rest st)))
       (else 
        (stream (stream-first st) (filter-out-mults$ num (stream-rest st))))))
    
    ;; Stream -> Stream
    (define (sieve$ st)
      (stream (stream-first st)
              (sieve$ (filter-out-mults$ (stream-first st) (stream-rest st)))))
    
    ;; Nat -> [Listof Nat]
    (define (stol$ n)
      (take$ n (sieve$ (int-builder$ 2))))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert a string into an int so that I can keep
I am trying to compare two text files and output the first string in
I am trying to write a method that removes the first letter of a
I'm trying to use CSV to calculate the average of three numbers and output
I am trying to output the value of x in the following request: http://localhost:4827/Default.aspx?x=123123
I am trying to output JSON directly in my Razor view. The object being
I am trying to output a lot of data (membership record) into a JTextArea,
I am trying to output the comments of a facebook page in PHP. For
I m trying to output introtext of article selected by their alias <?php $db
I am trying to output a few paragraphs of text in an Excel spreadsheet,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.