我的Google Reader分享

Wednesday, July 30, 2008

KDE4.1初体验


  今天KDE4.1终于如期发布,我也迫不及待的下来用了,惊喜之处还是蛮多的,当然问题也存在不少。不过很多不是KDE4.1本身的问题,而是原先应用程序兼容性还有显卡驱动的问题。相信随着时间的推移,这些问题都会被解决的。先来看个截图吧:)
  这个是我现在的桌面,很爽,啊哈哈。不过KWin的效果还是比不上Compiz,另外N卡对于KWin的支持我感觉也不是太好,所以我索性把特效给关了(我就是不喜欢在KDE中用Compiz)。
  用上KDE4.1后的一个感觉就是屏幕还是不够大,不够宽。真是越大越好,能放更多的Widget。另外正好想学QT4,以后就能自己写Widget了,真TMD爽呐。
  当然我还有很多小问题没有解决,比如Phonon还不大清楚,现在声音都有点问题,要找点文档来看下。我是喜新厌旧啦,暂时和我的Mac-like Gnome Say Goodbye了。

Tuesday, July 29, 2008

Erlang中的快排,近段时间的总结

  最近两个星期在看Erlang,蛮有新鲜感的一种语言。由于看的英文版,看的比较慢,也看的比较仔细的,简直可以算是一字不漏的看了。前几天刚看到List Comprehensions,由它实现的一个qsort,我觉得在某种程序上也算是能体现Erlang的特点了。


qsort([]) -> [];
qsort([Pivot|T]) ->
qsort([X || X <- T, X < Pivot])
++ [Pivot] ++
qsort([X || X <- T, X >= Pivot]).



  Erlang似乎常常用到类似于这种的递归。说实话,我数据结构和算法学的是不怎么样,对于快排,我其实也是一知半解。不过Erlang这种形式,我倒是一看就明白了。现在要我用C来实现下快排应该没什么问题了。

  今天忽然想到,3月到现在,我到底学了点什么。想了半天,我是越想越心虚,感觉自己好像什么都没有学到。所以我决定写一个日志来仔细的想想自己有哪些进步,来填补下我的心虚。
  3月到4月,我看来只能心虚了,我完全不记得我到底是在干嘛了。看了一点操作系统和Linux Kernel的东西,无心的看了下Android的NotePad的代码,Ruby Quiz没有进行下去。其他时间我甚至不知道用在哪了。
  4月到5月,我记得我的笔记本上双系统的时候应该是在这段时间,Ubuntu8.04记得好像是4月底正式发布的,而我是从Beta3开始用的,想来应该是在4月初或者3月底吧。这个月大概(只能说大概)接触了REST,听说REST是在一年前Robbin在“侠客行”上的演讲,虽然我最后没有去听,但是印在入场券上我看到了。然后一年后我不知怎么的,开始找REST的资料了。而这时候Rails已经是2.0了,其默认创建的项目就是基于REST的,这对我上手倒是帮了很大的忙。然后这个月干的事情应该就是再次熟悉Linux的各个方面吧。
  5月到6月,开始了一个自己的项目。有感于学校教材的昂贵和大四那帮人走人时对没用的书的处理方式(竟然按斤卖了。。。。),决定做一个系统,利用学校内部网络,让大家把没用的书分享出来。然后大概做了20多天吧,具体时间我无聊时上Github看看就知道,因为我每天都有Push上去。这个系统应该是5月下旬开始的,那上,中旬我在干嘛呢。。。想不出来。。。
  6月到7月,6月上,中旬我是在做我的那个项目,因为时间离的近,所以我段时间的存在感还比较实在,没有很大心虚的感觉。。。然后下旬开始应付考试了,换句话说,开始放荡了。一考试,我就特想玩游戏。。。。记不清了,好像又玩的三国志11和英雄无敌吧。三国志11我就是流氓,用流言流死人。。。英雄无敌我就是卡死人了,我的电脑啊。。。。。
  7月到现在(其实也就是到8月了,还剩两天,我还能干出舍惊天到地的?),前面半个月还是考试+课程设计。课程设计用我的那个Alpha都算不上的项目就过关了,而且我自信比其他人做的应该要好,因为我可是花了时间的了,不比他们好也太没面子了。。。然后到现在,在做一个基于ArcGIS的项目。晚上在看Programming Erlang和RESTful Web Service这两本书,玩DS上的口袋妖怪和EVE ONLINE。
  对了,提一提EVE ONLINE,从去年暑假开始玩的一个网游。其实两年前就玩过,后来因为觉得卡贵没玩,去年8月1号决心玩了。然后半年时间浪费了,今年以来,改过自新变成蹲站党。最近每天玩上4个小时左右,但因为昨天网卡(学校的上网包月卡,70RMB/月)到期,将会再次成为蹲站党。。。。这应该算是个好消息。。。。。
  虽然进步很慢,但是进步还是有的,正所谓欲速则不达,就这样吧。在就这样的基础上再提高下自己利用时间的效率就好了。

