zoukankan      html  css  js  c++  java
  • 【转】OCaml学习笔记

    原文地址: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才能退出
  • 相关阅读:
    jQuery对象初始化的多种传参数形式
    Ajax核心技术之XMLHttpRequest
    【转载】神奇的css属性pointer-events
    JavaScript学习笔记——错误处理
    jquery validate的漂亮css样式验证
    JavaScript Math和Number对象研究
    《JavaScript语言精粹》笔记
    HTML5版的String Avoider小游戏
    IE11 F12工具报错
    css之图片羽化处理
  • 原文地址:https://www.cnblogs.com/njucslzh/p/2032444.html
Copyright © 2011-2022 走看看