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.
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
You will need to adjust the second clause and invoke insert1 in this way