프로그래밍/OCaml
if...else... 조건문 사용 예제 for OCaml
Scripter
2013. 1. 26. 18:07
소스 파일명: testIf.ml
- (*
- Filename: testIf.ml
- Execute: ocaml testIf.ml -8e-2
- Or
- Compile: ocamlc -o testIf.ml
- Execute: ./testIf -8e-2
- Or
- Compile: ocamlc -o testIf.exe testIf.ml
- Execute: testIf -8e-2
- Date: 2013. 1. 26.
- Copyright (c) pkim _AT_ scripts.pe.kr
- *)
- let printUsing() =
- Printf.printf "Using: testIf [number]\n";
- Printf.printf " or ./testIf [number]\n";
- Printf.printf " or ocaml testIf.ml [number]\n";
- Printf.printf "This determines whether the number is positive or not.\n";;
- let isPositve = function
- | x when x > 0.0 -> "positive";
- | x when x < 0.0 -> "negative" ;
- | x when x = 0.0 -> "zero" ;
- | _ -> "not a floating number";;
- let main () =
- print_int(Array.length Sys.argv);;
- if ((Array.length Sys.argv) < 2) then
- printUsing()
- else
- let arg = float_of_string Sys.argv.(1) in
- let msg = isPositve(arg) in
- Printf.printf "%g is %s." arg msg;
- print_newline();;
- exit 0;;
- 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.