gpt4 book ai didi

syntax-error - OCaml : and keyword syntax error

转载 作者:行者123 更新时间:2023-12-03 08:05:12 26 4
gpt4 key购买 nike

我已经编写了这个mergesort实现,如果将除法功能放在mergesort函数之外,效果很好。但是,当我尝试使除法成为mergesort的内部函数时,遇到语法错误。

我知道,对此必须有一些非常简单的解释。我看了遍整个互联网,却一无所获。

这是代码:

let mergesort list = 
let rec sort lists acc = (
let rec merge sublist1 sublist2 merged_list =
match sublist1 with
|[] -> merged_list @ sublist2
|hd1 :: tl1 ->
match sublist2 with
|[] -> merged_list @ sublist1
|hd2 :: tl2 ->
if hd1 < hd2 then merge tl1 sublist2 (merged_list @ hd1::[])
else merge sublist1 tl2 (merged_list @ hd2::[])
in match lists with
|[] ->
(match acc with
|[] -> []
|hd :: [] -> hd
|_ -> sort acc [])
|hd :: tl -> sort (List.tl tl) ((merge (List.hd tl) hd [])::acc)
)
and rec divide list list_of_lists = (
match list with
[] -> list_of_lists
|hd :: tl -> divide tl ((hd :: []) :: list_of_lists)
)
in sort (divide list []) []
;;

结果是:
Characters 567-570:
and rec divide list list_of_lists = (
^^^
Error: Syntax error

最佳答案

您只需从那里的定义中删除rec关键字。

这是因为当您使用and关键字时,实际上是在语法上重复前面的定义,在本例中为let rec

因此,您当前的实现实际上与说let rec rec相同

关于syntax-error - OCaml : and keyword syntax error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47143535/

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