Associative array/Creation: Difference between revisions
Content added Content deleted
(→{{header|C}}: Moved to Associative arrays/Creation/C.) |
m (→Binary tree) |
||
Line 860: | Line 860: | ||
To retrieve a value, returning a default if the key is not found: |
To retrieve a value, returning a default if the key is not found: |
||
<lang ocaml>let quux = try StringMap.find "quux" map with Not_found -> some_value;;</lang> |
<lang ocaml>let quux = try StringMap.find "quux" map with Not_found -> some_value;;</lang> |
||
===Association list=== |
|||
Some list functions allow you to use a list as an associative map, although retrieval time is O(n) so a Hashtbl or binary tree should be used for larger data-sets. |
|||
<lang Ocaml>let dict = [("foo", 5); ("bar", 10); ("baz", 15)] |
|||
(* retrieve value *) |
|||
let bar_num = try List.assoc "bar" dict with Not_found -> 0;; |
|||
(* see if key exists *) |
|||
print_endline (if List.mem_assoc "foo" dict then "key found" else "key missing")</lang> |
|||
=={{header|Oz}}== |
=={{header|Oz}}== |