Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lookup tables in OCaml

Tags:

lookup

ocaml

I would like to create a lookup table in OCaml. The table will have 7000+ entries that, upon lookup (by int), return a string. What is an appropriate data structure to use for this task? Should the table be externalized from the base code and if so, how does one go about "including" the lookup table to be accessible from his/her program?

Thanks.

like image 502
Mat Kelly Avatar asked Feb 06 '09 11:02

Mat Kelly


1 Answers

If the strings are addressed using consecutive integers you could use an array.

Otherwise you can use a hash table (non-functional) or a Map (functional). To get started with the Map try:

module Int =
struct
  type t = int
  let compare = compare
end ;;

module IntMap = Map.Make(Int) ;;

If the table is too large to store in memory, you could store it in an external database and use bindings to dbm, bdb, sqlite,...

like image 174
Bruno De Fraine Avatar answered Sep 19 '22 05:09

Bruno De Fraine