OCaml學習筆記

來源:互聯網
上載者:User
文章目錄
  • 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

幾個常用命令(注意這個#是輸入的,不是那個#提示符):

  1. #use "x.ml";; 載入並編譯一個指令碼。即編寫即測試,方便極了!
  2. #load "x.cmo";; 載入一個bytecode模組
  3. #quit;; 不用^D也能退出:-)
    ocaml -- the toplevel system
    幾個常用命令(注意這個#是輸入的,不是那個#提示符):
  4. #use "x.ml";; 載入並編譯一個指令碼。即編寫即測試,方便極了!
    #load "x.cmo";; 載入一個bytecode模組
    #quit;; 不用^D也能退出:-)
    ocamlc -- the bytecode compiler
    基本用法:
  5. 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)
  6. 一般直接運行程式就好了。不行的時候就:
  7. ocamlrun x.out
  8. 最強的就是java炒作的“一次編譯到處運行”了,在OSX上ocamlc,然後在windows上run,沒有問題:-)
  9. ocamlopt -- the native code compiler
    TODO
  10. ocamlprof -- the profiling tool
    ocamldebug -- the debugger
    ocamllex and ocamlyacc -- lexer and LALR(1) grammar analysis tools
    ocamllex ocamlyacc (TODO)
  11. 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 = ()
    #
  12. 解釋執行(ocaml xxx.ml)
    $ ocaml helloworld.ml
    hello world
  13. 編譯執行(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。成功!強啊!
  14. 編譯執行(native-code)(ocamlopt -o xxx xxx.ml)
    $ ocamlopt -o helloworld helloworld.ml
    $ ./helloworld
    hello world
  15. 有用的附加模組
    單獨拿出來放在這裡了
  16. 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)
  17. 如何設定eshell的PATH?
  18. (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))
    )
  19. 在eshell裡退出ocaml要用C-q C-d RET輸入^D才能退出

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.