/* Find a specific number of solutions in Picat. Usage: init_fail2(N): - N > 0: try to find N solutions - N = 0: try to find all solutions fail2(): - as fail/0 but with the restriction of number of solution This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import util. % import cp. main => go. go => init_fail2(3), % find 3 solutions member(A,1..10), println(A), fail2(), nl. go2 => init_fail2(0), % find all solutions member(A,1..10), println(A), fail2(), nl. init_fail2(N) => % printf("Find %d solutions\n", N), if N > 0 then get_global_map().put(num_solutions,N) end. fail2() => M = get_global_map(), if M.has_key(num_solutions) then N = M.get(num_solutions), if N =< 1 then true else M.put(num_solutions,N-1), fail end else % fail if there's no init of # solutions fail end.