Saturday, April 14, 2007

Road to Metaprogramming, a step closer

I was trying to use cl-prevalence today. As a practice, I tried to define a deftransaction macro, just the one like in the bknr-datastore package. I didn't choose the latter, because in Slime, the transactions always get stuck.

Suppose you're using prevayler in Java. It has a transaction interface. Everytime you want a new transaction, you have to define a class, write the same string "implements Transaction", along with the prototype of the transaction execution method. What if you can make transaction something more like class? Just like "enum", raised to the level of a keyword? Or, rather a function declarator like synchronized, denoting the function to be expanded in to a Transaction subclass.

If you can write:

public transaction int myTransaction (int arg1, int arg2) {
return arg1 + arg2;
}

Life would be so much easier. The compiler then has to compile this piece of code into a Transaction subclass. I don't know if the Java 5 annotation can do this. I suspect that the decorators in the latest Python versions can do this. I doubt any Java-nik can do this trick properly.

Well, I'm sure that Lisp can do this. I wrote a 'deftxn' macro, and now I can define transactions just like I define functions (with defun substituted by deftxn of course). I can call the transaction just like I call ordinary functions: actually, they are ordinary functions. The deftxn macro actually defines such a function for me.

This practice brings me one step closer to the so-called "Metaprogramming". And Lisp is the king language in this field. Wish me luck : )

P.S.: the deftxn code:

(in-package :clp-ext)

