I was trying to understand what the following code does:
let rec fold_left f a list =
match list with
| [] -> a
| x::xs -> fold_left f (f a x) xs;;
when called with:
fold_left
(fun () -> print_string)
()
["hi"; "there\n"];;
according to me the first recursive call should call:
fold_left print_string (print_string () "hi") ["there\n"])
however when I try calling that I get this error:
# print_string () "hi";;
Error: This function has type string -> unit
It is applied to too many arguments; maybe you forgot a `;'.
what am I misunderstanding?
This still doesn’t work:
# print_string ();;
Error: This expression has type unit but an expression was expected of type
string
So how is the recursion I pasted at the top working then? It does work here:
# fold_left (fun () -> print_string) () ["hi"; "there\n"];;
hithere
- : unit = ()
why?