Skip to content

Instantly share code, notes, and snippets.

@kenmori
Last active May 30, 2018 05:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenmori/6f97ce63a461796ad259c192ee0fc0fc to your computer and use it in GitHub Desktop.
Save kenmori/6f97ce63a461796ad259c192ee0fc0fc to your computer and use it in GitHub Desktop.
OCaml初心者が入門する

OCaml初心者が入門する

http://kenjimorita.jp/wp-content/uploads/2018/05/C637C73D-4DD3-4B6B-9A80-2EB66A94204B.jpeg

随時更新

最新更新日 2018/5/30

install

brew install ocaml
brew install opam (パッケージマネージャー)
brew install rlwrap (Ocmalインタプリタで履歴が打てる)

rlwrapを使用してocamlを立ち上げる

rlwrap ocaml  

rlwrap ・・・OCamlインタプリタシェルで 最後のコマンドを繰り返したい場合に使うもの。履歴を辿れる。ocamlはそれ自体これらをサポートしていないためinstallすると良い。

コメント

(* something *)

(* This is a
 * multi-line
 * comment.
 *)
 

行の終わり

;;

終了

exit 0;;
//or
#quit;;

コンパイル

$ mkdir my_project
$ cd my_project
$ echo 'let () = print_endline "Hello, World!"' > my_prog.ml

To compile an OCaml program named my_prog.ml to a native executable, use ocamlbuild my_prog.native

my_prog.ml というOCamlプログラムをネイティブ実行可能にコンパイルするには ocamlbuild my_prog.native と打ちます

OCamlのgitignore

*.annot
*.cmo
*.cma
*.cmi
*.a
*.o
*.cmx
*.cmxs
*.cmxa

# ocamlbuild working directory
_build/

# ocamlbuild targets
*.byte
*.native

# oasis generated files
setup.data
setup.log

# Merlin configuring file for Vim and Emacs
.merlin

module

あるファイルの関数をmoduleとして使う

//amodule.ml
let hello () = print_endline "Hello, World!"

//main.ml
Amodule.hello ()

amodule.mlのhelloを使うの意味。最初の文字がincludeするファイル名の大文字であることに注意

型変換

(* 型変換*)
let i = 1;;
let f = 2.0;;
float i +. f;;

//or

(float_of_int i) +. f;;
//float_of_intは int型をfloatにして返す関数

再帰

let rec とすることで自身から呼ぶことができる。 recをつけないとrangeが呼ばれたとき前もって定義されているものとしてrangを探しに行ってしまう

# let rec range a b =
  if a > b then []
  else a :: range (a+1) b;;
val range : int -> int -> int list = <fun>

上記の val range : int -> int -> int list = <fun> は OCamlがどう推論したのかが表示されている

多相関数

多相関数・・・引数が何でも良いような関数。

下記は引数xの型はなんでもいいが必ず3を返すgive_me_a_three関数の型

# let give_me_a_three x = 3;;
val give_me_a_three : 'a -> int = <fun>

「なんでもいい型」は単引用符(`)に任意の文字(a)で表現される

local変数(実際はlocalな式)の定義

# let average a b =
   let sum = a +. b in
   sum /. 2.0;;
val average : float -> float -> float = <fun>

上記

let name = expression inの構文箇所は ローカルな式に名前をつけている。ただのエイリアス。

sumは a +.b の値を代入しているわけではない。

これでa +.b  のexpressionを後続の実行でsumとして使うことができる。

「実際はlocalな式」という表現はそのため。

sumは代入もできないし値を変えることもできない。


Question

ocamlcocamloptてなあに??

ocamlc・・・バイトコードにコンパイルする
ocamlopt・・・ネイティブコードにコンパイルする
どちらを使っていいかわからない場合ocamloptでok

ocamlopt -o progprog module1.ml module2.ml は何をしている?

-o exec-file ・・・ 出力するファイル名を明示して実行ファイルを生成するオプション。 -c ならコンパイルのみ
progprog ・・・ ファイル名
module1.ml module2.ml ・・・依存する順番を指定して渡している。渡す順番を間違えてはいけない(module1はmodule2に依存してはいけない)

ocamlbuild my_prog.nativeしたらこれらが生成されましたけど?これらはなあに??

_digests
_log
my_prog.cmi
my_prog.cmo
my_prog.cmx
my_prog.ml
my_prog.ml.depends
my_prog.native*
my_prog.o

WIP

WIP

参照

OCaml.gitignore

Errorメッセージ集

日本語チュートリアル

コンパイルオプション

https://ocaml.org/learn/tutorials/modules.ja.html

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