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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:48:28+00:00 2026-05-25T18:48:28+00:00

How do I do an operation for each element of a list, in order?

  • 0

How do I do an operation for each element of a list, in order?

Based on these two resources:

  1. http://www.swi-prolog.org/pldoc/doc/swi/library/lists.pl
  2. http://www.swi-prolog.org/pldoc/doc_for?object=foreach/2

I imagine I can always rely on:

  • foreach(member(X, [1,2]), write(X)).

Is that deterministic and can I wrap the member/2 predicate as I please in my own predicates and still always iterate in order?

  • 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-25T18:48:29+00:00Added an answer on May 25, 2026 at 6:48 pm

    Yes, but you have to worry about your predicate failing. If it can, the remaining elements in the list will not be processed because it produces a conjunction rather than a failure-driven loop.

    I would be more keen to use maplist/2 since I think it is more widely used than foreach/2 but I also hadn’t seen this option before. 🙂

    Edit: Let’s discuss what I mean about failure-driven loops.

    There are two primitive iteration methods in Prolog: recursion and failure-driven loops. Say I want to print out every item in a list. The recursive method is going to look like this:

    print_all([]).
    print_all([X|Rest]) :- write(X), nl, print_all(Rest).
    

    So given a list like [1,2,3], this is going to expand like so:

    print_all([1,2,3])
      write(1), nl, print_all([2,3])
        write(1), nl, write(2), nl, print_all([3])
          write(1), nl, write(2), nl, write(3), nl, print_all([])
            write(1), nl, write(2), nl, write(3), nl.
    

    This is how member/2 is usually implemented:

    member(X, [X|_]).
    member(X, [_|Xs]) :- member(X, Xs).
    

    So you can see the recursive method is pretty simple and general.

    Another simple but somewhat frowned-upon method is to simulate a failure to engage the backtracking mechanism. This is called a failure-driven loop and looks like this:

    print_all(List) :- member(X, List), write(X), nl, fail.
    print_all(_).
    

    When you run this version of print_all/1, what happens is a little more complex than simple expansion.

    print_all([1,2,3])
      member([1,2,3], 1)
        write(1), nl
          fail
      retry member([1,2,3], 2)
        write(2), nl
          fail
      retry member([1,2,3], 3)
        write(3), nl
          fail
    retry print_all(_)
      true
    

    Verbally, the fail forces Prolog to back up to the last choice point it made and try using the next solution. Well, write/1 and nl/0 don’t produce choice points because they only have one solution, but member/2 does have multiple solutions—one for each item in the list. So Prolog takes each item out of the list and prints it. Finally when member/2 runs out of solutions, Prolog backs up to the previous choice point, which is the second body of the print_all/1 predicate, which always succeeds. So the output looks the same. I think people nowadays generally prefer not to use failure-driven loops, but I don’t understand the arguments well enough to parrot them usefully.

    One thing that may help you to see what’s going on is use the trace predicate and step through the expansion of both versions, and see if you can make sense of the differences. My notation above is completely made up for this answer and may not be as clear.

    Looking back over what I originally wrote and your actual question:

    • foreach is going to be deterministic
    • member will always iterate in order, because lists are defined in such a way that you must access each item in turn

    Moreover, these days at least on S.O. you will get a lot of people telling you to use maplist and its ilk, so it’s probably not just going to work, but also a good idea.

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

Sidebar

Related Questions

Possible Duplicate: Does std::list::remove method call destructor of each removed element? I have a
Say I have a list of integers, where each element is a number from
Just for fun, I had a std::list of const char* , each element pointing
I have the following code which recursively operates on each element within a List
I've a list L of size N where each element of the list is
I have a multi-line string that I want to do an operation on each
I'm designing a database of statistics about the operation of mechanical equipment. Each batch
How can the XOR operation (on two 32 bit ints) be implemented using only
What's the most efficient way to lower case every element of a List or
I am trying to select an element in the list depending on its html

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.