如何制作互动节目?

Gut*_*ore 2 ocaml functional-programming interactive input

我正在学习Ocaml,我需要创建一个可以通过以下方式与用户交互的程序:

Program: "Welcome!"
User: command1 arg1 arg2
program: "The answer is..."
User: command2 arg
program: "The answer is..."
User: exit
Run Code Online (Sandbox Code Playgroud)

我需要一个循环方案来制作类似的东西

Jef*_*eld 5

这是一个循环,它将读取输入行直到它到达文件末尾,或者看到一行显示"退出".

let rec loop () =
    match read_line () with
    | "exit" -> ()
    | s -> Printf.printf "I saw %s\n%!" s; loop ()
    | exception End_of_file -> ()
Run Code Online (Sandbox Code Playgroud)

要在源文件中调用此循环,这样的东西将起作用:

let () = loop ()
Run Code Online (Sandbox Code Playgroud)

要在顶层(OCaml REPL)中试用它:

# loop ();;
Run Code Online (Sandbox Code Playgroud)