OCaml 中的查找表
我想在 OCaml 中创建一个查找表。 该表将包含 7000 多个条目,在查找(通过 int)时,返回一个字符串。 用于此任务的合适数据结构是什么? 该表是否应该从基本代码中外部化,如果是这样,如何“包括”可从他/她的程序访问的查找表?
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果使用连续整数对字符串进行寻址,则可以使用数组。
否则,您可以使用哈希表(非功能性)或映射(功能性)。 要开始使用地图,请尝试:
如果表太大而无法存储在内存中,您可以将其存储在外部数据库中并使用到 dbm,bdb,sqlite,...
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:
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,...
要将表存储在单独的文件中(例如作为数组),只需创建一个包含以下内容的文件
strings.ml
:编译此文件:
如 manual,这定义了其他 Ocaml 模块可以使用的模块
Strings
参考。 例如,您可以启动一个顶层:并通过访问数组中的特定位置来查找字符串:
To store the table in a separate file (e.g. as an array), simply create a file
strings.ml
with the content:Compile this with:
As explained in the manual, this defines a module
Strings
that other Ocaml modules can reference. For example, you can start a toplevel:And lookup a string by accessing a particular position in the array: