LoginSignup
1
0

More than 5 years have passed since last update.

MacでOCamlを使ってみたときのメモ

Posted at

環境

  • Mac 10.10.3
  • OCaml 4.02.2
  • Emacs

MacにOCamlをインストール

$ brew install ocaml
$ ocaml -version

とりあえず動かしてみる

Hello World

$ ocaml
        OCaml version 4.02.2

# let () = Printf.printf "Hello, world!\n";;
Hello, world!
# ^D
$ # Control + D で抜けることができる

日本語をPrintすると文字化けするので直す

$ ocaml
        OCaml version 4.02.2

# let () = Printf.printf "はろーわーるど\n";;
(なんか文字化けする)

以下のような ~/.ocamlinit を用意したらなおる

$ cat ~/.ocamlinit
let printer ppf = Format.fprintf ppf "\"%s\"";;
#install_printer printer;;

クラスを定義してみる

ここからはインタプリタじゃないやつでやる

$ cat script.ml
class slack =
  object (self)
  method print =  Printf.printf "slack class\n"
end;;

let slack = new slack;;
slack#print;;

$ ocaml script.ml
slack class

.ml が拡張子になる。 ocaml <ファイル名> でそのファイルのプログラムが実行される。

OCamlはオブジェクト指向 + 関数型言語なのでクラスが定義できる。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0