链表 OCaml
如何创建一个链接列表来保存 OCaml 中的数据?我正在尝试创建一个单链表,但是我在语法上遇到了问题。我只想制作一个模块来简单地从链接列表中获取'a,插入'a或删除'a。
有人有什么想法吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何创建一个链接列表来保存 OCaml 中的数据?我正在尝试创建一个单链表,但是我在语法上遇到了问题。我只想制作一个模块来简单地从链接列表中获取'a,插入'a或删除'a。
有人有什么想法吗?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
正如轶事所说,ocaml 已经有了列表。
然而,为了您的兴趣,您可以通过以下方式构建自己的列表。显然,您永远不会在真正的应用程序上使用它:)如果您想要一些树,数据结构将非常相似。
As aneccodeal told, ocaml already has lists.
However, for your interest, this is how you could build your own list. Obviously, you would never use it on a real application :) If you want to have some trees, the data structure would be very similar.
OCaml 具有内置列表:
整数列表:
[1;2;3;4;5] ;;
返回: int 列表 = [1; 2; 3; 4]
字符串列表:
[“这个”;“那个”;“其他”];;
返回: 字符串列表 = ["this"; “那”; "other"]
或者您可以使用 cons 运算符 :: 来构建列表:
1::2::3::[];;
返回: int 列表 = [1; 2; 3]
获取列表的头部(第一项):
List.hd [1;2;3]
返回 1
获取列表的尾部(第一项之后的所有项目)
List.tl [1;2;3]
返回: int 列表 = [2; 3]
此外,您还可以通过查看以下内容来了解 OCaml 标准库中列表的实现方式:
[OCaml 的安装位置]/lib/ocaml/std-lib/list.ml
OCaml has lists built in:
List of integers:
[1;2;3;4;5] ;;
returns: int list = [1; 2; 3; 4]
List of Strings:
["this";"that";"other"];;
returns: string list = ["this"; "that"; "other"]
Or you can use the cons operator :: to build lists:
1::2::3::[];;
returns: int list = [1; 2; 3]
To get the head (first item) of a list:
List.hd [1;2;3]
returns 1
To get the tail of a list (all items after the first item)
List.tl [1;2;3]
returns: int list = [2; 3]
Also, you can have a look at how lists are implemented in the OCaml standard library by looking at:
[installation location for OCaml]/lib/ocaml/std-lib/list.ml
OCaml 不是已经将列表作为原语了吗?我从大学开始就没有做过 SML,但我似乎记得
head
和tail
原语。我看到其他人已经实现了真正的链表数据结构...查看 Dustin 的 OCaml 链接列表 例如。Doesn't OCaml already have lists as a primitive? I haven't done SML since college, but I seem to recall
head
andtail
primitives. I see that other people have implemented a true linked list data structure out there though... check out Dustin's OCaml Linkedlist for example.