/* maplist/2,3,4,5 in Picat. Testing misc maplist variants. 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. % % Testing misc maplist variants % go => L1 = [2,4,6,8,10], if maplist(even,L1) then println(correct1) else println(wrong1) end, L2 = [2,4,6,8,10,11], if maplist(even,L2) then println(wrong2) else println(correct2) end, nl. % Using maplist/2 and all_different/1 (CP module) go2 ?=> N = 3, A = new_array(N,N), A :: 1..N, AList = A.array_matrix_to_list_matrix(), maplist(all_different,AList), maplist(all_different,AList.transpose()), solve(A), println(A), fail, nl. go2 => true. % % Misc tests % go3 => numlist(1,6,L), writeln(l=L), println("\ntesting maplist(xyz,L,L,Res1)"), maplist(xyz,L,L,Res1), writeln(res1=Res1), println("\ntesting maplist(take2,L,Res1,Res2)"), maplist(take2,L,Res1,Res2), writeln(res2=Res2), println("\ntesting maplist(take3,L,L,L,Res3)"), maplist(take3,L,L,L,Res3), writeln(res3=Res3), println("\ntesting maplist(take4,L,L,L,L,Res4)"), maplist(take4,L,L,L,L,Res4), writeln(res4=Res4), println("\ntesting maplist(twice,L,Twice)"), maplist(twice,L,Twice), writeln(twice=Twice), println("\ntesting maplist(even,Twice)"), maplist(even,Twice), println("\ntesting maplist(add1,Twice,Odds)"), maplist(add1,Twice,Odds), writeln(odds=Odds), maplist(odd,Odds), nl. %% %% Testing numlist/4 and more maplist/n %% go4 => numlist(1,2,10,L), writeln(L), numlist(3,2,21,L2), writeln(L2), numlist(0,1000,10_000,L3), writeln(l3=L3), maplist(add1,L3,L4), writeln(l4=L4), % Note: One have to escape ($) the predicate here. maplist($addn(11),L3,L5), writeln(l5=L5), nl. % % maplist(Goal, List). % True if Goal can successfully be applied on all elements of List. % % maplist(Goal, List1, List2) % As maplist/2 on two elements from two lists % maplist(Goal, List1, List2, List3) % As maplist/2 on three elements from three lists % maplist(Goal, List1, List2, List3, List4) % As maplist/2 on four elements from four lists % % maplist(Goal, List1, List2, List3, List4, List5) % As maplist/2 on five elements from five lists % % maplist(Goal, List) ?=> maplist_(List, Goal). maplist(Goal, List1, List2) ?=> maplist_(List1, List2, Goal). maplist(Goal, List1, List2, List3) ?=> maplist_(List1, List2, List3, Goal). maplist(Goal, List1, List2, List3, List4) ?=> maplist_(List1, List2, List3, List4, Goal). maplist(Goal, List1, List2, List3, List4, List5) ?=> maplist_(List1, List2, List3, List4, List5, Goal). %% %% Helper predicates %% maplist_([], _) ?=> true. maplist_(ElemTail, Goal) ?=> ElemTail = [Elem|Tail], call(Goal, Elem), maplist_(Tail, Goal). maplist_([], X, _) ?=> X = []. maplist_(Elem1Tail1, Elem2Tail2, Goal) ?=> Elem1Tail1 = [Elem1|Tail1], Elem2Tail2 = [Elem2|Tail2], call(Goal, Elem1, Elem2), maplist_(Tail1, Tail2, Goal). maplist_([], X, Y, _) ?=> X = [], Y = []. maplist_(Elem1Tail1, Elem2Tail2, Elem3Tail3, Goal) ?=> Elem1Tail1 = [Elem1|Tail1], Elem2Tail2 = [Elem2|Tail2], Elem3Tail3 = [Elem3|Tail3], call(Goal, Elem1, Elem2, Elem3), maplist_(Tail1, Tail2, Tail3, Goal). maplist_([], X, Y, Z, _) ?=> X = [], Y = [], Z = []. maplist_(Elem1Tail1, Elem2Tail2, Elem3Tail3, Elem4Tail4, Goal) ?=> Elem1Tail1 = [Elem1|Tail1], Elem2Tail2 = [Elem2|Tail2], Elem3Tail3 = [Elem3|Tail3], Elem4Tail4 = [Elem4|Tail4], call(Goal, Elem1, Elem2, Elem3, Elem4), maplist_(Tail1, Tail2, Tail3, Tail4, Goal). maplist_([], X, Y, Z, A, _) ?=> X = [], Y = [], Z = [], A = []. maplist_(Elem1Tail1, Elem2Tail2, Elem3Tail3, Elem4Tail4, Elem5Tail5, Goal) ?=> Elem1Tail1 = [Elem1|Tail1], Elem2Tail2 = [Elem2|Tail2], Elem3Tail3 = [Elem3|Tail3], Elem4Tail4 = [Elem4|Tail4], Elem5Tail5 = [Elem5|Tail5], call(Goal, Elem1, Elem2, Elem3, Elem4, Elem5), maplist_(Tail1, Tail2, Tail3, Tail4, Tail5, Goal). %% %% numlist(Lower,Upper, List) %% %% Create a list with elements from L..U %% numlist(L, U, Ns) ?=> integer(L), integer(U), L= U = U1, List=[U]. numlist_(L, U, LNs) => LNs = [L|Ns], L2 is L+1, numlist_(L2, U, Ns). %% %% numlist(L, Step, U, Ns) %% %% Create a list with element from L..U with step Step %% numlist(L, Step, U, Ns) ?=> integer(L), integer(U), L= U + Step > U2, List=[U]. numlist_(L, Step, U, LNs) => LNs = [L|Ns], L + Step =< U, L2 is L + Step, numlist_(L2, Step, U, Ns). %% %% Some test predicates %% add1(X,Y) => Y #= X + 1. addn(N, X,Y) => Y #= X + N. twice(X,Y) => Y #= 2*X. xyz(X,Y,Z) => writeln($xyz(X,Y,Z)), Z #= X + Y + 1. take2(X,Y,ZT) => ZT = [Z,T], Z #= X + 1, T #= Y + 1. take3(X,Y,Z, Res) => Res #= X + 2*Y + 3*Z. take4(X,Y,Z,A, Res) => Res #= X + 2*Y + 3*Z + A.