(defmacro deftxn (name (system &rest args) &body body)
(dolist (arg args)
(when (listp arg)
(error "can't have argument defaults in transaction declaration for transaction ~A, \
please implement a wrapper" name)))
(let ((args-name (gensym))
(tx-name (intern (string-upcase (concatenate 'string "tx-" (symbol-name name)))
(symbol-package name))))
`(progn
(defun ,tx-name (,system ,@args)
,@body)
(defun ,name (,system &rest ,args-name)
(execute ,system
(apply #'make-transaction ',tx-name ,args-name))))))

Tuesday, April 10, 2007

一个懒人兼傻瓜的Linux生活

我是一个傻瓜,因为我对于Linux的配置几乎可谓是一窍不通。/etc/目录下的文件,我几乎从来没有去动过:除了apt/source.list,这个是个例外,我大致知道怎么改。。。

我还是一个懒人,因为我对于学会配置Linux没有特别大的热情。据说程序员几乎都是懒人,也不奇怪,GC这种东西,逐渐成为主流,也就说明了懒惰成为了主流吧。。。

然而就像我这样一个傻瓜和懒人,在Linux下生活得相当地安逸——当然,前提是我对于玩游戏没有特别大的兴趣。如果我非玩WoW不可,那么我肯定还在windows的泥潭中挣扎着。然而我没有这样的需求,因此Linux完全可以应付我的生活。

并不是说我的生活没有娱乐。正相反,听歌看电影(嘘,盗版的。。。-.-)我都一样不落。我也没有使用非常高深的技巧来达到这个目的。事实上,我只是多敲了几次aptitude install,就做到了这一切。由于一开始分不清几种播放器的区别,保险起见,我把gxine,mplayer,和totem都装上了。呃,我还在mplayer的网站上下过一个codec的压缩包。然后,一切就搞定了。没有任何魔法需要施展。甚至比windows还简单:安装软件的时候你甚至不需要选择安装路径。记得在windows下的时候,默认的c:\program files是一定要改掉的,因为装在系统盘上有各种各样的潜在问题。然而在Linux下,你甚至连这个问题都不需要考虑。

也不是说我的生活没有出现过问题。中文显示也曾困扰过我。刚装完的时候,切换到中文以后,屏幕上的字一度有大有小,看着很令人不爽。该怎么办呢?去哪里修改传说中的xconf么?还是改xfont的配置?(BTW,我都不知道是不是有这样的配置存在)No,No,No。。。还是aptitude install一下搞定。原来是有几个中文字体没装,装上,立马就好了。

关于中文,出现过各种各样的问题。比如说Scim输入法怎么装的问题。本来我是有可能要钻研一下,然后跑去/etc/下配置的。然而,在查阅了88后,发现只要装一个multilanguage的配置包,然后简单运行一下 就好了。于是我又一次开心地错过了学习配置的机会。。。另外,emacs里输入和显示中文也是一个很大的问题。在经过一番google之后,我发现,只要我放弃emacs的x模式,用控制台模式,甚至不需要什么配置,就能搞定中文。我欣然地接受了这个解决方案。

因此,我很开心地坐在这里写blog。我完全不用担心我的电脑什么时候会开始变得诡异地越跑越慢,这让我需要思考的事情又少了一件。

其实,题目应该叫懒人张毅超的幸福生活。

Linux下,我活得真的很幸福。。。

Wednesday, April 04, 2007

How you install and configure java on a debian machine

If you want to install JRE:
# apt-get install sun-java5-jre

If you want to install JDK:
# apt-get install sun-java5-jdk

If you want to install Java plugin for the browser:
# apt-get install sun-java5-plugin

If you want to install Java documentation, install this package and follow the instructions:
# apt-get install sun-java5-doc

If you wan to install some fonts:
# apt-get install sun-java5-fonts

If you want to install some demos for the JDK:
# apt-get install sun-java5-demo

After you have installed everything you want, you need to make sure that Sun java is the default Java runtime environment:
# update-java-alternatives –set java-1.5.0-sun

Which programming language will you choose?

I had some chat with some of my former roommates last night. They are double E guys and are now working on embedded systems now. I mentioned that we don't use C very much now, and they got very surprised. They had classes teaching them C as freshmen, and they worked hard for another 2 or 3 years to be able to use it well. And when they finally had the time to breathe in the open air, all of a sudden, they found out that the CS guys don't use C that much any more.

It's not that C used to be very popular among the CS guys. If you take "used to" to mean several decades ago, then I'm probably wrong. C was very popular, and is popular still if you take look at the top guys in the industry. But C is the last resort a programmer will go to if speed is a must. Or perhaps if you or your boss only know C, then C is a natural choice.

If you want to program on a real computer which runs modern operating systems, you should choose your language carefully -- or rather, lazily. Programmers are lazy, and that's the reason why there are programming languages at all. Some people don't want to remember machine code, and hola, they invented sentence-like executable statements. If you work hard to be as lazy as, or even lazier than those guys, you'll live much happier here after.

Java is hot. Should I have said "was"? I'm not so sensative to the trends. People usually feel very comfortable with Java. If you think this statement looks strange, you must not have heard of Lisp before. If you decide to stay in the "mainstream", which is not a bad idea, you can put Java under your belt and leave for the adventure now! Bless you.

If you are so lazy that you hate even the braces in Java, and the awkward inner classes, you can take a look at Python. If you never want to be professional, it could be your best choice. If I had never learned C, I would choose Python as my first language perhaps. It's a very good language. If you think programmers need discipline, then Python is definately your choice! It requires you not to miss even a single tab.

I don't know much of Perl. I don't think it's designed for programs. I used it for some small scripts, and that's the farthest I ventured. I know I'm biased, but it's the best I could have done.

Smalltalk and Lisp are two languages that are very alike, in the sense that people had to work hard to describe the grammars to those that come from the C or Java background. However, they have the simplest gramars. If you think the word "meta" sounds cool, and you're after the ulmate language, then these two are worth your try. If you want to look different from everyone else, Lisp is your natural choice.

If you're an unfortunate EE guy working on embedded systems, C is your only choice. If you worship RMS and want to follow the GNU coding convention, C is also good for you, though Stallman himself may prefer Lisp. Judging by the fact that he implemented a Lisp system. If you are normal enough to feel that C is too much trouble, you can try C++.

Those are all that I know of. Basic and Pascal are not for real programmers. Ruby sounds like perl, and are famous but for its web framework. C# is from the notorious Microsoft so I don't want to appear to have even heard of it. Javascript is a very good thing, however it can't possibly run outside a browser. Lisp is the best. I really want everybody to understand this and agree with me.
When to use p2p?

This morning I heard my two roommates talking about p2p searching. As doctors-to-be, they are under the pressure of publishing papers, and p2p is a fairly new idea in the searching field. One of them thought it'd be cool to have p2p searching while the other observed that he went to far.

If Google implemented its huge clusters with p2p technology, I would not be surprised. I would consider it cool and love Google more fanatically. But if I have to host some tiny portion of Google's database, and search a thousand people's machines for some terms while knowing that more people are searching mine, well, I personally don't think it's quite a good idea. It's not that it's like making your private house a public place. Not at all. Using bittorrent has already made it public. It's because p2p doesn't fit here.

If you look around and take a look at whatever you do that's p2p, most probably you'll see file sharing. Whether you have realized it, chatting and instant messaging is somewhat p2p, or at least somewhere p2p has crept into. And if there are any other daily routines that may be p2p, I don't know of them. What do you think made the file sharing and instant messaging pioneers in the p2p field?

Time is my answer to the question. Both of them can be time consuming. If you can download a file with ftp within a second (legally), why choose bittorrent? Bittorrent itself claims to be a technology to shorten the time people waits for downloading. Instant messaging itself doesn't cost much time, so long as only the software is concerned. However, if two people are talking over a network connection made up by the im, they often make the connection last.

My point is, when people have to do one thing for a long time, with the help of others, they won't deny others' request to help, at least not before they finish. That's how p2p work. If I want to download a file from many people, and when I'm downloading, I won't refuse to upload some and let other people download. However, once I'm done, I can promise nothing. I may have to shut down the computer and go to sleep. Nobody should expect my effort to keep the system up and running. I got my own business to mind.

That's the problem p2p searching got. If it's inside Google, well, those machines are born (or manufactured if you like) to search and cooperate with each other. They can be masters and slaves, or they can be peers. Democracy never goes into the corporations anyway. If it's outside, things are so different. Searching are expected to be done within one second -- Google promised much faster. Even if you search more than a thousand times a day, it doesn't add up to an hour, and maybe a lot less. When you're not searching, how can you be sure that you can help other people search? You may have the computer on, and plugged into the internet, but you may also have the p2p searching process a very low priority one in the shadowed background. Even if you wish to help, you don't necessarily be of much help.

I think p2p is very good thing, and I've been thinking a lot about it. But like anything else, p2p got its own weaknesses. Don't abuse the idea. Of course, if you're writing a paper to get your degree, I'm on your side.
几乎成了英雄》观感

一共九章的小说,在网上读了七章,终于按捺不住,冲进书店去买了书,给作者的收入做了贡献。张嘉佳,南大才子,据说纵横于荧屏之上。 对于一个浙大人来说,难免感觉落寞。然而他的才气,还是不得不令我感到折服。

之所以会去买这本书,恐怕还是与我心中一直蠢蠢欲动的叛逆气息有关吧。主角南宫成,正是那个一直被我的自制力所压抑的那另一个我所渴 望成为的样子。更不羁,虽然多了些我所不屑的痞子气,然而,人所不屑,又何尝不是人所仰羡而不得的呢?南宫的行为,完全无视了常理, 然而他的,却还落在人可以理解的范围里面。而正是他的那颗心,才真正让我觉得他是离我并不太远的人物。

曾几何时,我也是一个无视,或者说不愿视,不知视人世常理的人。然而大学四年的打磨,以及整日的撞钟,将我身上的棱角已经渐渐地冲刷 去了。假如时间是一条河流,当它流过我身边之后,恐怕会变成携满泥沙的黄河吧。。。因为它从我身上带走了曾经是我体内峥嵘的碎屑。

听起来像是自我的炫耀。我总是不自觉地要听下来想想自己说的话是不是有夸耀的成分。然而,这一次决不会是。在每一个人身上,都有着南 宫成的影子。当一个人在自己心里暗暗骂娘的时候,他的心在那一瞬间就变成了一个无比弱化版的南宫成。每一个人都会体验过心血来潮,一 时冲动的经历。那时候,正是被压抑已久的南宫成想要突围而出的时刻。然而,真正能够冲破束缚,甚而获得美女青睐的,终究只有南宫成一 个。

作为一个情感方面的白痴,我不知道南宫成那样的人究竟会不会是女人的宠儿。韦小宝是,然而那终究是金庸的一家之言。作为一个缺少了他 的张扬个性的人,我不禁希望那种女人缘是作者一厢情愿的YY。然而即便如此,得友如南宫,也可谓是人生一大乐事。

Monday, April 02, 2007

天啦。。。我的blog上的广告越来越诡异了呢。。。日文都出来了,真是一大滴汗啊!

难道日文也和我的blog内容有关系吗。。。我可是没输过一个假名阿。。。

竟然有一大半是浪费在这种弱智的事上!

今天尝试着要用ecb来看代码。。。结果死活不会在函数调用上跳转到函数定义。。。

翻来翻去ecb的文档和mailing list,终于确认是etags的功能,于是去找etags的文档,还是死活搞不定,按M-.提示说no tags file loaded。。。然而tags文件是指定了的,而且这个提示google不到。。。

最后查key binding,发现绑的是cee-tags-find而不是默认的find-tag,难怪行为不一样。。。而cee-tags-find也google 不到,最后google那个cee-tags,才发现原来是bigloo代进来的绑定!bigloo,一个java作的scheme后端。。。为什么会需 要这个呢。。。而且ctags,貌似是vi的tags文件阿。。。确认它试图找一个叫cee-tags的文件。有空用ctags作个试试看它认不。

之前也老是做这样的事。。。研究到半夜,却是很没有价值的小事。。。

不过好歹研究出来了哈~~还是满牛的嘛^_^

What’s so elegant about the Common Lisp language?

Well, first of all, IMHO, it’s a uniform language. What is a uniform language? Take a look at C. A function call is like foo(bar). But in C, everything can’t be done with just functions. You gotta use the control statements. The if statement, for instance, its head looks quite like a function: if(blah), while it’s got a huge and ugly body with or without braces, one or more statements. It’s not uniform. Function calls and control statements look way different from each other. They won’t even greet each other meeting on the road, I guess.

Smalltalk is somewhat uniform, because it contains no control statements. Smalltalk is totally OO, and it actually uses polymorphism to avoid conditions. If you ever read about polymorphism-instead-of-condition talk, and got really confused or even frustrated looking at your C++ or Java code, do take a look at Smalltalk. There’s no special if keyword there. If you have to do anything depending upon a condition, use ifTrue: ifFalse: keyword method of the Boolean class. It’s not a keyword, it’s a method. If you wish, you can write your own whenTrue: whenFalse: methods and work equally well. And, the fact that they’re ordinary functions means they look exactly the same like any other functions of the language. And thus is the language uniform.

Well, I call Smalltalk “somewhat” uniform because its definition of methods looks different from the calls of the methods. Few languages actually make both sides look exactly the same. In JavaScript for example, you can define a function just like writing ordinary statements. Say, foo = function(bar) {return bar;}. It’s an assignment, so it’s a legal statement. But on the other hand, you can still define functions the traditional and declarative way: function foo(bar) {return bar;}. It does’t look like a statement and it doesn’t behave like one. The latter situation can be tolerated since we’re talking about “looking” elegant. The former part of the sentence declares the lack of elegance of the JavaScript language.

Lisp is different. It’s uniform. if is special in Lisp, but it looks just the same way as anything else. (if t (foo bar) (fooo barr)). Foo is supposed to be a function. And the if expression looks just like the invocation of the foo function. You wanna see the function declaration? No problem, (defun foo (bar) (bar)). Looks the same. Parantheses, and tokens in between. Everything is a list. What elegance!

Said I’m learning Common Lisp. I wanted to learn ‘coz I was told that it’s got super “metaprogramming” powers. I was curious so I decided to take a look myself. They were talking about macros. I am now looking at the macros.

I have not done the macros. There’re more but the bit I have chewed proved judicious. C has macros. Java doesn’t, what a pity, and I think Python and other scripting languages don’t either. Yet C does. However, the macros in C should be ashamed if they still call themselves macros when they have met macros from the Common Lisp language. They’re so different, and the Lisp counterpart is so powerful. It is because it’s dynamic.

The macros in C are just substitutions of small amount of text. It even place limits on the texts to substitute. If you #define M(a, b, c) m_blah_blah(a, b, c), and you try to use the macro M, you have to specify right 3 arguments. What’s more, the resulting code contains just m_blah_blah(a, b, c). There’s no trick to play. The preprocessor is so dumb that it knows nothing about computation or program logic.

While in Lisp, things are so different. A macro is more like a function that returns the text used to substitute the macro text. Well… not so much of texts, but rather, lists. A list is more higher level than just texts ‘coz it got more structural and program logic. When expanding the macros, the compiler actually computes things needed in the expansion. If anything can be calculated from the source code directly rather than human user intervention, the compiler can calculate it and put it right into the generated program, speeding up the runs of it. You don’t need to write another program to pre-generate the source code, and then use the source code to generate a compiled program. Sometimes, generated source is needed, I guess, even by Lisp programs. But not so much as by the C programs. I come to believe that the language is an elegant one. I come to love it.

I’m now trying to learn Common Lisp, the claimed most powerful language that eve r existed. And if you’re to use CL, you gotta use Emacs. I searched IDE’s, but I didn’t get much luck. However there’re Emacs and Vi modes for lisp. They say: ” If you really need to use Vi, then we have this mode for you…” and they don’t even try to cover up their contempt when they tell you this. So I had to use ema cs.

To use emacs, you gotta use Slime. It’s more a religion than not. CL? OK, Emacs you use, with Slime. You don’t get much choice. Fortunately, I’m not looking for options, since I’m not good at choosing. I dived into Slime directly.

With Slime, you can look up the hypersec with three key strokes. What a convenie nce! And I enjoyed it. Then one day I decided to get rid of the iceweasel I’ve a pted from the debian source. It sucks. I use FoxyProxy for proxying my Wikipedia access, and iceweasel doesn’t seem to get along quite along with the addon. To have to give up one of them, FoxyProxy will not be the answer. I can use Firefox from the Mozilla web site. And I did.

Then the C-c C-d h strokes for the Slime mode mysteriously broke down. I got abs olutely no idea what was up — I was so new in CL and Emacs that I knew less tha n an idiot. I peeked into the Slime code and saw that it used the browse-url pac kage to display the hyperspec docs. Great, but I didn’t find the browse-url sour ce. I then googled, people talked about setqing some browse-url-generic-program and more, and went suggesting C-h p for browse-url help info. I tried and found little luck. I even fixed the problem of not being able to open up my Firefox br owser because of the firefox and the run-mozilla script tries to guess the firef ox-bin name by the call name which was accidentally x-www-browser. But with Slim e, it doesn’t work. I was almost desparate.

Then I decided to try by myself. The Slime doc mentioned the *scratch* buffer as somewhere you can evaluate EL expressions. I tried, and it did eval my expr’s. I tried M-tab, and it showed me symbols starting with browse-url. I spent some t ime snoofing and found out the fact that my Emacs’s using some moz-program to op en up the browser. Then I found out the fact that Emacs was trying to bring abou t the browser with the command “mozilla”. And there’s no such thing as “mozilla” on my laptop. I made a link pointing to firefox, and the problem was fixed.

It’s easy as that. Nothing mysterious… yet nothing you would anybody’d tell yo u. It’s convention, and people expect you to know, rather than ask for. And now I tasted the power of convention.

What do I think about the state-of-the-art Java Web Frameworks?

Well, they’re definitely huge and clumsy. Most of them. Stripes don’t REQUIRE detailed configuration. Still it needs SOME configuration, ’cause it needs you to tell it what method of an autowired class should handle a request.

I’m not that deep into RoR. But it somehow gave me the impression that it needs minimal configuration. It tries to autowire everything by guessing from the names of the classes and the method names, if the user follows the convention. And it works with javascript well. I had the impression that it can push some logic to the browser side and provide AJAX utility.

Now the newest version claims to provide REST utility as well. I think REST is a great thing. I’ve found Restlet, a Java Rest framework. But it needs a bunch of configuration — hard coded in your source, that’s even worse than many web frameworks, which are configured with XML files.

I think a Java Web Framework can do better — much better. I’ve been thinking about it for quite some days now. I gradually had some idea of my ideal framework. It should be able to autowire anything if the user behaves good. On the other hand, the framework should support REST. After giving it some more thoughts, I think it’sbetter to put the controlers at the browser side and let the filters take up more responsibilities in program logic. There will not be actions in various frameworks, all business logic will be carried out by the browser and the filters. Even the greater part of the browser side scripts can be automatically generated. The user needs to configure nothing… nothing specific for the specific app, and everything is expected to be reusable.

Have to close now, may later I can explain myself better.

他热爱火球在手中积聚成型的感觉。很多高阶法师都只是下意识地施法,对于体内的能量流动失去了初学者的那种敏感。然而,他却从来不会忘记那种令人迷醉的感 觉,时时都要享受这种只有法师才能体验的乐趣。即使是最激烈的战斗也无法阻止他这么做。能量像是女郎纤长的手指一般,轻柔而挑逗地在他的血管中游走,把充 满力量的狂喜沿着神经输送到每一个末梢。与此同时,法术所激发出来的破坏力在他带着洁白手套的右手中狂暴地骚动着,叫嚣着对于毁灭的渴望。又一支箭从他脸 颊边飞过,在护盾上擦出了火花,伴随着尖锐的刮擦声钉在了他身后的树干上。然而,他的脸上还是洋溢着欣喜的神情,甚至隐隐地透着光芒。突然间,他紧闭着的 双眼猛地睁了开来。隐约围绕在他脸上的光圈也迅速膨胀开来,变成了刺目的光芒。他松开了右手。火球几乎是立刻出现在了人群中央,爆裂开来。

Sunday, April 01, 2007

还是得在外网上办博客啊。。。

自己实验室的机器上搭了一个。。半辈子了都没人理,凄惨得紧哪。。。

把这段时间写的一些东西都拉出来在这儿溜溜吧。。。