What happened to my Prolog List of Fillers?

I wrote a simple program to try to populate a list of a given length with elements that satisfy a certain restriction.

For example, I want to create a list of 4 integers from 0 to 9, which at least contains 3and 4. I can recall several (thousands, really) such lists:

[3,4,0,0]
[0,3,4,0]
[3,1,9,4]
etc...

But SWI Prolog just returns false. Am I just making some kind of logical mistake, or am I using Prolog incorrectly?

My code is:

is_single_digit_integer(N) :-
  integer(N),
  between(0, 9, N).

filled_list(Given, FillConstraint, OutLen, Out) :-
  is_list(Out),
  length(Out, OutLen),
  length(Given, GivenLen),
  between(0, OutLen, GivenLen),
  maplist(FillConstraint, Out),
  subset(Given, Out).

And by running the example below:

?- filled_list([3,4], is_single_digit_integer, 4, X).
+4
source share
2 answers

integer(N) is_list(Out) . , :

is_single_digit_integer(N) :-
  between(0, 9, N).

filled_list(Given, FillConstraint, OutLen, Out) :-
  length(Out, OutLen),
  length(Given, GivenLen),
  between(0, OutLen, GivenLen),
  maplist(FillConstraint, Out),
  subset(Given, Out).

, :

?- filled_list([3,4], is_single_digit_integer, 4, X).
X = [0, 0, 3, 4] ;
X = [0, 0, 4, 3] ;
X = [0, 1, 3, 4] ;
X = [0, 1, 4, 3] .

, , , Prolog . , ; , between , , length , .

:

?- filled_list([3,4], between(0,9), 4, X).
X = [0, 0, 3, 4] ;
X = [0, 0, 4, 3] ;
X = [0, 1, 3, 4] ;
X = [0, 1, 4, 3] ;
X = [0, 2, 3, 4] .
+4

:

Prolog Prolog , , . .

, integer/1 is_list/1. , ​​ , :

is_single_digit_integer(N) :-
  N in 0..9.

clpfd (in)/2 : N - 0 9.

:

?- is_single_digit_integer(N).
N in 0..9.

?- is_single_digit_integer(3).
true.

?- is_single_digit_integer(20).
false.

, integer/1 :

?- integer(N).
false.

& RightArrow; ?

is_list/1, :

?- is_list(Ls).
false.

, , Ls , , , :

length(Ls, _)

, Ls. :

?- length(Ls, _).
Ls = [] ;
Ls = [_6650] ;
Ls = [_6650, _6656] ;
Ls = [_6650, _6656, _6662] ;
etc.

Prolog, . , .

+5

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


All Articles