Monday, July 28, 2008

Programming Erlang 笔记(二)

Sequential Programming

Modules(模块)
  • Modules are the basic unit of code in Erlang.────模块是Erlang中基本的代码单元。
  • -module(geometry).
    -export([area/1]).
    area({rectangle, Width, Ht}) -> Width * Ht;
    area({circle, R}) -> 3.14159 * R * R.
    (这就是一个模块,geometry.erl)
  • 上面的例子中,函数area由两个子句组成,子句用分号隔开,最后用点号结束。
  • Each clause has a head and a body; the head consists of a function name followed by a pattern (in parentheses), and the body consists of a sequence of expressions, which are evaluated if the pattern in the head is successfully matched against the calling arguments. The patterns are matched in the order they appear in the function definition.────每个子句有一个头和身体;头由函数名跟着一个模式(在括号中)组成,身段由一序列的表达式组成,当头中的模式和调用的参数相匹配时,表达式就会被计算。模式按在函数中被定义的顺序进行匹配。
  • compile and run it:
    1> c(geometry).
    {ok,geometry}
    2> geometry:area({rectangle, 10, 5}).
    50
    3> geometry:area({circle, 1.4}).
    6.15752
  • the order of the clauses doesn’t matter, if the patterns in the clause are mutually exclusive.In general, though, clause order does matter.────如果子句中的模式是互斥的,那么子句就顺序就无所谓了。但是一般情况下,子句的顺序很有关系。


Functions with the Same Name and Different Arity
  • The arity of a function is the number of arguments that the function has. In Erlang, two functions with the same name and different arity in the same module represent entirely different functions. They have nothing to do with each other apart from a coincidental use of the same name.────一个函数的arity是指这个函数拥有的参数的个数。在Erlang中,同一个模块中名字相同但arity不同的两个函数是完全不同的两个函数。它们仅仅是巧合的有了相同的名字而已。
  • Here’s an example:
    sum(L) -> sum(L, 0).
    sum([], N) -> N;
    sum([H|T], N) -> sum(T, H+N).

