OCaml 맛보기

OCaml 맛보기

Haskell을 배우다 보니 OCaml과 비교하는 사람들이 꽤 있네요. OCaml은 어떤 언어인지 궁금해졌습니다. Linux 개발환경에 빨리 설치해보고 어떻게 동작하는지 알아보려 명령어를 남깁니다.

Install OCaml by OCaml Package Manager(opam)

Linux에 opam이라는 패키지 관리자를 설치하고 간단하게 사용하는 방법입니다. opam을 설치하면 OCaml이 자동으로 설치됩니다.

## ** Get binary distribution **
$ bash -c "sh <(curl -fsSL https://raw.githubusercontent.com/ocaml/opam/master/shell/install.sh)"
## ** Initialize **
$ opam init            # Initialize ~/.opam
## ** Lookup **
$ opam list -a         # List all available packages
$ opam search QUERY    # List packages with QUERY in their name or description
$ opam show PACKAGE    # Display information about PACKAGE
## ** Install **
$ opam install PACKAGE # Download, build and install the latest version of PACKAGE
                     # and all its dependencies
$ opam remove PACKAGE  # Uninstall the given package
## ** Upgrade **
$ opam update          # Update the packages database
$ opam upgrade         # Bring everything to the latest version possible
# ** More **
opam CMD --help      # Command-specific manpage

The OCaml top level (a.k.a REPL)

Haskell, Python 같은 언어에서는 REPL(read-eval-print loop)이라고 하는데 OCaml은 Top level 이라고 합니다.

$ ocaml
        OCaml version 4.13.1
# 1 + 2 * 3;;
- : int = 7
# exit 0;;
$

Install the Dune build system for OCaml

OCaml은 Dune 이라는 빌드 시스템을 갖고 있어요. Haskell 에서 cabal과 stack, Rust에서 cargo 와 같다고 생각하면 되겠습니다.

$ opam install dune
The following actions will be performed:
  ∗ install dune 2.9.3
<><> Processing actions <><><><><><><><><><><><><><><><><><><><><><><><><><><><>
⬇ retrieved dune.2.9.3  (https://opam.ocaml.org/cache)
∗ installed dune.2.9.3
Done.
$

A first project

아주 간단한 helloworld라는 프로젝트를 만들고 결과를 실행해봅니다.

$ mkdir helloworld
$ cd helloworld/
$ dune init exe helloworld
Success: initialized executable component named helloworld
$ dune build
Info: Creating file dune-project with this contents:
| (lang dune 2.9)
$ dune exec ./helloworld.exe
Hello, World!

내 개발환경에 직접 실행해보면 새로운 기술과 벌써 친해진 듯합니다. 이 뒤로 OCaml을 자주 만나길 기대해봅니다.

References