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

  • Home
  • SEARCH
  • 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 5988911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:01:32+00:00 2026-05-22T23:01:32+00:00

I was wondering if anyone could help me with this problem: I have to

  • 0

I was wondering if anyone could help me with this problem: I have to order a list using Prolog with Constraing Logic Programming and I must do it with the more efficient way I can.

So the main predicate I have defined is the next one:

order(Xs,Ys) :-
    same_length(Xs,Ys),      /* To determine the list Ys with the Xs' length */
    perm(Xs,Ys),             /* Permutation */
    ordered(Ys),             /* Is Ys ordered? */
    ! .

The implementation of each of the previous auxiliary predicates is as follows:

same_length(Xs,Ys) :-
    length(Xs,L),
    length(Ys,L).

perm([],[]).
perm([X|Xs],Ys) :- elem(X,Ys,Ws), perm(Xs,Ws).

ordered([]).
ordered([_]).
ordered([X,Y|Xs]) :- X =< Y, ordered([Y|Xs]).

elem(X,[X|Ys],Ys).
elem(X,[Y|Ws],[Y|Zs]) :- elem(X,Ws,Zs).

I have proved the program I made and it works! But I don’t know if it is possible to improve the efficiency, and if it is, how can I do it (I was reading this old thread here). Should I add or modify any of the constraints?

Thanks!

  • 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-22T23:01:32+00:00Added an answer on May 22, 2026 at 11:01 pm

    Your definition of same_length/2 will not terminate very often. Instead, consider

    same_length([],[]).
    same_length([_|Xs], [_|Ys]) :-
       same_length(Xs, Ys).
    

    equally, using library(lambda) use

    ... maplist(\_^_^true,Xs, Ys), ...
    

    in place of

    ... same_length(Xs, Ys), ...
    

    It seems you want to reformulate sorting by stating first, that the list is ordered, and only then searching for a permutation. Below works in SICStus, SWI, YAP.

    ordered2([]).
    ordered2([_]).
    ordered2([X,Y|Xs]) :-
       when((nonvar(X),nonvar(Y)),
            ( X =< Y, ordered2([Y|Xs]) )).
    
    list_sorted2(Xs,Ys) :-
        maplist(\_^_^true,Xs,Ys),
        ordered2(Ys),
        perm(Ys,Xs).
    

    Please note that the arguments in perm/2 are now exchanged! Using SWI:

    ?- time(order([10,9,8,7,6,5,4,3,2,1],Xs)).
    % 38,434,099 inferences, 10.655 CPU in 11.474 seconds (93% CPU, 3607101 Lips)
    
    ?- time(list_sorted2([10,9,8,7,6,5,4,3,2,1],Xs)).
    % 50,139 inferences, 0.023 CPU in 0.032 seconds (72% CPU, 2205620 Lips)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if anyone could take a look at this code and help
I'm wondering if anyone could help me out here. Is it possible when deleting
I'm really bad at regex and was wondering if anyone could help me with
I was wondering if anyone could help me solve how to view $_POST requests.
I have an idea of how I might do this but I was wondering
Was wondering if someone could give me some direction on this. I've spent a
I'm building an application using ASP.net MVC 3 and I'm wondering if anyone knows
im having a little problem with the Google API maps, i was wondering if
Let me start by saying, I am new to Java programming. I have coded
A few days ago i asked this question: Displaying images from Facebook photo albums

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.