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

The Archive Base Latest Questions

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

I need to write a program for equivalence classes and get this outputs… (equiv

  • 0

I need to write a program for equivalence classes and get this outputs…

(equiv '((a b) (a c) (d e) (e f) (c g) (g h))) 
 => ((a b c g h) (d e f))

(equiv '((a b) (c d) (e f) (f g) (a e)))
 => ((a b e f g) (c d))

Basically, A set is a list in which the order doesn’t matter, but elements don’t appear more than once.
The function should accept a list of pairs (elements which are related according to some equivalence relation), and return a set of equivalence classes without using iteration or assignment statements (e.g. do, set!, etc.).

However, set utilities such as set-intersection, set-union and a function which eliminates duplicates in a list and built-in functions union, intersection, and remove-duplicates are allowed.

Thanks a lot!

By the way, It’s not a homework question. A friend of mine need this piece of code to solve smilar questions.

  • 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-14T21:28:53+00:00Added an answer on May 14, 2026 at 9:28 pm

    That sounds like a typical homework question.

    It’s not that difficult, though.

    A simple recursive function over the input list will do. The ingredients of the function are already mentioned in the task description: simple set operations.

    If it is homework, then this applies: The typical strategy for homework questions is that YOU have to show first your solution attempt. That should be at least a mostly correct formulation of the algorithm or almost working code. Then Lispers may help you with the finishing touches…

    Well, time passes and no solution.

    So here is one using Common Lisp:

    We need three functions.

    The first function adds a single pair to the set of pairs. A pair is a list. The set of pairs is a list of pairs. For the pair we compute two sets: the set of pairs that are equivalent and the set of pairs that are not equivalent. We combine the pairs that are equivalent with our in input pair into a single set.

    (defun equiv-add (e l)
      (let ((l- (remove-if     (lambda (i) (intersection e i)) l))
            (l+ (remove-if-not (lambda (i) (intersection e i)) l)))
        (cons (remove-duplicates (reduce #'union (cons e l+)))
              l-)))
    

    The second function adds each pair of a set of pairs to the result. It adds them by calling EQUIV-ADD.

    (defun equiv-aux (list result)
      (if (null list)
          result
        (equiv-aux (rest list)
                   (equiv-add (first list)
                              result))))
    

    The third function just calls EQUIV-AUX with the input set and an empty result. Additionally it sorts the result sublists.

    (defun equiv (list)
      (mapcar (lambda (el)
                (sort el #'string-lessp))
              (equiv-aux list '())))
    

    Example calls:

    CL-USER 34 > (equiv '((a b) (c d) (e f) (f g) (a e)))
    ((A B E F G) (C D))
    
    CL-USER 35 > (equiv '((a b) (a c) (d e) (e f) (c g) (g h))) 
    ((A B C G H) (D E F))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 383k
  • Answers 383k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Under the EJB 3.1 standard, EJB methods can very easily… May 14, 2026 at 10:50 pm
  • Editorial Team
    Editorial Team added an answer Take a look at this tutorial: This tutorial demonstrates how… May 14, 2026 at 10:50 pm
  • Editorial Team
    Editorial Team added an answer Whenever you are always going to load an entity, just… May 14, 2026 at 10:50 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.