Micheal Ross
Micheal Ross

Reputation: 211

Ocaml syntax quite weird

I have this program in Ocaml that reads a line from keyboard and returns an int :

let get_int ()  =
print_string "Insert a number\n" ;
let input =  read_line() in
let return__ = int_of_string( input )
;;

print_string "I'll print what you write : ";
print_int ( get_int() );
print_string "\n"

The problem is a syntax error on line 5, ";;" said the compiler.

I know that already exist functions that do this but I'm doing this to learn.

I read the official Ocaml documentation but I still don't get the syntax. Someone that could explain me something about it?

Upvotes: 1

Views: 170

Answers (1)

Splingush
Splingush

Reputation: 246

Your get_int-binding has to end in an expression. You can get rid of the last let-binding there and return the int directly:

let get_int () =
  print_string "Insert a number\n";
  let input = read_line () in
  int_of_string input;;

Upvotes: 1

Related Questions