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

  • Home
  • SEARCH
  • 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 703897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:52:53+00:00 2026-05-14T03:52:53+00:00

Okay so I have started a new language in class. We are learning Scheme

  • 0

Okay so I have started a new language in class. We are learning Scheme and i’m not sure on how to do it. When I say learning a new language, I mean thrown a homework and told to figure it out. A couple of them have got me stumped.

My first problem is:

Write a Scheme function that returns true (the Boolean constant #t) ifthe parameter is a list containing n a’s followed by n b’s. and false otherwise.

Here’s what I have right now:

(define aequalb
  (lambda (list)
    (let ((head (car list)) (tail (cdr list)))
      (if (= 'a head)
          ((let ((count (count + 1)))
             (let ((newTail (aequalb tail))))
             #f
             (if (= 'b head)
                 ((let ((count (count - 1)))
                    (let ((newTail (aequalb tail))))
                    #f
                    (if (null? tail)
                        (if (= count 0)
                            #t
                            #f)))))))))))

I know this is completely wrong, but I’ve been trying so please take it easy on me. Any help would be much appreciated.

  • 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-14T03:52:54+00:00Added an answer on May 14, 2026 at 3:52 am

    A trick I picked up from Essentials of Programming Languages is to always write recursive list functions by handling the two important cases first: null (end of list) and not null.

    So, the basic structure of a list function looks something like this:

    (define some-list-function 
      (lambda (list)
        (if (null? list)
            #f
            (do-some-work-here (head list)
                               (some-list-function (tail list))))))
    

    Usually you (1) check for null (2) do some work on the head and (3) recur on the tail.

    The first thing you need to do is decide what the answer is for a null list. It’s either #t or #f, but which? Does a null list have the same number of a as b?

    Next, you need to do something about the recursive case. Your basic approach is pretty good (although your example code is wrong): keep a count that goes up when you see a and down when you see b. The problem is how to keep track of the count. Scheme doesn’t have loops* so you have to do everything with recursion. That means you’ll need to pass along an extra counter variable.

    (define some-list-function 
      (lambda (list counter)
        (if (null? list)
            ; skip null's code for a second
    

    Now you have to decide whether the head is 'a or not and increment count if so (decrement otherwise). But there’s no count variable, just passing between functions. So how to do it?

    Well, you have to update it in-line, like so:

    (some-list-function (tail list) (+ 1 count))
    

    By the way, don’t use = for anything but numbers. The newer, cooler Lisps allow it, but Scheme requires you to use eq? for symbols and = for numbers. So for your 'a vs 'b test, you’ll need

    (if (eq? 'a (head tail)) ...)
    ; not
    (if (= 'a (head tail)) ...)
    

    I hope this helps. I think I gave you all the pieces, although there are a few things I skipped over. You need to change the null case now to check count. If it’s not = 0 at the end, the answer is false.

    You should also maintain a separate flag variable to make sure that once you switched to 'b, you return #f if you see another 'a. That way a list like '(a a a b b a b b) won’t pass by mistake. Add the flag the same way I added counter above, by adding another function parameter and passing along the value at each recursive call.

    Finally, if your teacher really isn’t giving you any help, and won’t, then you might want to read a basic book on Scheme. I haven’t used any of these myself, but I’ve heard they’re good: The Scheme Programming Language, How to Design Programs or err umm I thought there was a third book online for free, but I can’t find it now. I guess if you have lots of extra time and want to blow your mind, you can read Structure and Interpretation of Computer Programs. It teaches a little Scheme and lot about programming languages.

    *It does have some of these things, but it’s better to ignore them for now.

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

Sidebar

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.