On Sat, 30 Sep 2000, Pierre Weis wrote:
> Once more I would prefer a more general recursive function, that would
> loop for any (even non looping) function. For instance:
>
> # let rec bottom f = f (); bottom f;;
> val bottom : (unit -> 'a) -> 'b = <fun>
>
> and I consider the 'b in the type of bottom as clearly indicating the
> looping for ever nature of any application of the bottom function.
The name "bottom" might be a bit confusing in this context, because "f"
could be a function with observable side effects (e.g. I/O), whereas
"bottom" is usually used to indicate non-termination only, which cannot be
observed.
But this function (with a different name) and others would come handy more
often than once, e.g.:
let rec forever f = f (); forever f
let try_forever e f = try forever f with exn -> if e <> exn then raise exn
This would allow:
let _ =
try_forever End_of_file (fun () -> print_endline (read_line ()));
print_endline "Done!"
It is much more tedious (and error-prone if one forgets the "begin ...
end") to write the following:
let _ =
begin
try while true do print_endline (read_line ()) done
with End_of_file -> ()
end;
print_endline "Done!"
Maybe functions similar to "forever" and "try_forever" could make it into
the standard library? Such loops come up quite often with e.g. servers or
I/O-loops in general.
Best regards,
Markus Mottl
--
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl