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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:11:17+00:00 2026-06-17T07:11:17+00:00

(cons 2 (cons ( cons 2 3 ) (cons 4 5 ))) This gives

  • 0
(cons 2 (cons ( cons 2 3 ) (cons 4 5 )))

This gives me a list that looks like this : (2 (2 . 3) 4 . 5) when I try to count the number of elements in this list the output is 3 as exepected.

How do I calculate the number of individual elements of a pair ? The output in this case should be 5 for example.

  • 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-17T07:11:18+00:00Added an answer on June 17, 2026 at 7:11 am

    Here’s a possible solution, the question is essentially asking for the number of atoms in a list structure (not necessarily null-terminated proper lists):

    (define (count-all seq)
      (cond ((null? seq) 0)
            ((not (pair? seq)) 1)
            (else (+ (count-all (car seq))
                     (count-all (cdr seq))))))
    

    It works on sequences of elements like this:

    • If the sequence is empty, it has zero elements
    • If the sequence is not a cons cell (a pair of elements), it’s because it’s a single element – an atom
    • Otherwise add the elements of both the car and the cdr of the sequence

    It works as expected for arbitrarily nested list structures:

    (count-all '(2 (2 . 3) 4 . 5))
    => 5
    
    (count-all '(1 (2 . (3 (4 . 5) 6)) 7 . 8))
    => 8
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a piece of C code that looks like this: const char (*foo)[2]
I have this association-list in Common Lisp: (defvar base-list (list (cons 'a 0) (cons
This is suppose to be a word guesser that gives 5 chances to to
Running this works as expected: (defn long-seq [n] (lazy-seq (cons (list n {:somekey (*
I need to count the number of times that particular pairs of integers occur
This is more a question of pros/cons between PHP and JAVA. Iv been doing
This is about the design decision and understand the procs and cons for adopting
I found it in a statement like this: delete /*+ restrict_all_ref_cons */ from table_1
In SICP exercise 2.26, this Scheme code is given: (define x (list 1 2
I cannot figure out why this segment gives unresolved overloaded function error (gcc version

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.