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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:34:49+00:00 2026-05-31T02:34:49+00:00

I am working on a scenario in Prolog (eclipse) wherein I need a list

  • 0

I am working on a scenario in Prolog (eclipse) wherein I need a list structure to be reformatted.

I have a list of the form:

MyList = [a,b,c].

I was trying to see if I can flatten the list to a single element with all the commas replaced with the + operator.

So my result list would look like:

ResultList = [a+b+c] 

which is a single element list. The length of the initial list is arbitrary.

I know prolog is not suited for such operations, but can this be done?

  • 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-31T02:34:50+00:00Added an answer on May 31, 2026 at 2:34 am

    here it is, in standard Prolog. I think there should be no difference with Eclipse:

    list_to_op([X,Y|T], [R]) :-
        list_to_op(T, X+Y, R).
    

    edit: bug noted by false

    list_to_op([X], [X]).
    
    list_to_op([X], R, R+X).
    list_to_op([X|T], R, Q) :-
        list_to_op(T, R+X, Q).
    

    test:

    ?- list_to_op([a,b,c],X).
    X = [a+b+c] .
    

    The accumulator is required to give the appropriate associativity: the simpler and more intuitive definition

    list_to_op1([X], X).
    list_to_op1([X|R], X+T) :-
       list_to_op1(R, T).
    

    gives

    ?- list_to_op1([a,b,c],X).
    X = a+ (b+c) .
    

    If evaluation order is important, use list_to_op.

    edit:
    there is a bug: list_to_op([a,b],X) fails.

    here the correction, as often happens, it’s a simplification:

    list_to_op([], R, R).
    list_to_op([X|T], R, Q) :-
        list_to_op(T, R+X, Q).
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my scenario. I have a working login form that functions correctly. I
i need to model the following scenario, but I can't get it working with
I have a scenario in which DP Inheritance is not working, my code can
I am working on prolog and faced this scenario - In my query, I
the working scenario on a web bases app like a forum, the users can
I was working with spring-ehcache-annotations and came across following scenario: I have a simple
I am having difficulty trying to get a very simple scenario working with PRISM
I have multiple Google Maps on a single page. Scenario: Maps are working as
I'm new to RX, and I have my desired scenario working well, but it
Here is the scenario: In my working directory, I have a number of files

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.