REVISED: Monday, September 22, 2025
1. INTRODUCTION
The following is a simple and easy-to-understand OCaml program that implements a lookup table, often called a dictionary or map. It uses a common OCaml feature called an "association list" to store the key-value pairs. It defines a simple dictionary and includes functions to add and look up items, along with a clear example of how to use it. The comments explain the purpose of each line of code.
2. EXAMPLE
(* ocaml C:\AI2025\dictionary.ml *)
(* OCaml Lookup Table (Dictionary) Example *)
(* Type alias for our dictionary.
We are defining a dictionary for any key type ('k) and any value type ('v)
as a list of tuples, where each tuple is a (key, value) pair.
For example: [("apple", 1); ("banana", 2)] *)
type ('k, 'v) dictionary = ('k * 'v) list
(* An empty dictionary is simply an empty list. *)
let empty_dictionary : ('k, 'v) dictionary = []
(* Function to add a new key-value pair to the dictionary.
'key' is the key we want to add (e.g., "grape").
'value' is the value associated with the key (e.g., 3).
'dict' is the dictionary we are adding to.
The '::' operator adds the new (key, value) tuple to the front of the list.
This is an efficient operation for lists in OCaml. *)
let add key value dict =
(key, value) :: dict
(* Function to find a value by its key in the dictionary.
'key' is the key we are searching for.
'dict' is the dictionary we are searching in.
'List.assoc_opt' is a built-in OCaml function that safely searches an
association list. It returns the value wrapped in `Some`, like `Some 1`, if
the key is found. If the key is not found, it returns `None`. Using `option`
(`Some`/`None`) is the standard way to handle potential failures in OCaml
without causing errors. *)
let lookup key dict =
List.assoc_opt key dict
(* --- Example Usage --- *)
(* Start by creating an empty dictionary to store fruit counts.
The type annotation `(string, int) dictionary` tells OCaml that our keys
will be strings and our values will be integers. *)
let my_fruit_counts : (string, int) dictionary = empty_dictionary
(* Add some fruits and their counts to the dictionary.
Each 'let' binding creates a new version of the dictionary with the added item.
The original dictionary is not changed (this is called immutability). *)
let my_fruit_counts = add "apple" 10 my_fruit_counts
let my_fruit_counts = add "banana" 5 my_fruit_counts
let my_fruit_counts = add "orange" 8 my_fruit_counts
(* Now, let's look up some values. *)
(* Look up the count for "apple". *)
let apple_count = lookup "apple" my_fruit_counts
(* Look up the count for "grape", which is not in our dictionary. *)
let grape_count = lookup "grape" my_fruit_counts
(* OCaml's 'match' expression is the standard way to handle 'option' types.
It lets us safely check if we got `Some value` or `None`. *)
let print_fruit_count fruit_name count_option =
(* Print the name of the fruit we are checking. *)
print_endline ("Looking for: " ^ fruit_name);
(* Match the result of the lookup. *)
match count_option with
| Some count -> (* This case runs if the key was found. 'count' holds the value. *)
print_endline (" Found! Count: " ^ (string_of_int count))
| None -> (* This case runs if the key was not found. *)
print_endline " Not found in the dictionary."
(* Use our printing function to show the results. *)
let () =
(* This is the main entry point of the program. *)
print_endline "--- Fruit Dictionary Operations ---";
print_fruit_count "apple" apple_count;
print_fruit_count "grape" grape_count;
print_endline "---------------------------------"
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows
OCaml version: The OCaml toplevel, version 5.3.0
Coq-LSP version: 0.2.3
Loading personal and system profiles took 1101ms.
PS C:\Users\User> ocaml C:\AI2025\dictionary.ml
--- Fruit Dictionary Operations ---
Looking for: apple
Found! Count: 10
Looking for: grape
Not found in the dictionary.
---------------------------------
PS C:\Users\User>
3. CONCLUSION
An OCaml lookup table dictionary uses a common OCaml feature called an "association list" to store key-value pairs.
4. REFERENCES
Bird, R. (2015). Thinking Functionally with Haskell. Cambridge, England: Cambridge University Press.
Davie, A. (1992). Introduction to Functional Programming Systems Using Haskell. Cambridge, England: Cambridge University Press.
Goerzen, J. & O'Sullivan, B. & Stewart, D. (2008). Real World Haskell. Sebastopol, CA: O'Reilly Media, Inc.
Hutton, G. (2007). Programming in Haskell. New York: Cambridge University Press.
Lipovača, M. (2011). Learn You a Haskell for Great Good!: A Beginner's Guide. San Francisco, CA: No Starch Press, Inc.
Thompson, S. (2011). The Craft of Functional Programming. Edinburgh Gate, Harlow, England: Pearson Education Limited.