Extlib OCaml 에 붙여 쓰는 여러 가지 (써드 파티) 라이브러리 중의 하나이며, OCaml 의 표준 라이브러리가 지원하지 않는 일들(예를 들면 UTF8 문자 지원)을 한다.

현재 최신 버전인 Extlib 1.5.3 은 OCaml 4.00.0 과 4.00.1 용 라이브러리이고, Extlib 1.5.1, 1.5.2 는 OCaml 3.12.0, 3.12.1 용 라이브러리이다

64비트 윈도우의 경우 Extlib 가 전부 설치되지 못하고, 일부(마이트 코드영 라이브러리, 설치 과정 중 1번 선택)만 설치되는 현상이 있다.

설치하는 명령은 (Extlib 의 압축이 해제된 폴더에서)

    Prompt> ocaml install.ml

이다. 설치 과정 중에  모두(3번)를 선택하여 설치가 실패할 경우에는 1번(바이트 코드)를 선택한다. 설치 후 필요한 것은 라이브러리 파일 extLib,cma 이다. 이 라이브러리 파일이 OCaml 3.12.0 과 3.12.1 의 경우에는 %OCAML설치폴더% 에 생성되지만, OCaml 4.00.0 과 4.00.1 의 경우에는  %OCAML설치폴더%\lib\site-lib\extlib 폴더에 생성된다.

Extlib 의 설치가 성공적으로 끝났으면, 다음 간단한 파일을 작성하고 실행해 보자.

 

  * 파일명: helloUTF8.ml (파일 저장 시에 UTF8 인코딩로 저장)

open Printf
open UTF8
(* open UChar *)

let () = Printf.printf "Hello, world!\n"
let () = Printf.printf "안녕하세요?\n"

let str = "안녕하세요?1234"
let () = Printf.printf "length str = %d\n" (length str)
let () = Printf.printf "nth str 5 = %c\n" (UChar.char_of (get str 5))
let () = Printf.printf "nth str 5 = %c\n" (UChar.char_of (get str 6))
let () = Printf.printf "nth str 5 = %c\n" (UChar.char_of (get str 7))
let () = Printf.printf "nth str 5 = %c\n" (UChar.char_of (get str 8))

 

ocaml 이나 ocamlc 를 실행할 때는 항상 환경변수 PATH 와 OCAMLLIB 가 알맞게 맞추어져 있는지 확인한다.  ocaml 의 버전을 확인하는 병령은

Prompt> ocaml -version

이다. (참고로 Extlib 가 설치된 직후에는 이 명령이 먹히지 않으므로 새로운 명령창을 열고, PATH 와 OCAMLLIB 를 다시 설정해 준다.)

Prompt> set PATH=C:\OCaml312\bin;%PATH%

Prompt> set OCAMLLIB=C:\OCaml312\lib

 

* 환경변수 OCAMLLIB 의 설정 확인

Prompt> echo %OCAMLLIB%
C:\OCaml312\lib

 

* OCaml 3.12.1 에 설치된 Extlib 를 사용하는 실행 명령

Prompt> ocaml -I c:\ocaml312 extLib.cma helloUTF8.ml

실행 결과:
Hello, world!
안녕하세요?
length str = 10
nth str 5 = ?
nth str 5 = 1
nth str 5 = 2
nth str 5 = 3

* OCaml 4.00.1 에 설치된 Extlib 를 사용하는 실행 명령

Prompt> ocaml -I c:\ocaml400\lib\site-lib\extlib extLib.cma helloUTF8.ml

 

 * 파일명: testDynArray.ml (동적 배열 모튤 DynArray 을 사용하는 OCaml 소스)

(*
 *  Filename: testDynArray.ml
 *
 *     Purpose: Testing the DynArray module of Extlib.
 *
 *   Execute: ocaml -I C:\OCaml312 extLib.cma testDynArray.ml
 *
 *     Or
 *
 *   Compile: ocaml -I C:\OCaml312 extLib.cma -o testDynArray.exe testDynArray.ml
 *   Execute: testDynArray
 *
 *     Or
 *
 *   Execute: ocaml -I C:\OCaml400\lib\site-lib\exlib extLib.cma testDynArray.ml
 *
 *     Date: 2013. 1. 29.
 *   Author: pkim _AT_ scripts.pe.kr
 *)

open Printf
(* open DynArray *)

let darr = DynArray.create()

let () = DynArray.add darr 5
let () = printf "length of darr = %d\n" (DynArray.length darr)
let () = printf "the first element of darr is %d\n" (DynArray.get darr 0)

let () = DynArray.add darr 7
let () = printf "length of darr = %d\n" (DynArray.length darr)
let () = printf "the last element of darr is %d\n" (DynArray.last darr)

let () = DynArray.iter (printf "%d, ") darr
let () = print_newline()

let () = DynArray.iter (printf "%d, ") (DynArray.map (fun x -> x*x) darr)
let () = print_newline()
let () = print_newline()

let () = DynArray.add darr (-1)
let () = printf "length of darr = %d\n" (DynArray.length darr)
let () = printf "the last element of darr is %d\n" (DynArray.last darr)
let () = DynArray.iter (printf "%d, ") darr
let () = print_newline()

let sumIt arr = DynArray.fold_right (+) arr 0

let () = printf "The summmation of elements of the list is %d\n" (sumIt darr)
let () = print_newline()

let () = printf "Inserting -1 at the second position of the list\n"
let () = DynArray.insert darr 1 (-3)
let () = printf "length of darr = %d\n" (DynArray.length darr)
let () = printf "the last element of darr is %d\n" (DynArray.last darr)
let () = DynArray.iter (printf "%d, ") darr
let () = print_newline()
let () = print_newline()

let () = printf "Deleting the third element of the list\n"
let () = DynArray.delete darr 2
let () = printf "length of darr = %d\n" (DynArray.length darr)
let () = printf "the last element of darr is %d\n" (DynArray.last darr)
let () = DynArray.iter (printf "%d, ") darr
let () = print_newline()
let () = printf "The summmation of absolutes of elements of the list is %d\n" (sumIt (DynArray.map abs darr))
let () = print_newline()

(*
Output:
length of darr = 1
the first element of darr is 5
length of darr = 2
the last element of darr is 7
5, 7,
25, 49,

length of darr = 3
the last element of darr is -1
5, 7, -1,
The summmation of elements of the list is 11

Inserting -1 at the second position of the list
length of darr = 4
the last element of darr is -1
5, -3, 7, -1,

Deleting the third element of the list
length of darr = 3
the last element of darr is -1
5, -3, -1,
The summmation of absolutes of elements of the list is 9
*)

 

 

 

Posted by Scripter
,