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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:47:27+00:00 2026-05-16T19:47:27+00:00

I am trying to write a predicate to transform an array of elements into

  • 0

I am trying to write a predicate to transform an array of elements into a set of elements. In order to do so, I have created the following predicates in swi-prolog:

sterge(_, [], []).

sterge(A, [A|L], New):-
    sterge(A, L, New).

sterge(A, [B|L], [B|New]):-
 A \= B,
 sterge(A, L, New).

multime(A, New):-
 mult(A, [], New).

mult([], A, A).

mult([A|L], [A|T], New):-
 sterge(A, L, L1),
 mult(L1, T, New).

As you can see, the sterge predicate, takes 3 arguments. Its main purpose is to delete all the appearances of the A element into the L list, resulting the New list which doesn’t contain any A elements.

With the help of this predicate, I have tried to eliminate all the elements from a list, one by one, including all their appearances and add them to a new list which contains a single copy of the elements of the given array which is required to be transformed into a set of elements.

The mult predicate takes 3 arguments.

The first argument is the given array which has to be transformed into a set.

The second argument is a buffer list used to contain the unique elements of the array, one by one during the execution of the predicate.

And the third argument is used to return the complete set of arguments.

Practically, I wanted the third argument to be unified with the value of the second argument, when the given array, the first argument, is null([]).

And finally, the multime predicate who takes two arguments( the given array and the desired set) launches a call over the mult predicate with the following values for its arguments. For the first argument it takes the given array, for the second argument, the null list([]), and for the third argument, the desired set of elements, which needs to be obtained.

However, when I launch a call for the multime predicate, I always receive the falseresult. Could you please tell me where do I go wrong?

  • 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-16T19:47:28+00:00Added an answer on May 16, 2026 at 7:47 pm

    the body of your mult is broken.

    mult([A|L], [A|T], New):-
     sterge(A, L, L1),
     mult(L1, T, New).
    

    you are spliting the list of the first argument AND of the second argument. mult is called from multime with an empty list as second argument. the empty list cannot be splitted and so that predicate cannot match. that is why you are getting a false.

    here is the fixed code

    mult([A|L], T, New):-
      sterge(A, L, L1),
      mult(L1, [A|T], New).
    

    the list concatenating is done in the recursive call to mult. the recursion will end when L1 is an empty list, and the concatenated list will be returned via the New variable. note: the new list will be in reversed order.

    • 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 predicate that divides a list into N parts. This
While learning Prolog, I'm trying to solve the following problem, using accumulators: Write a
I'm trying to write some predicates to solve the following task (learnprolognow.com) Suppose we
I'm having some difficulties in prolog, I'm trying to write a predicate that will
I am trying to write a predicate palindrome/1 in Prolog that is true if
I was trying to write predicate divide(L,Len,Slist) which will be true when Slist can
I m trying write code that after reset set up rrpmax as 3000. It
I'm trying to write a predicate to check a specific value in a dictionary
I am trying to figure out how to write the following SQL in the
I'm trying to complete an exercise to write a program that takes the following

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.