프로그래밍/OCaml 26

(최대공약수 구하기) while... 반복문 예제 for OCaml

소스 파일명: testWhile.ml (* * Filename: testWhile.ml * * Purpose: Example using the while loop syntax * while .... * * Execute: ocaml testWhile.ml -200 300 * * Or * * Compile: ocamlc -o testWhile.exe testWhile.ml * Execute: testWhile -200 300 *) (* 사용법 표시 *) let printUsage() = Printf.printf "Using: testWhile.py [integer1] [integer2]\n"; Printf.printf "This finds the greatest common divisor of the gi..

구구단 출력 예제 for OCaml

OCaml 언어는 함수형 언어이면서도 명령형 언어의 특징을 대부분 가지고 잇다. 따라서 보통의 for 구문을 쓸 수도 있고, 재귀호출 함수릉 만들어 쓸 수도 있다. 아래의 소스에서 함수 printDan 은 보통의 for 구문을 쓴 것이고 함수 printAnotherDan 은 재귀호출 함수를 쓴 것이다. 소스 파일명: forTest.ml ------------------------------[소스 시작] (* Filename: forTest.ml Execute: ocaml forTest.ml Or Compile: ocamlc -o forTest forTest.ml Execute: ./forTest Or Compile: ocamlc -o forTest.exe forTest.ml Execute: forTest..

Hello 예제 2 for OCaml

컨솔에 문자 출력하는 ocaml 구문은 Printf.printf "문자열(스트링)" 이다. 여기서 개행문자 "\n"을 추가하면 개행된다. (Jython의 문자 출력 구문도 위와 같다.) 소스 파일명: hello.ml ------------------------------[소스 시작] Printf.printf "%s, %s" "Hello" "world!\n";; let name = "개똥이";; Printf.printf "%s, %s!\n" "Hello" name;; let name2 = ref "홍길동";; let greeting = "안녕하세요?";; name2 := "길동이";; Printf.printf "%s, %s씨!\n" greeting !name2;; -----------------------..

Hello 예제 for OCaml

소스파일명: hello.ml open Printf;; print_string "Hello, 안녕하세요?";; print_endline "";; let x = 23 in let y = 7 in printf "x의 값은 %d이고 y의 값은 %d입니다." x y; print_endline ""; printf "따라서 그 곱은 x * y = %d * %d = %d 입니다.\n" x y (x*y);; 컴파일하기: ocamlc -o hello.exe hello.ml 실행 명령> hello 실행 결과: Hello, 안녕하세요? x의 값은 23이고 y의 값은 7입니다. 따라서 그 곱은 x * y = 23 * 7 = 161 입니다. 컴파일 없이 실행하기: ocaml hello.ml 실행 결과: Hello, 안녕하세요? ..