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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:03:06+00:00 2026-06-17T14:03:06+00:00

i have to get list difference between two integer list (both ordinate). i white

  • 0

i have to get list difference between two integer list (both ordinate).
i white this:

difference(L,[],L) :- !.
difference([],_,[]) :- !.
difference([],[],W). 
difference([H|T1],[D|T2],T3) :- difference(T1,[D|T2],[H|T3]).
difference([H|T1],[H|T2],T3) :- difference(T1,T2,T3).

but why i can’t get my list difference?
if i write this:

difference([],[],W):- write(X).

and this example:

| ?- difference([1,4,4],[1,4],R).    
[4|_27]

it makes right!
NB if i have duplicate number i have to show it!

  • 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-06-17T14:03:07+00:00Added an answer on June 17, 2026 at 2:03 pm

    I find your code rather odd. For instance, your third clause: what’s W for? Seems like you mean to say:

    difference([],[],_).
    

    Second problem: in the fourth clause, there’s nothing stopping H and D from being independent variables with the same binding. I suspect you mean something like this:

    difference([H|T1],[D|T2],T3) :- H \= D, difference(T1,[D|T2],[H|T3]).
    

    Fixing these things seems to fix the predicate to give a reasonable looking answer:

    | ?- difference([1,4,4], [1,4], R).
    R = [4]
    

    I think your first several clauses are trying to handle different sorts of base cases, is that right? E.g.:

    difference(L, [], L)   % handles the case where the second list is exhausted
    difference([], _, [])  % handles the case where the first list is exhausted
    difference([], [], W)  % handles the case where the lists are exhausted at the same time
    

    One problem with this is that L = [] is a legitimate binding, so the first and third clauses mean the same thing. You can probably safely remove the third one, because it would have matched and produced the same answer on the first. The second clause is more interesting, because it seems to say that regardless of whatever work we’ve done so far, if the first list is empty, the result is empty. I find that possibility a bit jarring–is it possible you actually want these two base cases? :

    difference([], L, L).
    difference(L, [], L).
    

    I remain unconvinced, but until I have a better idea what you’re trying to accomplish I may not be able to help more. For instance, what should happen with difference([1, 4], [1, 4, 4], R)? I posit you probably want R = [4], but your code will produce R = [].

    Also, I find it unlikely that

    difference([],[],W):- write(X).
    

    is going to be a helpful debugging strategy, because Prolog will generate a new variable binding for X because there’s nothing for it to refer to.

    The final version I have with all my changes looks like this:

    difference(L, [], L) :- !.
    difference([], L, L) :- !.
    difference([H|T1], [D|T2], T3) :- D \= H, difference(T1, [D|T2], [H|T3]).
    difference([H|T1], [H|T2], T3) :- difference(T1, T2, T3).
    

    Edit: does this implement your requirements?

    not_in1(X, Left, Right) :- member(X, Left), \+ member(X, Right).
    
    not_in(X, Left, Right) :- not_in1(X, Left, Right).
    not_in(X, Left, Right) :- not_in1(X, Right, Left).
    
    differences(Left, Right, Differences) :-
        findall(X, not_in(X, Left, Right), Differences).
    
    
    ?- differences([1,2,3,4], [1,3,5], X).
    X = [2,4,5]
    

    If so, I’ll try to get your original code to produce answers that match.

    Edit 2: OK, so the problem with the solution above is that it is O(N^2). In the worst case (two totally distinct lists) it will have to compare every item from list 1 to every item of list 2. It’s not exploiting the fact that both lists are ordered (I believe that’s what you mean by ‘ordinate’).

    The result looks a lot more like your original code, but your original code is not taking advantage of the fact that the items are ordered. This is why the fourth and fifth cases are confusing looking: you should recur down one of the lists or the other depending on which number is larger. The corrected code looks like this:

    differences([], Result, Result).
    differences(Result, [], Result).
    differences([H|Ls], [H|Rs], Result) :- differences(Ls, Rs, Result).
    differences([L|Ls], [R|Rs], [L|Result]) :-
        L < R,
        differences(Ls, [R|Rs], Result).
    differences([L|Ls], [R|Rs], [R|Result]) :-
        L > R,
        differences([L|Ls], Rs, Result).
    

    You can see this produces the same result as the O(N^2) method:

    ?- differences([1,2,3,4], [1,3,5], X).
    X = [2,4,5]
    

    You were right, you do need both base cases. This is so the remainder of either list becomes part of the result. Presumably these will be the largest values ([5] in the example).

    Now I have three inductive cases: one for <, one for > and one for =. The equality case is intuitive: recur on both lists, discarding the head of both lists. The next case basically says if the left head is less than the right head, add it to the result and recur on the left’s tail. The right is unchanged in that case. The other case is the mirror of this case.

    Hope this helps!

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

Sidebar

Related Questions

How can we get the difference between two git repositories? The scenario: We have
I have been trying to get it to work using this thread but can't
I have one table having ID and other attributes. How can I get list
I have requirement to get Facebook friends list with their images. How can I
How can I get friend list who have not authorized my application yet? I
Trying to get a list of all substrings currently I have a function but
So i have this ArrayList: list.get(0) == love list.get(1) == foo list.get(2) == make
I have an app from where I can get the list of all installed
I have just written a method for getting a List of differences between two
I have a NSMutableArray: NSMutableArray *temp = //get list from somewhere. Now there is

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.