Domanda

Could you please show me how to get user's input in Ocaml-top?

It can be found in this link: http://www.typerex.org/ocaml-top.html

I'm a beginner in Ocaml and I'm trying to get the user's input to run this example

This is the code:

let rec hilo n =
     let () = print_string "type a number: " in
     let i = read_int () 
     in 
       if i = n then 
         let () = print_string "BRAVO" in
         let () = print_newline ()
         in print_newline ()
       else 
         let () = 
           if i < n then 
             let () = print_string "Higher"
             in print_newline () 
           else 
             let () = print_string "Lower"
             in print_newline ()
         in hilo n ;;

But the program stop at Type a number

È stato utile?

Soluzione

This is not really possible. OCaml-top sends its commands to the ocaml interpreter through the standard input, and read_int reads inputs on stdin also. This problem is tracked at https://github.com/OCamlPro/ocaml-top/issues/45

Altri suggerimenti

Try to run it from command prompt. Type this command in the command prompt: ocaml filename.ml You can use it only if you have set the path in environment variables.

I have this working and the diff with the file above is zero

$ ocaml

starts the interpreter

# #directory "/home/wherever-your-OCaml-stuff-is";;

yes, type the extra # and the ;;

# #use "hilo.ml";;

no need to compile anything

# hilo 24356256456;;

it is not much of a guessing game...

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top