Prolog: variable list header not set

I am writing simple code generating a simple list with 5 numbers, the first variable must be positive, and I am trying to understand why this code does not work

test([H|T]) :- H > 0, length(T,4).

when i call using

 length(X,5), test(X).

he shows me the following error:

ERROR: Arguments are not sufficiently instantiated

When I debug the code, the variable Hin is testnot created.

Does anyone know why?

+4
source share
1 answer

, test([H|T]) Prolog, H . , H > 0, , H . (H > 0 ) , Prolog , H , , H.

, test/1 (T), , 4. , , 5, .

, test(L) , , L - . CLP (FD):

:- use_module(library(clpfd)).

test(X) :- X ins 1..10000.

, X - , 1 10000. 5 :

?- length(X, 5), test(X), label(X).
X = [1, 1, 1, 1, 1] ;
X = [1, 1, 1, 1, 2] ;
X = [1, 1, 1, 1, 3] ;
X = [1, 1, 1, 1, 4] ;
X = [1, 1, 1, 1, 5] ;
...

, , all_different/1:

test(X) :- X ins 1..10000, all_different(X).

?- length(X, 5), test(X), label(X).
X = [1, 2, 3, 4, 5] ;
X = [1, 2, 3, 4, 6] ;
X = [1, 2, 3, 4, 7] ;
X = [1, 2, 3, 4, 8] ;
X = [1, 2, 3, 4, 9] ;
X = [1, 2, 3, 4, 10] ;
...
+5

Source: https://habr.com/ru/post/1680338/


All Articles