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 3343464
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:57:27+00:00 2026-05-18T00:57:27+00:00

Im trying count the number of positive elements in a list. Here is what

  • 0

Im trying count the number of positive elements in a list. Here is what I have so far:

 (define howMany
   (lambda (list)
      (cond
         [(not (list? list)) 0]
         [(null? list) 0]
         [(> list 0) (+ 1 (howMany (cdr list)))])))

It keeps giving me an error, “expects type real number”, how would you fix this?

Oh im calling this like so:

(howMany '(6 7 8))
  • 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-05-18T00:57:28+00:00Added an answer on May 18, 2026 at 12:57 am

    There are a couple of bugs in your code.

    (> list 0) should be (> (car list) 0) as you want to check if the first element of the list is greater than 0. You cannot apply the default implementation of > to a list either.

    (+ 1 (howMany (cdr list))) will also fail as howMany does not always evaluate to a number. You have to maintain a counter by passing that as an argument to the recursively called procedure. One way to do this is:

    (define (howmany lst)
      (let loop ((n 0) (lst lst))
        (if (null? lst) n
          (loop (if (> (car lst) 0) (add1 n) n) (cdr lst)))))
    

    Test:

    > (howmany '(1 2 3 4 5))
    5
    > (howmany '(1 2 3 -4 5))
    4
    > (howmany '(1 -2 3 -4 5))
    3
    > (howmany '(-1 -2 3 -4 5))
    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 count the number of records that have a null DateTime value.
I am trying to count the number of rows whose date has not yet
I have two tables: subject and student. I'm trying to count the number of
I have TableView. My datasource is SQLite database. I'm trying to count a number
I'm trying to count the number of rows in this query, however it's not
I'm trying to count the number of 0s in a string of numbers. Not
I'm trying to log page visits and count number of visitors. I have two
I'm trying to count the number of rows that have distinct values in both
Hey I have been trying to count the number of words in my text
I am trying to count the number of elements in an array using C.

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.