Hello,
I’m learning OCaml. I’m following recommanded book « Ocaml from the very beginning ».
In the beginning of the chapter #5 (sorting things) i see:
let rec sort l =
match l with
[] -> []
| h::t -> insert h (sort t) (* why sort t ? Will it call sort until no elements left ? *)
let rec insert x l =
match l with
[] -> [x]
| h::t ->
if x <= h
then x :: h :: t
else h :: insert x t
It raises many questions for me…
I’ve tried in the sort function to call insert like this: insert h t and it works too.
I was thinking that the original call insert h (sort h) will sort also the existing list, but that’s not the case. Unordered list passed as parameter stays unordered:
insert 10 [3;8;99;5;18];;
: int list = [3; 8; 10; 99; 5; 18]
(called with original insert h (sort h))
And why can I call a two parameters function without parenthesis ? I was expecting to call like this: insert (h t)
Thank you for your help.