Funs
  • funs are “anonymous” functions.────"funs"是匿名的函数。
  • 1> Z = fun(X) -> 2*X end.
    #Fun
    2> Z(2).
    4
    3> Double = Z.
    #Fun
    4> Double(2).
    4
    5> Hypot = fun(X, Y) -> math:sqrt(X*X + Y*Y) end.
    #Fun
    6> Hypot(3,4).
    5.00000
    7> Hypot(3).

    =ERROR REPORT==== 28-Jul-2008::20:14:30 ===
    Error in process <0.31.0> with exit value: {{badarity,{#Fun,[3]}},[{erl_eval,expr,3}]}

    ** exited: {{badarity,{#Fun,[3]}},[{erl_eval,expr,3}]} **
    8> TempConvert = fun({c, C}) -> {f, 32 + C*9/5};
    8> ({f, F}) -> {c, (F-32)*5/9}
    8> end.
    #Fun
    9> TempConvert({c,100}).
    {f,212.000}
    10> TempConvert({f, 212}).
    {c,100.000}
    11> TempConvert({c, 0}).
    {f,32.0000}
    12>
  • Functions that return funs, or functions that can accept funs as their arguments, are called higher-order functions.────那些返回funs,或者可以接受funs作为参数的函数叫作"higher-order"函数。
  • Functions That Have Funs As Their Arguments
    1. 12> L = [1,2,3,4].
      [1,2,3,4]
      13> lists:map(Double, L).
      [2,4,6,8]
      14> Even = fun(X) -> (X rem 2) =:= 0 end.
      #Fun
      15> Even(8).
      true
      16> Even(7).
      false
      17> lists:map(Even, [1,2,3,4,5,6,8]).
      [false,true,false,true,false,true,true]
      18> lists:filter(Even, [1,2,3,4,5,6,8]).
      [2,4,6,8]
      (接上例)
  • Functions That Return Funs
    1. 1> Fruit = [apple,pear,orange].
      [apple,pear,orange]
      2> MakeTest = fun(L) -> (fun(X) -> lists:member(X, L) end) end.
      #Fun
      3> IsFruit = MakeTest(Fruit).
      #Fun
      4> IsFruit(pear).
      true
      5> IsFruit(apple).
      true
      6> IsFruit(dog).
      false
      7> lists:filter(IsFruit, [dog,orange,cat,apple,bear]).
      [orange,apple]

the use of the -import and -export declarations in the module
  • The declaration -import(lists, [map/2, sum/1]). means the function map/2 is imported from the module lists, and so on. This means we can write map(Fun, ...) instead of lists:map(Fun, ...). cost/1 was not declared in an import declaration, so we had to use the “fully qualified” name shop:cost.────定义"-import(lists,[map/2,sum/1])"的意思是函数"map/2"是从模块"lists"中导入的。所谓导入的意思是指你可以用"map(Fun,...)这样的写法代替"lists:map(Fun, ...)"。而比如"cost/1"在导入声明中没有被声明,则我们必须使用"fully qualified"名称"shop:cost".
  • The declaration -export([total/1]) means the function total/1 can be called from outside the module shop2. Only functions that are exported from a module can be called from outside the module.────定义"-export([total/1])的意思是指函数"total/1"可以从模式外面被调用。只有那些从模块中导出的函数才可以在模块外被调用。


List Comprehensions
  • List comprehensions are expressions that create lists without having to use funs, maps, or filters. This makes our programs even shorter and easier to understand.────List comprehensions是一种可以不使用funs,maps,filters就可以创建lists(列表)的表达式。这让我们的程序更加短小,而且更加容易理解。
  • 1> L = [1,2,3,4,5].
    [1,2,3,4,5]
    2> lists:map(fun(X) -> 2*X end, L).
    [2,4,6,8,10]
    3> [2*X || X <- L]. [2,4,6,8,10]
  • The notation [ F(X) || X <- L] means “the list of F(X) where X is taken from the list L.” Thus, [2*X || X <- L ] means “the list of 2*X where X is taken from the list L.”────"[F(X) || X <- L]"的意思是指"经过F(X)处理的列表,X来自于列表L"。因而,"[2*X || X <- L]的意思是“经过2*X处理的的列表,X来自于列表L”。
  • 4> Buy=[{oranges,4},{newspaper,1},{apples,10},{pears,6},{milk,3}].
    [{oranges,4},{newspaper,1},{apples,10},{pears,6},{milk,3}]
    5> [{Name, 2*Number} || {Name, Number} <- Buy].
    [{oranges,8},{newspaper,2},{apples,20},{pears,12},{milk,6}]

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忘记掉所以的绑定。在这个命令之后,所以的变量都是未绑定的。