链表 OCaml

发布于 2024-08-11 09:15:11 字数 109 浏览 3 评论 0 原文

如何创建一个链接列表来保存 OCaml 中的数据?我正在尝试创建一个单链表,但是我在语法上遇到了问题。我只想制作一个模块来简单地从链接列表中获取'a,插入'a或删除'a。

有人有什么想法吗?

How would I create a linked list to hold my data in OCaml? I'm trying to make a singly linked list, however I'm having trouble with the syntax. I just want to make a module to simply get the 'a from the linked list, insert 'a or delete 'a.

Anyone have any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傲鸠 2024-08-18 09:15:11

正如轶事所说,ocaml 已经有了列表。

然而,为了您的兴趣,您可以通过以下方式构建自己的列表。显然,您永远不会在真正的应用程序上使用它:)如果您想要一些树,数据结构将非常相似。

exception Empty_list

type 'a my_list = Nil | List of 'a * 'a my_list

let head = function 
     Nil -> raise Empty_list
   | List(e,_) -> e;;

let tail = function
    Nil -> Nil
   | List(_,t) -> t

let l = List(1, List(4, List(8, Nil)));;

print_endline (string_of_int(head l));;

print_endline (string_of_int (head(tail l)));;

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.

exception Empty_list

type 'a my_list = Nil | List of 'a * 'a my_list

let head = function 
     Nil -> raise Empty_list
   | List(e,_) -> e;;

let tail = function
    Nil -> Nil
   | List(_,t) -> t

let l = List(1, List(4, List(8, Nil)));;

print_endline (string_of_int(head l));;

print_endline (string_of_int (head(tail l)));;
九八野马 2024-08-18 09:15:11

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

谁许谁一生繁华 2024-08-18 09:15:11

OCaml 不是已经将列表作为原语了吗?我从大学开始就没有做过 SML,但我似乎记得 headtail 原语。我看到其他人已经实现了真正的链表数据结构...查看 Dustin 的 OCaml 链接列表 例如。

Doesn't OCaml already have lists as a primitive? I haven't done SML since college, but I seem to recall head and tail 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文