1

I need to generate a list of booleans in prolog.

?- gener_booleans(Xs,3).
Xs = [true, true, true] ;
Xs = [true, true, false] ;
...
Xs = [false, false, false] ;

Here is link to another solution, but i do not know how to apply it to booleans. Get all sets of list in prolog Can anybody help? P.S the amount of lists is 2^N. Thanks!

Community
  • 1
  • 1

2 Answers2

3

If your Prolog has the maplist predicate (like SWI and YAP):

booleans(Xs, N) :-
    length(Xs, N),
    maplist(boolean, Xs).
boolean(true).
boolean(false).

(I renamed the predicate booleans because it can also check for booleans; prefer declarative names when programming in Prolog.)

false
  • 10,264
  • 13
  • 101
  • 209
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • 1
    Also GNU-Prolog has `maplist/2..`. But only YAP has [`library(lambda)`](http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/ISO-Hiord) preinstalled! Permitting: `maplist(\X^(X=true;X=false), Xs)`. BTW: not sure: shall the type be it be `bool` or `boolean`? – false Apr 29 '12 at 20:46
1
gener_booleans([],0).
gener_booleans([true|Xs],N) :- N>0, N1 is N-1, gener_booleans(Xs,N1).
gener_booleans([false|Xs],N) :- N>0, N1 is N-1, gener_booleans(Xs,N1).
Alexander Serebrenik
  • 3,567
  • 2
  • 16
  • 32