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.

Community
  • 1
  • 1
  • 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? – Patrick J. S. May 03 '15 at 22:52
  • If you are not into dealing with files and SWI installation, you might want to use it's [online interpreter](http://swish.swi-prolog.org/) (it is somewhat limited of course, but should suffice your needs. – Eugene Sh. May 03 '15 at 23:15
  • Now, every time I do a command, it says false. Including my other methods which worked before. x_x – lovechicken May 03 '15 at 23:31
  • Describe your environment, the steps and show the code and queries – Eugene Sh. May 03 '15 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](http://www.swi-prolog.org/pldoc/doc_for?object=listing/0) in the current module (probably the default `user` module unless you are actively using the modules feature). – hardmath May 04 '15 at 00:21
  • Guess what? I am going mad. I went to my professor today asking what the deal was. He ssh'ed into our linux lab computers where i was working, called the procedure...and it worked perfectly. I have no idea why it didn't work before. I think the program was wonky or something. Anyways, my code is perfect, and i think perhaps I was not calling it with a capital X. Thanks for all your answers! – lovechicken May 04 '15 at 20:53

1 Answers1

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