Skip to content

Instantly share code, notes, and snippets.

@voluntas
Last active December 31, 2023 05:11
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voluntas/7bf4badea43d6120886d1c16341847b1 to your computer and use it in GitHub Desktop.
Save voluntas/7bf4badea43d6120886d1c16341847b1 to your computer and use it in GitHub Desktop.
OCaml コトハジメ

OCaml コトハジメ

日時

2016-11-06

@voluntas

バージョン

0.0.0

URL

https://voluntas.githu.io/

突っ込みは Twitter @voluntas まで。

概要

自社の CTO と技術顧問 が OCaml ハカーなのに、覚えないのは損というのが動機。

普段 Erlang/OTP しかほとんど書いていないので、ここいらでいっちょ覚えてみるかと。

目標としてはまずはコマンドライン系のツール、あとは小さなネットワーク・サーバという感じ。

本番導入はあまり考えていない、ちょっとしたツールを作るくらいに使えるようになると良い。 Python くらい手に馴染んでくれればありがたいところ。

参考書

とりあえず二冊を紙で購入した。

セットアップ

全ては opam から始まるということで、 opam をインストール。開発環境は Mac です。

opam

まずは opam をいれる、そこからスタート

https://opam.ocaml.org/doc/Install.html#OSX

MacPorts で opam をインストール:

$ sudo port install opam

ocaml

OCaml は opam 経由で入れられる

間違って 4.02.3 を入れてしまった:

$ opam init --comp 4.02.3

最新版の 4.03.0 に切り替える:

$ opam switch 4.03.0

vim

あとで

opam install

https://github.com/realworldocaml/book/wiki/Installation-Instructions

utop と core をいれる

https://github.com/diml/utop

utop は REPL ということなのでいれておく、また core というライブラリを使うらしいので、とりあえずいれておく。

$ opam install core utop

.ocamlinit ファイルに以下を追加しろと書いてあるので追加する。

https://github.com/realworldocaml/book/wiki/Installation-Instructions#setting-up-and-using-utop

~/.ocamlinit:

#use "topfind";;
#thread;;
#camlp4o;;
#require "core.top";;
#require "core.syntax";;

はじめの一歩

https://realworldocaml.org/v1/en/html/a-guided-tour.html

これは動かない

# let log_entry maybe_time message =
    let time =
        match maybe_time with
        | Some x -> x
        | None -> Time.now ()
        in
        Time.to_sec_string time ^ " -- " ^ message
    ;;
    Error: This expression has type zone:Core.Zone.t -> string
        but an expression was expected of type string 

Core Time.to_sec_string の引数が変わったらしい

val to_sec_string : t -> string (* before *)
val to_sec_string : t -> zone:Zone.t -> string (* after *)
# let log_entry maybe_time message =
    let time =
      match maybe_time with
      | Some x -> x
      | None -> Time.now ()
    in
    Time.to_sec_string time ~zone:Core.Zone.local ^ " -- " ^ message
  ;;
  val log_entry : Time.t option -> string -> string = <fun>

参考

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment