最奇特的编程语言特征

jopen 12年前
     <p>目前为止,据我个人观察,从语法角度上讲,最被人诟病的一种语言应该是LISP语言了。打开这种语言的代码,一眼望去,满目全是括弧,一层套一层。 这种语言特征最大的问题是,它不符合人们通常的思维习惯。LISP语言的这个特点是个整体的语言现象,而任何一种语言其实也都有一些个别的很奇特的东西, 其中有些会奇怪的让你摸不着头脑。有一个好事者在<a href="/misc/goto?guid=4958321976670321114" target="_blank">一个帖子</a>上征集各种语言里不合常理的地方,结果收集到了320多条,问题最大最多的语言算是Javascript了,另外还有C,Java,Python,PHP等等。下面列出的是其中最有趣的几条。</p>    <ol>     <li>在C语言里,数组可以这样索引取值:<pre><code>a[10] </code></pre> <p>这种写法相当常见。</p> <p>然而,还有一种很少见的写法(绝对可用!)是这样的:</p> <pre><code>10[a] </code></pre> <p>这两种写法的效果是一样的。</p> </li>     <li>在JavaScript里:<pre><code> '5' + 3 结果是 '53' </code></pre> <p>而</p> <pre><code> '5' - 3 结果是 2 </code></pre></li>     <li>在JavaScript里,下面的语法结构<pre>return  {      id : 1234,      title : 'Tony the Pony'  };</pre> <p>会提示语法错误:”缺少分号”。而下面的写法却能按你预想的结果运行:</p> <pre>return {      id : 1234,      title : 'Tony the Pony'  };</pre> <p>更让人郁闷的是下面的写法竟然可以(至少在谷歌浏览器里是这样的):</p> <pre>return /*  */{      id : 1234,      title : 'Tony the Pony'  };</pre> <p>下面的例子也是同样的问题引起的,但不会报错,而你却得不到想要的结果:</p> <pre>return      2 + 2;</pre></li>     <li>JavaScript的等式对照表:<pre>''        ==   '0'           // false  0         ==   ''            // true  0         ==   '0'           // true  false     ==   'false'       // false  false     ==   '0'           // true  false     ==   undefined     // false  false     ==   null          // false  null      ==   undefined     // true  " \t\r\n" ==   0             // true</pre></li>     <li>Java里有趣的自动打包(auto boxing)和IntegerCache:<pre>Integer foo = 1000;  Integer bar = 1000;    foo = bar; // true  foo == bar; // false    //然而,如果 foo 和 bar 的值介于 127 和 -128 之间,  //情况会发生变化:    Integer foo = 42;  Integer bar = 42;    foo = bar; // true  foo == bar; // true</pre> <h3>解释</h3> <p>在Java源代码里你很快能翻出下面的内容:</p> <pre>/**   * Returns a <tt>Integer</tt> instance representing the specified   * <tt>int</tt> value.   * If a new <tt>Integer</tt> instance is not required, this method   * should generally be used in preference to the constructor   * {@link #Integer(int)}, as this method is likely to yield   * significantly better space and time performance by caching   * frequently requested values.   *   * @param  i an <code>int</code> value.   * @return a <tt>Integer</tt> instance representing <tt>i</tt>.   * @since  1.5   */  public static Integer valueOf(int i) {      if (i >= -128 && i</pre> <p><strong>注意;</strong> IntegerCache.high 的值,如果你不设置它,缺省是 127。</p> <p>自动打包(auto boxing)过程中发生的事情是:除非显式的创建:比如 foo = new Integer(42) ,否则 foo 和 bar 都是从缓存中取出的整数,当比较值时,它们会返回 true。正确的比较Integer的方式是使用 .equals 。</p> </li>     <li>在Java里:<pre>try {      return true;  } finally {      return false;  }</pre> <p>在Python和JavaScript也可以这样写。这样写的结果是什么?当作一个保留的问题考考大家吧。</p> </li>     <li>C++的模板可以做很多奇怪的事情,最好的例证是 <a href="/misc/goto?guid=4958321977473740188" target="_blank">“多维模拟排版”</a>,你可以用模板来计算“画出的”形状。下面是一段计算一个3×3矩形的有效的C++代码:<pre>#include"analogliterals.hpp"  using namespace analog_literals::symbols;              unsigned int c = ( o-----o                               |     !                               !     !                               !     !                               o-----o ).area;</pre> <p>这还有一个3D立方体的例子:</p> <pre>  assert( ( o-------------o              |L             \              | L             \              |  L             \              |   o-------------o              |   !             !              !   !             !              o   |             !               L  |             !                L |             !                 L|             !                  o-------------o ).volume == ( o-------------o                                                |             !                                                !             !                                                !             !                                                o-------------o ).area * int(I-------------I) );</pre></li>     <li>PHP处理数字字符<pre>"01a4" != "001a4"</pre> <p>如果两个字符串来的字符数不相等,它们不可能相等。前面的零非常重要,因为他们是字符而不是数字。</p> <pre>"01e4" == "001e4"</pre> <p>PHP不太喜欢字符,它会寻找任何的借口来把你提供的值当作数字。把这些十六进制的字符串稍微做些改动,PHP马上认为它们不再是字符,而是数字。 它们是按科学计数法写的数字(PHP并不在意你是否用了引号),它们是相等的,因为对于数字来说,前面的零是可以忽略的。更强化这个事实的例子是,你会发 现PHP认为 “01e4″ == “10000″ 是正确的,因为作为数字,它们的值是相当的。这是一个被特别提醒的语言特征,原因就是它不太合理。</p> </li>     <li>在一些没有保留字、关键字的语言里,例如PL/I, 你可以写出这样有趣而且合法的代码语句:<pre>IF IF THEN THEN = ELSE ELSE ELSE = THEN</pre> <p>(IF, THEN, ELSE 都是变量名),或</p> <pre>IF IF THEN THEN ELSE ELSE</pre> <p>(IF 是变量,THEN 和 ELSE 都是函数)</p> </li>     <li>JavaScript里八进制的转换:<pre>parseInt('06') // 6  parseInt('07') // 7  parseInt('08') // 0  parseInt('09') // 0  parseInt('10') // 10</pre></li>     <li>在C语言里,你可以把 do/while 语句和 switch 交织在一起,下面就是一个使用这种方法的内存拷贝的例子:<pre>void duff_memcpy( char* to, char* from, size_t count ) {      size_t n = (count+7)/8;      switch( count%8 ) {      case 0: do{ *to++ = *from++;      case 7:     *to++ = *from++;      case 6:     *to++ = *from++;      case 5:     *to++ = *from++;      case 4:     *to++ = *from++;      case 3:     *to++ = *from++;      case 2:     *to++ = *from++;      case 1:     *to++ = *from++;              }while(--n>0);      }  }</pre></li>     <li>在Python中:<pre>>>> x=5  >>> 1>> 1</pre></li>     <li>在Java中:<pre>int[] numbers() {    return null;  }</pre> <p>可以被写成:</p> <pre>int numbers() [] {    return null;  }</pre></li>    </ol>    <p>如果你觉得还没看够,请到<a href="/misc/goto?guid=4958321976670321114" target="_blank">这里</a>,还有更多。</p> 本文转载自: 外刊IT评论    <a href="/misc/goto?guid=4958183272158702965" rel="nofollow" target="_blank">http://www.aqee.net/</a>