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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:14:10+00:00 2026-05-17T19:14:10+00:00

I am trying to write some code that will loop through a list and

  • 0

I am trying to write some code that will loop through a list and add like terms. I’m trying to cons the cdr of the input list to a null list and then just compare the car of the list to the car of the new list and traverse down the list but my code just isn’t working. What am I doing wrong here?

  (define loop-add
  (lambda (l temp outputList)
    (if (or (equal? (cdr l) '()) (equal? (pair? (car l)) #f))
        outputList
        (if (equal? l '())
            outputList
            (let ((temp (cdr l)))
              (if (equal? temp '())
                  (loop-add (cdr l) outputList)
                  (if (equal? (cdr (car l)) (cdr (car temp)))
                      (loop-add l (cdr temp) (cons (append (cdr (car l)) (cdr (car (cdr l)))) outputList))
                      (loop-add l temp outputList))))))))

but the problem now is at the end line its just going to be an infinite loop. I need a way to recur with the input list but with temp being the cdr of the previous temp list.

  • 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-17T19:14:10+00:00Added an answer on May 17, 2026 at 7:14 pm

    Start by writing a procedure that can transform your input list into a new list of the unique terms in the original list, so

    (get-unique-terms '((2 1) (3 4) (5 3) (2 4)))
    (1 4 3) ; or something like that

    Call this new list TERMS. Now for each element in TERMS you can search the original list for matching elements, and get a sum of the coefficients:

    (define (collect-like-terms l)
      (let ((terms (get-unique-terms l)))
        ;; For each element of TERMS,
        ;;   Find all elements of L which have a matching term,
        ;;      Sum the coefficients of those elements,
        ;;      Make a record of the sum and the term a la L.
        ;; Collect the results into a list and return.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write some LINQ To SQL code that would generate SQL like
I'm trying to write some code that will print out the dimensions of all
I'm trying to write some simple C# code that will select some data from
I am trying to write a code that will grab some information provided by
I was trying to write some code that would check if an item has
So, im trying to write some code that utilizes Nvidia's CUDA architecture. I noticed
I'm trying to write some C# code that calls a method from an unmanaged
I have a mobile platform that I am trying to write some communications code
I'm trying to write some SQL that will delete files of type '.7z' that
I'm trying to write code that will disable submit button (or all submit buttons)

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.