我的Google Reader分享

Wednesday, August 06, 2008

Programming Erlang 笔记(三)

Sequential Programming---Again..

Guards
  • A guard is a series of guard expressions, separated by commas (,).──── 一个Guard是一系列由(,)分隔的Guard表达式
  • A guard sequence is either a single guard or a series of guards, separated by semicolons (;).──── 一个Guard序列可是是一个单一的Guard,也可以是一系列用(;)分隔的的Guard
  • Guard Examples
    1. f(X,Y) when is_integer(X), X > Y, Y <> ...──── The comma, which separates the test in the guard, means “and.”
    2. is_tuple(T), size(T) =:= 6, abs(element(3, T)) > 5
      element(4, X) =:= hd(L)
    3. X =:= dog; X =:= cat
      is_integer(X), X > Y ; abs(Y) <>Y或者Y的绝对值小于23。Guard Sequence用于表示“或”,而(,)来表示“且”
    4. The guard sequence G1; G2; ...; Gn is true if at least The guard sequence G1; G2; ...; Gn is true if at least one of the guards—G1, G2, ...—evaluates to true. one of the guards—G1, G2, ...—evaluates to true.


Records
  • record is not a shell command.record definitions can be included in Erlang source code files or put in files with the extension .hrl──── record不是一个Shell命令;record定义可以被包含在Erlang源代码中或者放在一个扩展名为“.hrl”的文件中
  • Records are declared with the following syntax:
    -record(Name, {
    %% the next two keys have default values
    key1 = Default1,
    key2 = Default2,
    ...
    %% The next line is equivalent to
    %% key3 = undefined
    key3,
    ...
    }).
    Name is the name of the record. key1, key2, and so on, are the names of the fields in the record; these must always be atoms.────上面的定义中,Name是record的名字,key1, key2....是记录中条目的名字,他们必须是atom
  • -record(todo, {status=reminder,who=joe,text}).
    Once a record has been defined, instances of the record can be created.──── 一旦一个record被定义了,就可以创建这个record的实例
    1> rr("records.hrl").
    [todo]
    2> X=#todo{}.
    #todo{status = reminder,who = joe,text = undefined}
    3> X1 = #todo{status=urgent, text="Fix errata in book"}.
    #todo{status = urgent,who = joe,text = "Fix errata in book"}
    4> X2 = X1#todo{status=done}.
    #todo{status = done,who = joe,text = "Fix errata in book"}
  • Extracting the Fields of a Record────从Record中提取元素
    5> #todo{who=W, text=Txt} = X2.
    #todo{status = done,who = joe,text = "Fix errata in book"}
    6> W.
    joe
    7> Txt.
    "Fix errata in book"
    8> X2#todo.text.
    "Fix errata in book"
  • Records Are Tuples in Disguise────record其实是伪装了的Tuple
    9> X2.
    #todo{status = done,who = joe,text = "Fix errata in book"}
    10> rf(todo).
    ok
    11> X2.
    {todo,done,joe,"Fix errata in book"}
    Records are just tuples.Internally there are only tuples.────Record就是Tuple;从内部来讲,Erlang中只能Tuple




case and if Expressions
  • case Expressions
    case has the following syntax:
    case Expression of
    Pattern1 [when Guard1] -> Expr_seq1;
    Pattern2 [when Guard2] -> Expr_seq2;
    ...
    end
  • if Expressions
    if
    Guard1 ->
    Expr_seq1;
    Guard2 ->
    Expr_seq2;
    ...
    end
    Often the final guard in an if expression is the atom true, which guarantees that the last form in the expression will be evaluated if all other guards have failed.────
    "if"表达式的最后一个guard常常是atom "true",它保证了如果前面的guard全部没有匹配的,最后一个肯定会被计算


Building Lists in Natural Order
  • The most efficient way to build a list is to add the elements to the head of an existing list
    The basic idea is fairly simple:
    1. Always add elements to a list head.
    2. Taking the elements from the head of an InputList and adding
    them head first to an OutputList results in the OutputList having
    the reverse order of the InputList.
    3. If the order matters, then call lists:reverse/1, which is highly opti-
    mized.
    4. Avoid going against these recommendations.
    If you ever see code like this:
    List ++ [H]
    it should set alarm bells off in your brain—this is very inefficient and acceptable only if List is very short.

No comments: