在 OCaml 中对列表进行排序
以下是对任何给定列表进行排序的代码:
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
任何人都可以帮我理解这里的问题吗?我没有发现 head
或 tail
在任何地方或代码中预定义
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的代码似乎是正确的,并且可以为我编译:
Your code seems correct, and compiles for me:
添加到 Pascal 所说的内容中,列表类型定义为:
这就是您要匹配列表
lst
的内容。Adding to what Pascal said, the list type is defined as:
and that's what you are matching your list
lst
against.符号“|”是横线符号,不是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.
不需要定义头部和尾部。它们是从您提供的“列表”中匹配的。
Head and tail need not to be defined. They are matched from 'list' you give.