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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:31:34+00:00 2026-05-11T07:31:34+00:00

I was going through this tutorial for fun, and got stuck on the very

  • 0

I was going through this tutorial for fun, and got stuck on the very last thing he says, ‘Exercise: Give a linearly recursive implementation of union and difference.’ (for a list)

Union, no sweat.

Difference, sweat.

An attempt looks like this. . .

(defun list-diff (L1 L2)   (cond     ((null L1) L2)      ((null (member (first L1) L2)) (cons (first L1) (list-diff (rest L1) L2)))     (t (list-diff (rest L1) L2))   ) ) 

Now, that returns all the elements that are in L1 that aren’t in L2, but it just returns all of L2 (obviously). Similarly, if I change the L2 in line 3 to ‘nil’, then it just returns all of L1 that aren’t in L2, but none of L2.

My attempts at work-arounds don’t look recursive, and when they are, I end up getting stack-overflows (like if I try calling (list-diff L2 L1) somewhere).

Any of his other exercises, such as list-intersection, only require running through the elements of L1. Here, I want to strike the crucial elements from L2, or run (list-diff L2 L1) and then union the results from both, but that’s not really linearly-recursive anymore.

Thoughts?

(not homework, really. I just thought I’d try to look at some LISP for fun.)

EDIT: a function that does this properly, based on the responses is:

(defun list-diff (L1 L2)   (cond     ((null L1) nil)     ((null (member (first L1) L2)) (cons (first L1) (list-diff (rest L1) L2)))     (t (list-diff (rest L1) L2))   ) ) 
  • 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. 2026-05-11T07:31:34+00:00Added an answer on May 11, 2026 at 7:31 am

    The set difference operation L1 \ L2 is defined as all elements e such that e is in L1 but e not in L2. So it looks to me like your second attempt is actually correct:

    Similarly, if I change the L2 in line 3 to ‘nil’, then it just returns all of L1 that aren’t in L2, but none of L2.

    It looks like you’re trying to compute the symmetric difference, although it isn’t clear to me that this is what the exercise requests.

    If you want to be clever about it, you can probably pass a third list into the function to serve as an accumulator. When L1 has elements, push the first element into the accumulator (and call recursively) when (null (member (first L1) L2)). When L1 is null, check the elements of L2 against the accumulator list, doing the same thing. When L1 and L2 are null, return the accumulator list.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer No, you will have to host it yourself (windows service… May 11, 2026 at 10:21 pm
  • Editorial Team
    Editorial Team added an answer When returning files from WCF, I would recommend checking out… May 11, 2026 at 10:21 pm
  • Editorial Team
    Editorial Team added an answer Absolutely you could do this using a DefiningQuery (which is… May 11, 2026 at 10:21 pm

Related Questions

I want to pass a comma delemited list of values as a parameter to
I am currently learning ASP.NET MVC so please excuse my question if it has
I'm interested in putting together my first online game using Flash as the client
I was going through some code and came across a scenario where my combobox

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.