Collections: Difference between revisions

1,711 bytes added ,  2 years ago
Line 2,866:
[two] => 2
)</pre>
 
=={{header|Picat}}==
===Lists===
<lang Picat>go =>
L = [1,2,3,4],
L2 = L ++ [[5,6,7]], % adding a list
L3 = ["a string"] ++ L2, % adding a string
 
% Prolog way
append([0],L,[5],L4).</lang>
 
===Arrays===
Array are often used instead of lists for speed. The array literal use <code>{...}</code> (instead of the lists <code>[...]</code>).
<lang Picat>go2 =>
L = {1,2,3,4},
L2 = {0} ++ L ++ {5},
 
% multi dimensional arrays
M = new_array(4,4),
bind_vars(M,0). % initialize to 0</lang>
 
===Maps===
Associative arrays/Dictionaries.
<lang Picat>go3 =>
Map = new_map(),
Map.put(a,1),
Map.put(b,2),
 
% Initialize map with values
Map2 = new_map([c=3,d="some value"]).</lang>
 
===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.
<lang Picat>go4 =>
S = new_set([1,2,3,4,5,"picat"]),
S.put(1),
S.put(21).</lang>
 
===Structures===
A structure takes the form <code>$s(t1,...,tn)</code>, where <code>s</code> 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.
<lang Picat>go5 =>
S = new_struct(a_struct,5),
S[2] = 4, % place 4 at position 2
arg(3,S,"string"). % place "string" at position 3</lang>
 
===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.
<lang Picat>go6 =>
L = [1,3,2,4,5,3,6],
H = new_min_heap(L),
H.heap_push(-123).</lang>
 
 
=={{header|PicoLisp}}==
495

edits