How to make an interactive program?

后端 未结 1 1059
太阳男子
太阳男子 2021-01-22 13:58

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:          


        
相关标签:
1条回答
  • 2021-01-22 14:33

    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 ()
    

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

    # loop ();;
    
    0 讨论(0)
提交回复
热议问题