소스 파일명: testIf.ml

  1. (*
  2.    Filename: testIf.ml
  3.    Execute: ocaml testIf.ml -8e-2
  4.      Or
  5.    Compile: ocamlc -o testIf.ml
  6.    Execute: ./testIf -8e-2
  7.      Or
  8.    Compile: ocamlc -o testIf.exe testIf.ml
  9.    Execute: testIf -8e-2
  10.    Date: 2013. 1. 26.
  11.    Copyright (c) pkim _AT_ scripts.pe.kr
  12. *)
  13. let printUsing() =
  14.      Printf.printf "Using: testIf [number]\n";
  15.      Printf.printf "   or  ./testIf [number]\n";
  16.      Printf.printf "   or  ocaml testIf.ml [number]\n";
  17.      Printf.printf "This determines whether the number is positive or not.\n";;
  18. let isPositve = function
  19.      | x when x > 0.0 -> "positive";
  20.      | x when x < 0.0 -> "negative" ;
  21.      | x when x = 0.0 -> "zero" ;
  22.      | _ -> "not a floating number";;
  23. let main () =
  24.     print_int(Array.length Sys.argv);;
  25.     if ((Array.length Sys.argv) < 2) then
  26.         printUsing()
  27.     else
  28.         let arg = float_of_string Sys.argv.(1) in
  29.         let msg = isPositve(arg) in
  30.         Printf.printf "%g is %s." arg msg;
  31.         print_newline();;
  32.     exit 0;;
  33. main ();;



컴파일> ocamlc -o testIf.exe testIf.ml

실행> testIf -1.2
-1.2 is negative.


실행> testIf 0.0
0 is zero.


실행> testIf -0.1e-127
-1e-128 is negative.


Posted by Scripter
,