显示标签为“Lisp/Scheme”的博文。显示所有博文
显示标签为“Lisp/Scheme”的博文。显示所有博文

2010年5月9日星期日

Lisp learning notes1

Although Lisp is a powerful programming language with a very simple syntax. However, the ads is always said in a tricky way. Lots of functions and marcos should be remembered if you want to use lisp. I just want to make a list of functions and marcos I have met in the book to keep me remember of all this.
Here it is:















































formatmost flexible way of output
ta shorthand for the stream *standard-output*
defunfunctions define
loadload a file of expression
compile-filecompile a lisp file
listmake a list
(list :a 1 :b 2 :c 3)property list
getfchoose element by property
defvardefine a variable
pushadd items 1st to 2nd
dolistlooping from 2nd param
~aoutput format consume one argument
~10temit spaces
~%emit one more newline to put blank line
~{ and ~}loops over that list
force-output=flush in c
read-lineread a single line of text
*query-io*global variable contain input stream
orlogical operation
y-or-n-pask for y/n
looprepeat until return called
returnend loop
ifjust "if"
notlogical operation
with-open-fileopens a file
with-standard-io-syntaxensures certain variables that affect
printoutput as a lisp readable format
setfassignment operator
readread from the stream in
evenpreturn T if argument is an even number
remove-if-nottakes a predicate and a list return list match predicate
lambdaanonymous function
equalcompare strings
mapcarmap over a list
defmarcodefine a macro
reversereverse a list
popinverse operation of push
-pcheck if the param is be passed
' or `stop evaluation expression
,start evaluated
#shorthand for get function by name
functionsyntactic sugar for #'
@splices the value
&key / &rest / &optionalvary params
funcall(funcall #'foo para1 para2)call functions
apply==funcall paras be a list

2009年7月20日星期一

The Rise of "Worse is Better''

By Richard Gabriel

I and just about every designer of Common Lisp and CLOS has had extreme exposure to the MIT/Stanford style of design. The essence of this style can be captured by the phrase ``the right thing.'' To such a designer it is important to get all of the following characteristics right:

* Simplicity-the design must be simple, both in implementation and interface. It is more important for the interface to be simple than the implementation.
* Correctness-the design must be correct in all observable aspects. Incorrectness is simply not allowed.
* Consistency-the design must not be inconsistent. A design is allowed to be slightly less simple and less complete to avoid inconsistency. Consistency is as important as correctness.
* Completeness-the design must cover as many important situations as is practical. All reasonably expected cases must be covered. Simplicity is not allowed to overly reduce completeness.

I believe most people would agree that these are good characteristics. I will call the use of this philosophy of design the ``MIT approach.'' Common Lisp (with CLOS) and Scheme represent the MIT approach to design and implementation.

The worse-is-better philosophy is only slightly different:

* Simplicity-the design must be simple, both in implementation and interface. It is more important for the implementation to be simple than the interface. Simplicity is the most important consideration in a design.
* Correctness-the design must be correct in all observable aspects. It is slightly better to be simple than correct.
* Consistency-the design must not be overly inconsistent. Consistency can be sacrificed for simplicity in some cases, but it is better to drop those parts of the design that deal with less common circumstances than to introduce either implementational complexity or inconsistency.
* Completeness-the design must cover as many important situations as is practical. All reasonably expected cases should be covered. Completeness can be sacrificed in favor of any other quality. In fact, completeness must sacrificed whenever implementation simplicity is jeopardized. Consistency can be sacrificed to achieve completeness if simplicity is retained; especially worthless is consistency of interface.

Early Unix and C are examples of the use of this school of design, and I will call the use of this design strategy the ``New Jersey approach.'' I have intentionally caricatured the worse-is-better philosophy to convince you that it is obviously a bad philosophy and that the New Jersey approach is a bad approach.

However, I believe that worse-is-better, even in its strawman form, has better survival characteristics than the-right-thing, and that the New Jersey approach when used for software is a better approach than the MIT approach.

Let me start out by retelling a story that shows that the MIT/New-Jersey distinction is valid and that proponents of each philosophy actually believe their philosophy is better.

Two famous people, one from MIT and another from Berkeley (but working on Unix) once met to discuss operating system issues. The person from MIT was knowledgeable about ITS (the MIT AI Lab operating system) and had been reading the Unix sources. He was interested in how Unix solved the PC loser-ing problem. The PC loser-ing problem occurs when a user program invokes a system routine to perform a lengthy operation that might have significant state, such as IO buffers. If an interrupt occurs during the operation, the state of the user program must be saved. Because the invocation of the system routine is usually a single instruction, the PC of the user program does not adequately capture the state of the process. The system routine must either back out or press forward. The right thing is to back out and restore the user program PC to the instruction that invoked the system routine so that resumption of the user program after the interrupt, for example, re-enters the system routine. It is called ``PC loser-ing'' because the PC is being coerced into ``loser mode,'' where ``loser'' is the affectionate name for ``user'' at MIT.

The MIT guy did not see any code that handled this case and asked the New Jersey guy how the problem was handled. The New Jersey guy said that the Unix folks were aware of the problem, but the solution was for the system routine to always finish, but sometimes an error code would be returned that signaled that the system routine had failed to complete its action. A correct user program, then, had to check the error code to determine whether to simply try the system routine again. The MIT guy did not like this solution because it was not the right thing.

The New Jersey guy said that the Unix solution was right because the design philosophy of Unix was simplicity and that the right thing was too complex. Besides, programmers could easily insert this extra test and loop. The MIT guy pointed out that the implementation was simple but the interface to the functionality was complex. The New Jersey guy said that the right tradeoff has been selected in Unix-namely, implementation simplicity was more important than interface simplicity.

The MIT guy then muttered that sometimes it takes a tough man to make a tender chicken, but the New Jersey guy didn't understand (I'm not sure I do either).

