在 OCaml 中对列表进行排序

发布于 2024-08-30 18:06:23 字数 951 浏览 3 评论 0原文

以下是对任何给定列表进行排序的代码:

let rec sort lst =
   match lst with
     [] -> []
   | head :: tail -> insert head (sort tail)
 and insert elt lst =
   match lst with
     [] -> [elt]
   | head :: tail -> if elt <= head then elt :: lst else head :: insert elt tail;;

[来源:代码

但是,我收到了 Unbound 错误:

Unbound value tail
# let rec sort lst =
   match lst with
     [] -> []
   | head :: tail -> insert head (sort tail)
 and insert elt lst =
   match lst with
     [] -> [elt]
   | head :: tail -> if elt <= head then elt :: lst else head :: insert elt tail;;
Characters 28-29:
     | head :: tail -> if elt <= head then elt :: lst else head :: insert elt tail;;
     ^
Error: Syntax error

任何人都可以帮我理解这里的问题吗?我没有发现 headtail 在任何地方或代码中预定义

Here is the code on sorting any given list:

let rec sort lst =
   match lst with
     [] -> []
   | head :: tail -> insert head (sort tail)
 and insert elt lst =
   match lst with
     [] -> [elt]
   | head :: tail -> if elt <= head then elt :: lst else head :: insert elt tail;;

[Source: Code

However, I am getting an Unbound error:

Unbound value tail
# let rec sort lst =
   match lst with
     [] -> []
   | head :: tail -> insert head (sort tail)
 and insert elt lst =
   match lst with
     [] -> [elt]
   | head :: tail -> if elt <= head then elt :: lst else head :: insert elt tail;;
Characters 28-29:
     | head :: tail -> if elt <= head then elt :: lst else head :: insert elt tail;;
     ^
Error: Syntax error

Can anyone please help me understand the issue here?? I did not find head or tail to be predefined anywhere nor in the code

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

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

发布评论

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

评论(4

少女的英雄梦 2024-09-06 18:06:23

您的代码似乎是正确的,并且可以为我编译:

    Objective Caml version 3.11.1

# let rec sort lst = ...

val sort : 'a list -> 'a list = <fun>
val insert : 'a -> 'a list -> 'a list = <fun>

# sort [ 1 ; 3 ; 9 ; 2 ; 5 ; 4; 4; 8 ; 4 ] ;;
- : int list = [1; 2; 3; 4; 4; 4; 5; 8; 9]

Your code seems correct, and compiles for me:

    Objective Caml version 3.11.1

# let rec sort lst = ...

val sort : 'a list -> 'a list = <fun>
val insert : 'a -> 'a list -> 'a list = <fun>

# sort [ 1 ; 3 ; 9 ; 2 ; 5 ; 4; 4; 8 ; 4 ] ;;
- : int list = [1; 2; 3; 4; 4; 4; 5; 8; 9]
我一直都在从未离去 2024-09-06 18:06:23

添加到 Pascal 所说的内容中,列表类型定义为:

type 'a list = [] | :: of 'a * 'a list

这就是您要匹配列表 lst 的内容。

Adding to what Pascal said, the list type is defined as:

type 'a list = [] | :: of 'a * 'a list

and that's what you are matching your list lst against.

白衬杉格子梦 2024-09-06 18:06:23

符号“|”是横线符号,不是l字符,->是字符。是减号和更大的符号。我认为您复制并粘贴了 Inria 网站上的代码段。请检查并重写特殊符号。我测试了一下,效果很好。

The symbol “|“ is the horizontal line symbol, it is not l character and the -> are the minus symbol and the bigger symbol. I think you copied and pasted the segment of code in the website of Inria. Please check and rewrite the special symbols. I tested it and it works well.

挖个坑埋了你 2024-09-06 18:06:23

不需要定义头部和尾部。它们是从您提供的“列表”中匹配的。

Head and tail need not to be defined. They are matched from 'list' you give.

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