/* Collect 0s last in a list in Picat. If there is a 0 in the list, it must be placed last. Here are all solutions for N = 4 (with all_different_except_0/1 and increasing_except_0/1): [0,0,0,0] [1,0,0,0] [1,2,0,0] [1,2,3,0] [1,2,3,4] [1,2,4,0] [1,3,0,0] [1,3,4,0] [1,4,0,0] [2,0,0,0] [2,3,0,0] [2,3,4,0] [2,4,0,0] [3,0,0,0] [3,4,0,0] [4,0,0,0] This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> N = 4, X = new_list(N), X :: 0..N, all_different_except_0(X), increasing_except_0(X), collect_0s_last(X), solve(X), println(X), fail, nl. go => true. % % All 0s must be placed last % collect_0s_last(X) => N = X.len, foreach(I in 1..N) X[I] #= 0 #=> sum([X[J] #> 0 : J in I+1..N]) #= 0 end. increasing_except_0(A) => increasing_except_C(A,0). increasing_except_0_strict(A) => increasing_except_C_strict(A,0). increasing_except_C(A,C) => N = A.len, foreach(I in 1..N, J in I+1..N) (A[I] #!= C #/\ A[J] #!= C) #=> (A[I] #<= A[J]) end. increasing_except_C_strict(A,C) => N = A.len, foreach(I in 1..N, J in I+1..N) (A[I] #!= C #/\ A[J] #!= C) #=> (A[I] #< A[J]) end.