Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exit a program?

Tags:

ocaml

If the input argument are not as expected I want to exit the program. How should I achieve that? Below is my attempt.

let () =
  if ((Array.length Sys.argv) - 1) <> 2 then                                                                                                                                              
    exit 0 ; ()                                                                                                                                                                           
  else
    ()

Thanks.

like image 368
UnSat Avatar asked Feb 10 '23 21:02

UnSat


1 Answers

exit n is a right way to exit program, but your code has a syntax error. if ... then exit 0; () is parsed as (if ... then exit 0); (). Therefore you got a syntax error around else, since it is not correctly paired with then.

You should write:

let () =
  if ((Array.length Sys.argv) - 1) <> 2 then begin                                                                                                                                           
    exit 0 ; ()                                                                                                                                                                           
  end else
    ()

or simply,

let () = if Array.length Sys.argv - 1 <> 2 then exit 0
like image 152
camlspotter Avatar answered Mar 05 '23 01:03

camlspotter