Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print out all the solutions in Prolog?

Tags:

prolog

I'm trying to print out the list containing all the solutions to a problem, but Prolog is giving me only part of the solutions and is cutting off in the middle like this:

[[something-something], [something-something], [something-something], [...-...], [...][...].

How do I get Prolog to print all the solutions?

like image 848
dtgee Avatar asked Nov 24 '25 14:11

dtgee


1 Answers

I use maplist/2

?- numlist(1,10,L),maplist(writeln,L).
1
2
3
4
5
6
7
8
9
10
L = [1, 2, 3, 4, 5, 6, 7, 8, 9|...].

If you are running SWI-Prolog, you can instruct it to avoid ellipsis

?- [user].
|: no_ellipsis :-
|:     current_prolog_flag(toplevel_print_options, V),
|:     select(max_depth(_), V, U),
|:     set_prolog_flag(toplevel_print_options, U).
|: end_of_file.
|: % user://3 compiled 0,05 sec, 1 clauses
true.

?- no_ellipsis.
true .

?- numlist(1,20,L).
L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20].

such rule could be placed in startup file, ( ~/.plrc on Unix )

Or you could use a write predicate

?- numlist(1,20,L),format('~w',[L]).
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
L = [1, 2, 3, 4, 5, 6, 7, 8, 9|...].
like image 56
CapelliC Avatar answered Nov 28 '25 16:11

CapelliC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!