我的Google Reader分享

Wednesday, July 16, 2008

Programming Erlang 笔记(一)

Variables(变量)

  • All variable names must start with an uppercase letter.────所有的变量必须用大写字母开头
  • 例子: Eshell V5.5.5 (abort with ^G)
    1> X = 123456789.
    123456789
    2> X.
    123456789
    3> X*X*X*X*X*X*X
    3> .
    437124189620885610010004822109262358637075660656881926429

  • Single Assignment Is Like Algebra,Variables That Don’t Vary────赋值就像代数里一样,不变的变量。(回想一下,在没有接触C等其他高级语言之前,代数中的变量是怎么样的。没错,它们的值是不变的)。 例子(接上例):4> X = 1234.

    =ERROR REPORT==== 16-Jul-2008::22:34:38 ===
    Error in process <0.31.0> with exit value: {{badmatch,1234},[{erl_eval,expr,3}]}

    ** exited: {{badmatch,1234},[{erl_eval,expr,3}]} **


  • Pattern Matching────模式匹配。在其他语言(C,JAVA,RUBY and so on)中,“=”都是赋值用,而在Erlang中它是作为模式匹配用的。比如:Lhs = Rhs,它的意思是,计算右边的值(Rhs),然后和左边的(Lhs)匹配。 例子:Eshell V5.5.5 (abort with ^G)
    1> X = (2+4).
    6
    2> Y = 10.
    10
    3> X = 6.
    6
    4> X = Y.

    =ERROR REPORT==== 16-Jul-2008::22:41:58 ===
    Error in process <0.31.0> with exit value: {{badmatch,10},[{erl_eval,expr,3}]}

    ** exited: {{badmatch,10},[{erl_eval,expr,3}]} **


Floating-Point Numbers(浮点数)
  • “/” always returns a float; thus, 4/2 evaluates to 2.0000 (in the shell). ────“/”操作符总是返回一个浮点数;所以,4/2计算得2.0000(in the shell)。
  • N div M and N rem M are used for integer division and remainder; thus, 5 div 3 is 1, and 5 rem 3 is 2.────N div M 和 N rem M 用于整除和计算余数,因而,5 div 3 等于1,而5 rem 3 等于2.
  • 例子:Eshell V5.5.5 (abort with ^G)
    1> 5/3.
    1.66667
    2> 4/2.
    2.00000
    3> 5 div 3.
    1
    4> 5 rem 3.
    2
    5> 4 div 2.
    2
    6> Pi = 3.14159.
    3.14159
    7> R = 5.
    5
    8> Pi * R * R.
    78.5397


Atoms
  • In Erlang, atoms are used to represent different non-numerical constant values.────在Erlang中,atoms被用来代表不同的非数字常量。
  • In Erlang, atoms are global, and this is achieved without the use of macro definitions or include files.────在Erlang中,atoms是全局的,而且这不是通过宏定义或者包含头文件来实现的。
  • Atoms start with lowercase letters, followed by a sequence of alphanumeric characters or the underscore (_) or at (@) sign.7 For example: red, december, cat, meters, yards, joe@somehost, and a_long_name.────Atoms以小写字母开头,跟一串字母数字或者下划线(_)或者at(@)标志。比如:red, december, cat, meters, yards, joe@somehost, a_long_name。
  • The value of an atom is just the atom.────一个atom的值就是atom本身。例子: 1> hello.
    hello



Tuples(元组)
  • You can create a tuple by enclosing the values you want to represent in curly brackets and separating them with commas. for example:{joe, 1.82}────你可能用大括号括起用逗号分隔的值来创建一个元组。比如{joe, 1.82}

  • Tuples are similar to structs in C────元组和C语言中的结构类似。
  • To make it easier to remember what a tuple is being used for, it’s common to use an atom as the first element of the tuple, which describes what the tuple represents.────为了能更加容易的记住一个元组是做什么用的,通常用一个atom来做元组的第一个元素,用来描述这个元组代表什么。
  • Tuples can be nested.────元组可以嵌套。例子:Eshell V5.5.5 (abort with ^G)
    1> Person = {person,
    1> {name, joe},
    1> {height, 1.82},
    1> {footsize, 42},
    1> {eyecolor, brown}}.
    {person,{name,joe},{height,1.82000},{footsize,42},{eyecolor,brown}}
  • 2> F = {firstName, joe}.
    {firstName,joe}
    3> L = {lastName, armstrong}.
    {lastName,armstrong}
    4> P = {person, F, L}.
    {person,{firstName,joe},{lastName,armstrong}}
  • Extracting Values from Tuples────从元组中抽取数据
    1. pattern matching is fundamental to Erlang and that it’s used for lots of different tasks.────模式匹配是Erlang的基础而且它被用于许多不同的任务。
    2. If we want to extract some values from a tuple, we use the pattern
      matching operator =.────如果我们想要从一个元组中抽取数据,我们用匹配操作符“=”。
    3. 例子:Eshell V5.5.5 (abort with ^G)
      1> Point = {point, 10, 45}.
      {point,10,45}
      2> {point, X, Y} = Point
      2> .
      {point,10,45}
      3> X.
      10
      4> Y.
      45
    4. 一个更为复杂的例子:Eshell V5.5.5 (abort with ^G)
      1> Person={person, {name, {first, joe}, {last, armstrong}}, {footsize, 42}}.
      {person,{name,{first,joe},{last,armstrong}},{footsize,42}}
      2> {_,{_,{_,Who},_},_} = Person.
      {person,{name,{first,joe},{last,armstrong}},{footsize,42}}
      3> Who.
      joe
    5. _ as a placeholder for variables that we’re not interested in. The symbol _ is called an anonymous variable.────"_"作为占位符。符号"_"叫作匿名变量。


