0

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.

6
  • 2
    If you are sure that you are querying correctly. You might load the wrong file. The error message you've repeated there states that you try to call a procedure that isn't there. Are you getting any warnings/errors after you consult the program? May 3, 2015 at 22:52
  • If you are not into dealing with files and SWI installation, you might want to use it's online interpreter (it is somewhat limited of course, but should suffice your needs.
    – Eugene Sh.
    May 3, 2015 at 23:15
  • Now, every time I do a command, it says false. Including my other methods which worked before. x_x May 3, 2015 at 23:31
  • Describe your environment, the steps and show the code and queries
    – Eugene Sh.
    May 3, 2015 at 23:47
  • 3
    Once you think you have the predicates loaded/defined in SWI-Prolog, issue the query ?- listing. This will display all the predicates defined in the current module (probably the default user module unless you are actively using the modules feature).
    – hardmath
    May 4, 2015 at 0:21

1 Answer 1

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.