Now I want to argue that worse-is-better is better. C is a programming language designed for writing Unix, and it was designed using the New Jersey approach. C is therefore a language for which it is easy to write a decent compiler, and it requires the programmer to write text that is easy for the compiler to interpret. Some have called C a fancy assembly language. Both early Unix and C compilers had simple structures, are easy to port, require few machine resources to run, and provide about 50%--80% of what you want from an operating system and programming language.

Half the computers that exist at any point are worse than median (smaller or slower). Unix and C work fine on them. The worse-is-better philosophy means that implementation simplicity has highest priority, which means Unix and C are easy to port on such machines. Therefore, one expects that if the 50% functionality Unix and C support is satisfactory, they will start to appear everywhere. And they have, haven't they?

Unix and C are the ultimate computer viruses.

A further benefit of the worse-is-better philosophy is that the programmer is conditioned to sacrifice some safety, convenience, and hassle to get good performance and modest resource use. Programs written using the New Jersey approach will work well both in small machines and large ones, and the code will be portable because it is written on top of a virus.

It is important to remember that the initial virus has to be basically good. If so, the viral spread is assured as long as it is portable. Once the virus has spread, there will be pressure to improve it, possibly by increasing its functionality closer to 90%, but users have already been conditioned to accept worse than the right thing. Therefore, the worse-is-better software first will gain acceptance, second will condition its users to expect less, and third will be improved to a point that is almost the right thing. In concrete terms, even though Lisp compilers in 1987 were about as good as C compilers, there are many more compiler experts who want to make C compilers better than want to make Lisp compilers better.

The good news is that in 1995 we will have a good operating system and programming language; the bad news is that they will be Unix and C++.

There is a final benefit to worse-is-better. Because a New Jersey language and system are not really powerful enough to build complex monolithic software, large systems must be designed to reuse components. Therefore, a tradition of integration springs up.

How does the right thing stack up? There are two basic scenarios: the ``big complex system scenario'' and the ``diamond-like jewel'' scenario.

The ``big complex system'' scenario goes like this:

First, the right thing needs to be designed. Then its implementation needs to be designed. Finally it is implemented. Because it is the right thing, it has nearly 100% of desired functionality, and implementation simplicity was never a concern so it takes a long time to implement. It is large and complex. It requires complex tools to use properly. The last 20% takes 80% of the effort, and so the right thing takes a long time to get out, and it only runs satisfactorily on the most sophisticated hardware.

The ``diamond-like jewel'' scenario goes like this:

The right thing takes forever to design, but it is quite small at every point along the way. To implement it to run fast is either impossible or beyond the capabilities of most implementors.

The two scenarios correspond to Common Lisp and Scheme.

The first scenario is also the scenario for classic artificial intelligence software.

The right thing is frequently a monolithic piece of software, but for no reason other than that the right thing is often designed monolithically. That is, this characteristic is a happenstance.

The lesson to be learned from this is that it is often undesirable to go for the right thing first. It is better to get half of the right thing available so that it spreads like a virus. Once people are hooked on it, take the time to improve it to 90% of the right thing.

