/* Collections (Rosetta code) in Picat. http://rosettacode.org/wiki/Collections """ Collections are abstractions to represent sets of values. In statically-typed languages, the values are typically of a common data type. Task Create a collection, and add a few values to it. """ This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ main => go. % List go => L = [1,2,3,4], L2 = L ++ [[5,6,7]], % adding a list L3 = ["a string"] ++ L2, % adding a string println(l=L), println(l2=L2), println(l3=L3), % Prolog way append([0],L,[5],L4), println(l4=L4), nl. % Arrays % Array are often used instead of lists for speed. go2 => L = {1,2,3,4}, L2 = {0} ++ L ++ {5}, println(l=L), println(l2=L2), % N-D arrays M = new_array(4,4), bind_vars(M,0), % initialize to 0 println(M), nl. % Maps % associative arrays/dictionaries go3 => Map = new_map(), Map.put(a,1), Map.put(b,2), println(Map), % Initialize map with values Map2 = new_map([c=3,d="some value"]), println(Map2), nl. % Sets % A set is a map where every key is associated with the atom 'not_a_value'. % All of the built-ins for maps can be applied to sets. go4 => S = new_set([1,2,3,4,5,"picat"]), println(S), S.put(1), S.put(21), println(S), nl. % Heaps % A heap is a complete binary tree represented as an array. A heap can be a min-heap or a max- % heap. In a min-heap, the value at the root of each subtree is the minimum among all the values in % the subtree. In a max-heap, the value at the root of each subtree is the maximum among all the % values in the subtree. go5 => L = [1,3,2,4,5,3,6], H = new_min_heap(L), H.heap_push(-123), println(H). % % Structures % A structure takes the form $s(t1,...,tn), where s is an atom, and n is called the arity of the % structure. The dollar symbol is used to distinguish a structure from a function call. % go6 => S = new_struct(a_struct,5), println(S), S[2] = 4, % place 4 at position 2 arg(3,S,string), % place string at position 3 println(S), nl.