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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:18:20+00:00 2026-06-12T18:18:20+00:00

Please, help me with one simple exercise on the Scheme. Write function, that return

  • 0

Please, help me with one simple exercise on the Scheme.

Write function, that return count of atoms on the each level in the
list. For example:

(a (b (c (d e (f) k 1 5) e))) –> ((1 1) (2 1) (3 2) (4 5) (5 1))

My Solution:

(define (atom? x)
  (and (not (pair? x)) (not (null? x))))
(define (count L)
  (cond ((null? L) 0)
        ((pair? (car L))
         (count (cdr L)))
        (else
         (+ 1 (count (cdr L))))))
(define (fun L level)
  (cons 
   (list level (count L))
   (ololo L level)))
(define (ololo L level)
  (if (null? L)
      '()
      (if (atom? (car L))
          (ololo (cdr L) level)            
          (fun (car L) (+ level 1)))))
(fun '(a (b (c (d e (f) k 1 5) e))) 1)

It’s work fine, but give not correctly answer for this list:

(a (b (c (d e (f) (k) 1 5) e)))

is:

((1 1) (2 1) (3 2) (4 4) (5 1))

But we assume that ‘f’ and ‘k’ on the one level, and answer must be:

((1 1) (2 1) (3 2) (4 4) (5 2))

How should I edit the code to make it work right?


UPD (29.10.12):
My final solution:

(define A '(a (b (c (d e (f) k 1 5) e))))

(define (atom? x)
  (and (not (pair? x)) (not (null? x))))

(define (unite L res)
  (if (null? L) (reverse res)
      (unite (cdr L) (cons (car L) res))))

(define (count-atoms L answ)
  (cond ((null? L) answ)
        ((pair? (car L))
         (count-atoms (cdr L) answ))
        (else
         (count-atoms (cdr L) (+ answ 1)))))

(define (del-atoms L answ)   
  (cond ((null? L) answ)
        ((list? (car L))
         (begin
         (del-atoms (cdr L) (unite (car L) answ))))
        (else
         (del-atoms (cdr L) answ))))

(define (count L)
(define (countme L level answ)
  (if (null? L)  (reverse answ)
      (countme (del-atoms L '()) (+ level 1) (cons (cons level (cons (count-atoms L 0) '())) answ))))
  (countme L 1 '()))

(count A)

What can you say about this?

  • 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-12T18:18:21+00:00Added an answer on June 12, 2026 at 6:18 pm

    Do you know what you get if you run this?

    (fun '(a (b (c (d e (f) k 1 5) e)) (a (b (c)))) 1)
    

    You get this:

    ((1 1) (2 1) (3 2) (4 5) (5 1))
    

    The whole extra nested structure that I added on the right has been ignored. Here is why…

    Each recursion of your function does two things:

    1. Count all the atoms at the current “level”
    2. Move down the level till you find an s-expression that is a pair (well, not an atom)

    Once it finds a nested pair, it calls itself on that. And so on

    What happens in oLoLo when fun returns from the first nested pair? Why, it returns! It does not keep going down the list to find another.

    Your function will never find more than the first list at any level. And if it did, what would you to do add the count from the first list at that level to the second? You need to think carefully about how you recur completely through a list containing multiple nested lists and about how you could preserve information at each level. There’s more than one way to do it, but you haven’t hit on any of them yet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a simple one! yet, I am missing something. Please help me out.
Please help. I'm losing sleep over this one! :) I have a list of
Suppose i have one simple function in my program. Whenever i call that function
I know this is a simple one for some of you, so please help
guys please help me to create one simple search engine it should be like
Some one help me on this please. I am working on a simple app
Can any one please help me how to go to another activity in android
Can any one please help me solve this. I am resizing some flash object/embed
Hey friends any one please help me for this issue.in this javascript code i
Any one please help me to build a horizontal bar graph in my android

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.