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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:01:43+00:00 2026-06-11T08:01:43+00:00

Trying to write a procedure that given a value and a list, it deletes

  • 0

Trying to write a procedure that given a value and a list, it deletes all the occurence of that value in the list a wrote:

delMember(X, [], []) :- !.
delMember(X, [X|Xs], Y) :- !, delMember(X, Xs, Y).
delMember(X, [T|Xs], Y) :- !, delMember(X, Xs, Y2), append([T], Y2, Y).

Since the cut this code cannot answer correctly queries like:

delMember(Y, [1,2,3,1,2,3,1,2,3], [1, 2, 1, 2, 1, 2 ]).

If I delete the cuts:

delMember(X, [], []).
delMember(X, [X|Xs], Y) :- delMember(X, Xs, Y).
delMember(X, [T|Xs], Y) :- delMember(X, Xs, Y2), append([T], Y2, Y).

it fail in queries like:

delMember(Y, [1,2,3,1,2,3,1,2,3], [1,2,3,1,2,3,1,2,3]).

(returns true , when the correct answer is false).

How can I make it works in both situations?

Maybe I can check that X is not T in the third line of code, I tried:

delMember(X, [T|Xs], Y) :- not(X = T), delMember(X, Xs, Y2), append([T], Y2, Y).

but it does not work.

  • 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-11T08:01:45+00:00Added an answer on June 11, 2026 at 8:01 am

    Use of the cut

    delMember(X, [], []) :- !.
    delMember(X, [X|Xs], Y) :- !, delMember(X, Xs, Y).
    delMember(X, [T|Xs], Y) :- !, delMember(X, Xs, Y2), append([T], Y2, Y).
    

    Here, you can see that you use !/0 in the last clause of your predicate. It’s not necessary. After the last clause, there’s no choice left (Prolog remembers choice points from left to right and top to bottom), so a cut (that removes choices), won’t do anything useful since you’re already at the bottom of your list of choices.

    To illustrate, see

    a :- b; c.
    a :- d.
    

    Here, to prove a, Prolog will first try b, then c, then d (left to right, then top to bottom).

    BTW, as a beginner in Prolog, you should go as far as totally avoid the use of the cut. It will just add to your misunderstandings as long as you don’t get recursion and the other basics of logic programming.

    Prolog recursion

    That little note aside, your problem is that you’ve not properly understood Prolog recursion yet. Please see the first part of this answer that already adresses this concern.

    Your third clause is wrong:

    delMember(X, [T|Xs], Y) :- delMember(X, Xs, Y2), append([T], Y2, Y).
    

    it should read:

    delMember(X, [T|Xs], [T|Y]) :- delMember(X, Xs, Y).
    

    Well, it’s not really wrong, it’s just really suboptimal. It’s not tail-recursive and uses append/3, which would turn your linear predicate into a quadratic one. Plus, as you noticed, since it’s not tail-recursive, termination is tougher to obtain in some cases.

    Then, to remove the use of the cut !/0, you can consider adding a guard to the last clause:

    delMember(_, [], []).
    delMember(X, [X|Xs], Y) :-
        delMember(X, Xs, Y).
    delMember(X, [T|Xs], [T|Y]) :-
        dif(X, T),
        delMember(X, Xs, Y).
    

    The guard, dif(X, T), specifies that if we’re in the third case we cannot be in the second at the same time : X cannot be unified with T here.

    Note, there’s still one way we can’t use the predicate, this is, +, -, +, as cTI tells us. So queries like ?- delMember(1, R, [2, 3]). will loop with my version.

    I hope it has been useful.

    • 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 a procedure that returns a list of all primes below
I'm trying to write a stored procedure that will have 6 bit value flags
I am trying to write a procedure order(List,Result) that has a List as input
I am trying to write a stored procedure that checks if a value exist,
I'm trying to write a little procedure that write (append would be even better)
I'm trying to write a simple procedure that will add to a List every
I'm trying to write an procedure that does something after 2 objects are returned
I'm trying to write a procedure for a baseball program that will use multiple
I'm trying to write a stored procedure in SQL that will : Make a
I am trying to write a stored procedure that will allow me to write

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.