Lisp-Ten Commandments

这十条戒律因该说是在进行Lisp编程时,需要引起格外注意的地方,否则便有可能引入错误。很有意思呢。。不太好翻译,还是英文原文表达的简洁易懂一些。所以摘录如下:

第一戒:

When recurring a list of atom, lat, ask two questions about it: (lat? l) and else.

When recurring on a number, n, ask two questions about it: (zero? n) and else.

When recurring on a list of S-expressions, l, ask three question about it: (null? l), (atom? ( car l)), and else.

 

第二戒:

Use cons to build lists.

 

第三戒:

When building a list, describe the first typical element, and then cons it onto the natu­ral recursion.

 

第四戒:

Always change at least one argument while recurring.

When recurring on a list of atoms, lat, use (cdr lat).

When recurring on a num­ber, n, use (sub1 n).

When recurring on a list of S-expressions, l, use (carl) and (cdr l) if neither (null? l) nor (atom? (carl)) are true.

 

第五戒:

When building a value with + ,always use 0 for the value of the terminating line, for adding 0 does not change the value of an addition.

When building a value with x, always use 1 for the value of the terminating line, for multiplying by 1 does not change the value of a multiplication.

When building a value with cons, always consider () for the value of the terminating line.

 

第六戒:

Simplify only after the function is correct.

 

第七戒:

Recur on the subparts that are of the same nature:
On the sublists of a list.
On the subexpressions of an arithmetic expression.

 

第八戒:

Use help functions to abstract from representation.

 

第九戒:

Abstract common patterns with a new function.

 

第十戒:

Build functions to collect more than one value at a time.

posted @ 2012-08-12 17:46  hi_fly  阅读(258)  评论(0编辑  收藏  举报