Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting a list, what is wrong with this code?

Status
Not open for further replies.

ProgrammerX

Programmer
Feb 8, 2003
1
GB
Hi, I am trying to sort a list of numbers. I want to do it this way - I remove the minimum number in the list, and insert into a new list. Then I remove the next minimum number and append it to the new list, and so on. This is what I have, but Prolog just answers 'No' to any query.

Please note that the predicates my_append and remove_min, both work correctly.

my_sort([],[]).
my_sort(Unsorted_List,Sorted_List):-
remove_min(Unsorted_List,Min,Unsorted_List),
my_append(Sorted_List,Min,Sorted_List).
my_sort(Unsorted_List,Sorted_List).

I would appreciate some pointers as to where I have gone wrong with this please.
Thank you
Maz
 
Maybe it's in the line
my_append(Sorted_List,Min,Sorted_List).
because you are forcing the first and third arguments to unify while they don't, because one is the other plus the appended item. just change the third argument variable name.

Please support my only question in return posted under my username (jason20992001).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top