type state = Out Enter WExit In let strmfold f = (* fold f on a character stream starting with v *) let rec loop v = (* both normal and exception branch return function *) (try let c = input_char stdin in fun () -> loop (f v c) with End_of_file -> fun () -> v) () (* called outside try => tail recursive *) in loop let delta = function Out -> (function '/' -> Enter _ -> Out) Enter -> (function '*' -> In '/' -> Enter _ -> Out) WExit -> (function '*' -> WExit '/' -> Out _ -> In) In -> (function '*' -> WExit _ -> In) let _ = print_endline (if strmfold delta Out = Out then "OK" else "ends in comment")
This document was generated using caml2html