/* Difference lists in Picat. 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. % % Note the need for $ to "escape" the terms. % go => dappend($[a,b,c|E]-E, $[x,y,z|W]-W, O), writeln(o=O), add_at_end($[a,b,c|E2]-E2, a, O2), writeln(o2=O2), nl. go2 => L1=$[[0:_I in 1..10]|E1]-E1, writeln(l1=L1), L2=$[[1:_I in 1..10]|E2]-E2, writeln(l2=L2), dappend(L1,L2,L3), writeln(l3=L3), writeln(len=L3.length), nl. % another approach go3 => app($s([a,b,c|T],T),$s([d,e|R],R),X), println(x=X), nl. go4 => app2([[a,b,c|T],T],[[d,e|R],R],X), println(x=X), nl. % Picat's version of Prolog's % append(I-M, M-O, I-O). dappend(IM,MO,IO) => IM=$I-M, MO=$M-O, IO=$I-O. % Add element at end of a list % From Bratko "Prolog Programming for Artifical Intelligence" % add_at_end(L1-[Item|Z2], Item,L1-Z2). add_at_end(L1ItemZ2,Item,L1Z2) => L1ItemZ2=$L1-[Item|Z2], L1Z2 = $L1-Z2. % app(s(X,V),s(V,V1),s(X,V1)) => true. app(SXV,SVV1,SXV1) => SXV = $s(X,V), SVV1 = $s(V,V1), SXV1 = $s(X,V1). % a variant using lists instead of s/2 app2(XV,VV1,XV1) => XV = [X,V], VV1 = [V,V1], XV1 = [X,V1]. % this don't work (unfortunately, but that's how Picat works) % app2([X,V],[V,V1],[X,V1]) => true.