I've written this mergesort implementation, which works fine if I put the divide function outside of the mergesort function. But when I try to make divide an inner function of mergesort I encounter a syntax error.
I know, there must be some really simple explanation for this. I've looked all over the internet, yet found nothing.
Here is the code:
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 []) []
;;
and it results into:
Characters 567-570:
and rec divide list list_of_lists = (
^^^
Error: Syntax error
reckeyword that the error is telling you to?andwill repeat the previous definition, which is in this caselet rec, so you’re effectively writinglet rec rec- Nick Zuber