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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:27:03+00:00 2026-06-18T16:27:03+00:00

I’m developing an application written in Prolog. I get to a point in which

  • 0

I’m developing an application written in Prolog. I get to a point in which I want to filter a list of elements and remove those which do not meet a certain constraints or conditions, keeping the original list order. I thought the best way for doing it would be something like this:

filter([],Filtered).
filter([L|List],[F|Filtered]) :-
    /* Conditions are met: bypass the element to the filtered list */
    check_conditions(L),
    filter(List,Filtered).
filter([L|List],Filtered) :-
    /* Conditions are not met: do not include the element in the filtered list */
    filter_aborts(List,Filtered).

Before accepting it as a solution to my problem I wanted to give it a try (independently), so I compiled and run my code (SWI-Prolog) and tested a few situations. When I type in the Prolog query using a hard-coded list (or whatever you want to call it) I get what something like this:

?- filter([id01,id02,id03,id04,id05,id06],F).
F = [id03, id05, id06|_G1024] .

The list is certainly filtered but I get this instance name “_G1024” at the end of it. I understand this happens because F is not instantiated, but I don’t know what would be the solution. Moreover, if I try to do something different like entering the list as an instantiated variable, what I get is even more strange:

?- L=[id02,id03,id04,id05,id06].
L = [id02, id03, id04, id05, id06].

?- filter(L,F).
L = [] ;
L = [id01],
F = [id01|_G347] ;
L = [id01, id01],
F = [id01, id01|_G403] 
... and so on.

Aren’t Prolog variables supposed to be assign-once variables? Does my program actually change L or I am not understanding it correctly? A part from that, since I am new to Prolog, I would appreciate any comment regarding my Prolog -let’s say…- “style”.

  • 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-18T16:27:04+00:00Added an answer on June 18, 2026 at 4:27 pm

    your base recursion should be written

    filter([],[]).
    

    and you have a typo

    filter([L|List],[L|Filtered]) :-
    ...
    

    instead of (second) L you had the singleton F

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm interested in microtypography issues on the web. I want a tool to fix:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.