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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:45:19+00:00 2026-05-25T10:45:19+00:00

Hello, I’m new to Scheme. I’m reading tutorials on the web, and I wonder

  • 0

Hello, I’m new to Scheme.

I’m reading tutorials on the web, and I wonder how I could count occurrences of atoms in nested lists in Scheme. I found some tutorials mentioning bagify and flatten; I tried to mix them but I got an error. What could be wrong?

Here’s the code:

(define (bagify lst)
  (foldl (lambda (key ht)
         (hash-update ht key add1 0))
       #hash() lst))

(define (flatten mylist)
  (cond ((null? mylist) '())
        ((list? (car mylist)) (append (flatten (car mylist)) (flatten (cdr mylist))))
        (else (cons (car mylist) (flatten (cdr mylist))) (bagify(mylist)))))
  • 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-25T10:45:19+00:00Added an answer on May 25, 2026 at 10:45 am

    I think bagify and flatten are red herrings for this kind of problem. Sure, you could use flatten, and then count the occurrences in the resulting flattened list. However, it’s much more efficient and straightforward to just traverse the nested list.

    For simplicity, let’s start by implementing the function for the non-nested case. Here’s a version that counts occurrences of '?, for even more simplicity:

    (define count-?s
      (lambda (ls)
        (cond
          [(null? ls) 0]
          [(eq? (car ls) '?) (add1 (count-?s (cdr ls)))]
          [else (count-?s (cdr ls))])))
    

    Changing this to work on nested lists just requires adding one cond line. The implementation of flatten you found contains a hint here: we want to check at each step of the recursion whether the car of the list is itself a list (However, using list? is more power than we need; we can use pair? instead, as long as our input is always a proper, nested list).

    Once we know the car is also a (potentially-nested) list, we need to pass it to a function that knows how to handle lists. Fortunately, we’re in the middle of defining one!

    (define count-?s*
      (lambda (ls)
        (cond
          [(null? ls) 0]
          [(pair? (car ls)) (+ (count-?s* (car ls)) (count-?s* (cdr ls)))]
          [(eq? (car ls) '?) (add1 (count-?s* (cdr ls)))]
          [else (count-?s* (cdr ls))])))
    

    And that’s all there is to it. Very little thought involved, no? So little, in fact, that you can just replace a couple expressions and wind up with a function that does something completely different to the nested list:

    (define remove-?s*
      (lambda (ls)
        (cond
          [(null? ls) '()]
          [(pair? (car ls)) (cons (remove-?s* (car ls)) (remove-?s* (cdr ls)))]
          [(eq? (car ls) '?) (remove-?s* (cdr ls))]
          [else (cons (car ls) (remove-?s* (cdr ls)))])))
    

    Solving a problem for a nested list is really easy once you’ve solved it for a flat list.

    1. Start with the flat list solution.
    2. Check for a pair in the car.
    3. Do the natural recursion on both the car and the cdr.
    4. Combine the answers with a binary operator that makes sense with the left-hand side of the null? case, e.g., +/0, */1, cons/'(), and/#t, or/#f.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello there and Merry Christmas !!! I am new to WPF and I am
Hello everyone, I'm developing a photo sharing web site using the CodeIgniter PHP framework
Hello i have this code var queue = new BlockingCollection<int>(); queue.Add(0); var producers =
Hello I am new to the Objective C and I have a problem. I
Hello i would like do somthing like that: <?php $count = 0; foreach($a as
Hello and Happy New Year all! While thinking about new project I decided to
Hello friends a very novice question as I am very new to programming. I
Hello I have the following error by git-fsck, which cannot be cleaned by git-gc
Hello again ladies and gents! OK, following on from my other question on ASP.NET
Hello I was writing a Regular Expression (first time in my life I might

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.