gpt4 book ai didi

ocaml - 在 OCaml 中对列表进行排序

转载 作者:行者123 更新时间:2023-12-01 01:34:33 25 4
gpt4 key购买 nike

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

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;;

[来源: Code

但是,我收到一个未绑定(bind)错误:
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在任何地方或代码中预定义

最佳答案

您的代码似乎正确,并为我编译:

    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]

关于ocaml - 在 OCaml 中对列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2756489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com