文章目錄
- ocaml -- the toplevel system
原文地址:http://hi.baidu.com/cg51/blog/item/19035643e8f9fe109313c623.html
定義函數:以下方法等同
# let f=fun x y->x+y;;
val f : int -> int -> int = <fun>
# let e ab =
ab*ab;;
val e : int -> int = <fun>
# let f=function x->function y->x+y;;
val f : int -> int -> int = <fun>
使用函數 # let xx x=x*x;;
val xx : int -> int = <fun>
# xx 10;;
- : int = 100
具名引數
# let f ~x:a ~y:b ~z:c = a + b + c;;
val f : x:int -> y:int -> z:int -> int = <fun>
# let y a b c=a+b+c;;
val y : int -> int -> int -> int = <fun>
定義:
let f ~x:a ~y:b ~z:c = a + b + c;;
<=> let f ~x:x ~y:y ~z:z = x + y + z;;
<=> let f ~x ~y ~z = x + y + z;;
使用:
# f 1 2 3;;
- : int = 6 固定傳參順序
<=> f ~x:1 ~y:2 ~z:3;;
<=> f ~y:2 ~x:1 ~z:3;; 這樣多方便!
<=># let x = 1 and y = 2 and z = 3 in f ~x ~y ~z;;
- : int = 6
選擇性參數可以用來設定預設值、簡化調用等。選擇性參數必須是具名引數。
定義
# let f ?x:(x=3) y = x + y;;
val f : ?x:int -> int -> int = <fun>
# f 4;;
- : int = 7
# f ~x:10 8;;
- : int = 18
ocaml tools用法ocaml -- the toplevel system
幾個常用命令(注意這個#是輸入的,不是那個#提示符):
- #use "x.ml";; 載入並編譯一個指令碼。即編寫即測試,方便極了!
- #load "x.cmo";; 載入一個bytecode模組
- #quit;; 不用^D也能退出:-)
ocaml -- the toplevel system
幾個常用命令(注意這個#是輸入的,不是那個#提示符):
- #use "x.ml";; 載入並編譯一個指令碼。即編寫即測試,方便極了!
#load "x.cmo";; 載入一個bytecode模組
#quit;; 不用^D也能退出:-)
ocamlc -- the bytecode compiler
基本用法:
- ocamlc x.ml -o x.out 編譯一個.ml代碼並產生可執行檔bytecode檔案x.out
ocamlc -c x.ml 編譯一個.ml代碼並產生.cmo目標代碼檔案(bytecode)和.cmi介面檔案
多檔案聯編/模組編寫
ocamlc -a ... (參見3) (TODO)
ocamlrun -- the runtime system
運行ocamlc編譯出來的bytecode的。如果ocamlc好比javac,那麼ocamlrun就好比java(jre)
- 一般直接運行程式就好了。不行的時候就:
- ocamlrun x.out
- 最強的就是java炒作的“一次編譯到處運行”了,在OSX上ocamlc,然後在windows上run,沒有問題:-)
- ocamlopt -- the native code compiler
TODO
- ocamlprof -- the profiling tool
ocamldebug -- the debugger
ocamllex and ocamlyacc -- lexer and LALR(1) grammar analysis tools
ocamllex ocamlyacc (TODO)
- ocamldoc -- the documentation generator
ocamldep -- the dependency generator
OCaml中的各種檔案尾碼
ml
mli
cmi
cmo
cmx
cma
cmxa
a
o
so
dll
四種運行方式
互動介面(ocaml)
$ ocaml
Objective Caml version 3.08.2
# print_string "hello world";;
hello world- : unit = ()
#
- 解釋執行(ocaml xxx.ml)
$ ocaml helloworld.ml
hello world
- 編譯執行(byte-code)(ocamlc -o xxx xxx.ml)
$ ocamlc -o helloworld helloworld.ml
$ ./helloworld
hello world
head一下編譯出來的helloworld看看,其首行是
#!/usr/local/bin/ocamlrun
也就是說,使用ocamlrun來解釋byte-code
來來來,在OSX上編譯,然後去Windows下面ocamlrun helloworld。成功!強啊!
- 編譯執行(native-code)(ocamlopt -o xxx xxx.ml)
$ ocamlopt -o helloworld helloworld.ml
$ ./helloworld
hello world
- 有用的附加模組
單獨拿出來放在這裡了
- Emacs Mode for O'Caml
Main key bindings:
TAB indent current line
M-C-q indent phrase
M-C-h mark phrase
C-c C-a switch between interface and implementation
C-c C-c compile (usually make)
C-x` goto next error (also mouse button 2 in the compilation log)
- 如何設定eshell的PATH?
- (add-hook 'eshell-mode-hook
'(lambda nil
(eshell/export "EPOCROOT=\\Paragon\\")
(let ((path))
(setq path ".;c:/program files/microsoft visual studio/vb98/")
(setq path (concat path ";c:/programs/perl/bin"))
(setenv "PATH" path))
(local-set-key "\C-u" 'eshell-kill-input))
)
- 在eshell裡退出ocaml要用C-q C-d RET輸入^D才能退出