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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:03:21+00:00 2026-05-26T16:03:21+00:00

I wrote a bubble sort in Prolog (code below). It works but it smells.

  • 0

I wrote a bubble sort in Prolog (code below). It works but it smells. I’m quite new to prolog. Here’s the problematic part:

% Problem: convert the true value to something
% I can actually use.
sorted_value(X,X) :- sorted(X).
sorted_value(X,[]) :- not(sorted(X)).

It’s weird I need to use this function to convert a True value to something (in this case, []) and False to another thing in order to use them. Isn’t there a cleaner way?

% Bubble Sort a list.

% is the list sorted?
sorted([]).
sorted([Head|[]]).
sorted([First|[Second|Rest]]) :-
  min(First,Second,First),
  sorted([Second|Rest]).

% swap all pairs in the list that
% needs to be swapped
bubble_sort_list([], []).
bubble_sort_list([Head|[]],[Head]).
bubble_sort_list([First|[Second|Rest]], [One|Solution]) :-
  min(First,Second, One),
  max(First,Second,Two),
  bubble_sort_list([Two|Rest],Solution).

% Problem: convert the true value to something
% I can actually use.
sorted_value(X,X) :- sorted(X).
sorted_value(X,[]) :- not(sorted(X)).

% Repeatedly call bubble_sort until
% the list is sorted
bubble_sort_helper([],List, Solution) :-
  bubble_sort_list(List, SortedList),
  sorted_value(SortedList, Value),
  bubble_sort_helper(Value,SortedList, Solution).
bubble_sort_helper(A,List,List).

% this is what you call.
buuble_sort(List,Solution) :-
  bubble_sort_helper([],List,Solution).
  • 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-26T16:03:22+00:00Added an answer on May 26, 2026 at 4:03 pm

    Every predicate call either succeeds (possibly several times) or fails. If you think declaratively and really describe what a sorted list is, there would only rarely seem to be a need to explicitly represent the truth value itself in a program. Rather, it would typically suffice to place a predicate call like “ascending(List)” in a Prolog program to describe the case of a sorted list. There seems to be no advantage to instead call a predicate like “ascending(List, T)” and then use T to distinguish the cases. Why not use the implicit truth value of the predicate itself directly? If you really need to reify the truth value, you can for example do it like this to avoid calling ascending/1 twice:

    ascending(Ls, T) :- 
        (   ascending(Ls) -> T = true
        ;   T = false
        ).
    

    Notice how the truth of ascending/1 is used to distinguish the cases.

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

Sidebar

Related Questions

i write a c++ code for Bubble sort algorithm and i dont know how
I am trying to write a simple bubble sort in Common Lisp (also works
I'm looking to implement a bubble sort. I have the following code that I
I know bubble sort is probably not the fastest way to do this but
i wrote a regular merge sort array code and all i want to do
Wrote the following in PowersHell as a quick iTunes demonstration: $iTunes = New-Object -ComObject
I wrote a visual c# 2008 windows form application ,then i edited the code
I wrote a small program in VS2005 to test whether C++ global operator new
I wrote the question as a comment in the code, I think its easier
i Am given a problem to solve the Bubble breaker puzzle Game. there are

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.