OCaml 소개
| Author: |
서상현 |
| Date: |
2008-03-03 |
몸 풀기
$ ocaml
# 2 + 2;;
- : int = 4
# print_endline "Hello, world!";;
Hello, world!
# #quit;;
파일 실행하기
$ cat > hello.ml
print_endline "Hello, world!";;
$ ocaml hello.ml
Hello, world!
$ ocaml
# #use "hello.ml";;
Hello, world!
컴파일하기
$ ocamlc -o hello hello.ml
$ file ./hello
./hello: a /usr/bin/ocamlrun script text executable
$ ./hello
Hello, world!
$ ocamlopt -o hello.opt hello.ml
$ file ./hello.opt
./hello.opt: ELF 32-bit LSB executable, Intel 80386...
$ ./hello.opt
Hello, world!
기본 자료형
- bool: true, false
- int: 0, 1, 2
- float: 0.1, 1.0, 1e1
- char: 'a', 'b', 'c'
- string: "SPARCS"
선언, 문자열, 배열
# let i = 0;;
# i;;
- : int = 0
# let s = "SPARCS";;
# s.[0];;
- : char = 'S'
# let a = [|0; 0; 0|];;
# a.(0) <- 1;;
# a.(0);;
- : int = 1
레코드
# type date = {year: int; month: int; day: int};;
# let today = {year=2008; month=3; day=3};;
# today.year;;
- : int = 2008
# type counter = {mutable count: int};;
# let c = {count=0};;
# c.count <- c.count + 1;;
# c.count;;
- : int = 1
레퍼런스
# let c = ref 0;;
# !c;;
- : int = 0
# c := 10;;
# !c;;
- : int = 10