Reputation: 63
I have a simple assignment of writing an insertion sort in Prolog. Here are the instructions:
(10 points) insertionSort(List, Sorted) Write an insertion sort program in prolog. You may assume all elements of the list are numbers.
Basically, I have to give it a list, and it will return a list of the sorted values. After completely failing when writing it myself (though mine seems similar to everything else - see my code posted below), I decided to go get help on the internet. I have literally tried every example of an insertion sort I have found on the internet for prolog...and not a SINGLE ONE worked. Not a single one.
I don't understand why. Maybe it's because I am supposed to be using SWI prolog.
I keep getting the two following errors:
ERROR: toplevel: Undefined procedure: insertionSort/2 (DWIM could not correct goal)
ERROR: toplevel: Undefined procedure: insertionSort/3 (DWIM could not correct goal)
I am so fed up with this stupid error. It is in no way helpful. How come when I do the exact same thing I do to call another simple procedure (a sum procedure thing), by compiling the file and then calling the thing I want, it works, but with this insertionSort one, it doesn't? I am positive I am not calling it incorrectly.
Here is my code so far. Not like it does anything.
insertionSort([],[]) :-
!.
insertionSort([H|T], X) :-
insertionSort(T, Y),
insert(H, Y, X).
insert(A, [], [A]) :-
!.
insert(A, [H|T], [A|L]) :-
A =< H,
insert(H, T, L).
insert(A, [H|T], [H|L]) :-
A > H,
insert(A, T, L).
Like I said, I've already tried probably 2 dozen + examples on the internet, and all of them come up with error messages, so if your answer is to link me to something, I can guarantee I already exhausted it (like this: Prolog insertion sort - doesn't that look like it would work? Well, it doesn't.)
Please, I am so frustrated. Any help is appreciated.
Upvotes: 0
Views: 1202
Reputation: 63
It was the weird machine. My code was perfect. It worked just fine. I don't know where those errors came from
also I have no idea how to mark this question as answered
Upvotes: 0