2

I am working on writing an insert function that takes two arguments, a number and a sorted list and it should produce that same list with the number included in its proper position. Here's what I got so far:

insert1(X,[]) :-
   [X].

insert1(X, [H|T]) :-
   X > H,
   insert1(X,T).

insert1(X,[H|_T]) :-
   X < H,
   T is [X|T]. 

I am getting the following error:

ERROR: '.'/2: Arguments are not sufficiently instantiated ("x" must hold one character)
Exception: (6) insert1(2, [1, 4, 5]) ? creep

I would appreciate your help folks.

false
  • 10,264
  • 13
  • 101
  • 209
Stranger
  • 864
  • 4
  • 21
  • 48

1 Answers1

0

The error message it's rather cryptic: '.'/2 it's the list constructor, and you're calling it. See this question for a detailed explanation of the syntax.

Also the last clause is wrong, because is/2 must be used only for arithmetic.

But generally you are approaching the problem from a wrong 'perspective'. Arguments in Prolog are immutable. You need another argument to hold the modified list.

Here is a possible workaround for the first and third clause

insert1(X, [], [X]).
...
insert1(X, [H|T], [X,H|T]) :-
  X < H.

You will need to adjust the second clause and invoke insert1 in this way

?- insert1(3, [1,2,3,4], L).
Community
  • 1
  • 1
CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • CapelliC, let me work on this and I will get back to you if i have any problems. Thanks for the help. – Stranger Dec 16 '12 at 23:27
  • I am still having issues with this one: Here's what I have so far: insert1(X,[],[X]). insert1(X,[H|T],[H|T]) :- X > H, insert1(X,T,_T1). insert1(X,[H|_T],[X,H|T]) :- X < H. I am getting the same original list if I try to insert something in my list. Any help will be appreciated. – Stranger Dec 17 '12 at 23:33
  • second rule must copy H, but T will change, then introduce another Var and pass it down into recursive call. – CapelliC Dec 17 '12 at 23:39
  • This is what I have to the best of my understanding about the hint you gave sir: insert1(X,[],[X]). insert1(X,[H|T],[H|T1]) :- X > H, insert1(X,T,[_Z|T1]). insert1(X,[H|_T],[X,H|_T1]) :- X < H. – Stranger Dec 17 '12 at 23:52
  • Why do you prefix variables with underscore? don't ! Attempt to keep rules simple as possible. Second rule will be *very* similar to third I wrote in the answer! – CapelliC Dec 17 '12 at 23:59
  • thanks for the help so far. When you say introduce new Var, how do I use that var ? – Stranger Dec 18 '12 at 00:20
  • This is what I got so far and strangely I tested for 3 [4,5] it works but if do 3 [2,4,5], it displays [2,4,5]..Any ideas why? Here's my code: insert1(X,[],[X]). insert1(X,[H|T],[X,H|T]) :- X < H. insert1(X,[H|T],[H|T]) :- X > H, insert1(X,T,_T1). – Stranger Dec 18 '12 at 00:58
  • Refer to http://stackoverflow.com/questions/9004265/inserting-x-in-its-correct-position-in-a-sorted-list – Stranger Dec 18 '12 at 01:12
  • 1
    Again in your third rule you have _T1, that's a singleton! Well, the rule should read `insert1(X,[H|T],[H|R]) :- X > H, insert1(X,T,R).` – CapelliC Dec 18 '12 at 06:49