A wrong lesson is to take the parable literally and to conclude that C is the right vehicle for AI software. The 50% solution has to be basically right, and in this case it isn't.

But, one can conclude only that the Lisp community needs to seriously rethink its position on Lisp design. I will say more about this later.

2009年7月19日星期日

初识Lisp

翻刘汝佳大牛的课件,无意中看到一本书SICP就买了本。Lisp这个世界上还在使用的第二老的语言,显然有她的独到的魅力。



Lisp的语法相当诡异,她使用前缀表达式,这一表达式在加强了其表述能力的同时,让许多人望而却步。Lisp已经有50年的历史了,但悲剧的是这门优雅强大的语言重来就没有成为主流语言过。国内连本Lisp的书都买不到。Lisp是大师的语言,高楼大厦都是大师所设计的;C是农民工语言,摩天大楼为其所建。只看完了sicp的第一节,就感觉了他的灵活性,以及强大的表达能力。



Lisp最近又有回升趋势,




让我们看看其他语言高手的评价吧!据资料显示,著名Java高手、Jolt大奖获奖作者Bruce Tate在2007年发表的《编程语言的王道:Lisp之美》中这样说到:



“Lisp
长久以来一直被视为伟大的编程语言之一。其漫长的发展过程(接近五十年)中引发的追随狂潮表明:这是一门非同凡响的语言。在 MIT,Lisp
在所有程序员的课程中占了举足轻重的地位。像 Paul Graham 那样的企业家们将 Lisp
卓越的生产力用作他们事业成功起步的推动力。但令其追随者懊恼万分的是,Lisp 从未成为主流编程语言。作为一名
Java程序员,如果您花一点时间研究 Lisp 这座被人遗忘的黄金之城,就会发现许多能够改进编码方式的技术。”


这位Java高手显然把Lisp当化石一样的研究。但事实上,我觉得Lisp更像是一个新诞生星云,在孕育未来的生命。Lisp让人更接近机器的思考,学习一门语言的同时就可以掌握解释器,编译器是如何工作的。SICP 也同时介绍了人类抽象事物的能力,将思维带到了另一个层次。下面我们看点实际的东西:



*Lisp支持内置的列表,这一点比java内置的vector成熟很多,python向他学的倒是比较像。

*自动内存管理,同样Lisp的表现比java,python更加优秀。

*支持动态类型

*支持匿名函数,这个别人都是向他学的,java匿名函数语法相当累赘。

*统一的语法,没有Lisp的程序员因为忘记语法而苦恼,Lisp帮你打破一切语法的糖衣。

   在其他语言中,a+b+c+d, 而Lisp使用(+ a b c d),几乎所有的格式都是(函数名  <参数列表>)。

*交互环境,Lisp的交互环境让很多人认为他是一个纯解释语言,但其实不然。

*可扩展性,Lisp是一个programmalbe programming language :)



既然Lisp有比java python跟强的表达能力,那么Lisp的效率如何呢?
















































































































Test Lisp JavaPythonPerl C++
exception handling0.010.901.541.731.00
hash access1.063.234.011.851.00
sum numbers from file 7.54 2.63 8.34 2.49 1.00

100+ x C++
reverse lines 1.61 1.221.38 1.25 1.00

50-100 x C++
matrix multiplication 3.30 8.90278.00 226.00 1.00

10-50 x C++
heapsort 1.67 7.0084.42 75.67 1.00

5-10 x C++
array access 1.75 6.83141.08 127.25 1.00

1-5 x C++
list processing 0.93 20.4720.33 11.27 1.00

0-1 x C++
object instantiation 1.32 2.3949.11 89.21 1.00
word count 0.73 4.612.57 1.64 1.00
25% to 75% 0.93 to 1.67 2.63 to 7.002.57 to 84.42 1.73 to 89.21 1.00 to 1.00

从上表我们可以看到,Lisp几乎是Java的1.5到4倍,Python的10到50倍(当然Python还在不断进化当中),而她跟C++差不多。



说了这么多,我还是写几本打你推荐的Lisp书比较好:



Paul Graham's On Lisp and ANSI
Common Lisp

SICP(scheme), MIT PRESS



Lisp的运行环境:

scheme用MIT Scheme(Linux下有点问题), PLT-Scheme用的蛮好

Lisp用 cmucl, sbcl, clisp都比较好,gcl传说还不是ANSI标准,我Fedora下运行还是段错误