Lists(列表)
  • create a list:Eshell V5.5.5 (abort with ^G)
    1> ThingsToBuy = [{apples, 10}, {pears, 6}, {milk, 3}].
    [{apples,10},{pears,6},{milk,3}]
    2> [1+7, hello, 2-2, {cost, apple, 30-20}, 3].
    [8,hello,0,{cost,apple,10},3]

  • We call the first element of a list the head of the list.what’s left is called the tail of the list.────我们把列表的第一个元素叫做“头”,剩下的叫做“尾”。比如[1,2,3,4,5]这个列表中,头是整数1,尾是列表[2,3,4,5]。
  • Note that the head of a list can be anything, but the tail of a list is usually also a list.────注意,一个列表的头可以是任何类型,但是尾通常也是一个列表。
  • (*)If T is a list, then [H|T] is also a list, with head H and tail T.Whenever we construct a list using a [...|T] constructor, we should make sure that T is a list.────如果T是一个列表,那么[H|T]也是一个列表,头是H,尾是T。不管我们如何用[...|T]来构造一个列表,必须要保证T一定是一个列表。
  • 3> ThingsToBuy1 = [{oranges, 4}, {newspaper, 1}|ThingsToBuy].
    [{oranges,4},{newspaper,1},{apples,10},{pears,6},{milk,3}]

    (接上面的例子)
  • Extracting Elements from a List────从列表中提取元素


    4> [Buy1|ThingsToBuy2] = ThingsToBuy1.
    [{oranges,4},{newspaper,1},{apples,10},{pears,6},{milk,3}]
    5> Buy1.
    {oranges,4}
    6> [Buy2, Buy3|ThingsToBuy3] = ThingsToBuy2.
    [{newspaper,1},{apples,10},{pears,6},{milk,3}]
    7> Buy2.
    {newspaper,1}
    8> Buy3.
    {apples,10}
    9> ThingsToBuy3.
    [{pears,6},{milk,3}]
    (接上面例子)




Strings(字符串)
  • Strictly speaking, there are no strings in Erlang. Strings are really just lists of integers. Strings are enclosed in double quotation marks (")────严格的讲,Erlang中并没有字符串。所谓字符串实际上只是整数的列表。字符串是用双引号括起来的。
  • Eshell V5.5.5 (abort with ^G)
    1> Name = "Hello".
    "Hello"

  • When the shell prints the value of a list it prints the list as a string, but only if all the integers in the list represent printable characters.────当shell打印一列表的值的时候,如果这个列表中的整数都能代表一个可打印的字符,它就会把这个列表当作一个字符串来打印。
  • 2> [1,2,3].
    [1,2,3]
    3> [83,117,114,112,114,105,115,101].
    "Surprise"
    4> [1,83,117,114,112,114,105,115,101].
    [1,83,117,114,112,114,105,115,101]

  • We don’t need to know which integer represents a particular character.We can use the “dollar syntax” for this purpose.────我们没有必要去知道哪个整数代表哪个字符,我们可以用美元符号"$"来做这件事情。(见下例)
  • 5> I = $s.
    115
    6> [I-32, $u, $r, $p, $r, $i, $s, $e].
    "Surprise"



Pattern Matching Again(再谈模式匹配)
  • Note: The command f() tells the shell to forget any bindings it has. After this command, all variables become unbound────注意:f()命令会告诉shell忘记掉所以的绑定。在这个命令之后,所以的变量都是未绑定的。

No comments: