如何制作互动节目? [英] How to make an interactive program?

查看:111
本文介绍了如何制作互动节目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm learning Ocaml and I need to create a program that can interact with the user in the following way:

Program: "Welcome!"
User: command1 arg1 arg2
program: "The answer is..."
User: command2 arg
program: "The answer is..."
User: exit

我需要一个循环的方案来制作类似的内容

I need a scheme of the loop that make something like that

推荐答案

这是一个循环,它将读取输入行直到文件结束,或者看到一行说退出的行。
$ b

Here's a loop that will read lines of input until it reaches end of file, or sees a line that says "exit".

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

为了在源文件中调用这个循环,类似这样的工作将会起作用:

To call this loop in a source file, something like this will work:

let () = loop ()

要在顶层(OCaml REPL)尝试它:

To try it out in the toplevel (OCaml REPL):

# loop ();;

这篇关于如何制作互动节目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