Wednesday, November 30, 2011

programming languages and their statements

最近看了《黑客与画家》,其中有一点感受就是语言的能力是有差异的,姑且不去研究究竟谁是老大,了解各种语言的优劣总是没有害处的,正巧在hack news上看到了这篇文章programming languages and their statements,关于各个语言以及对这些语言的陈述,同时还可以对各种语言进行对比。按照字母序罗列了50种语言,如下:

programming languages



  1. Ada

  2. Agda

  3. APL

  4. Assembler

  5. AWK

  6. C

  7. C#

  8. C++

  9. Clojure

  10. Cobol

  11. Common Lisp

  12. Coq

  13. D

  14. Delphi

  15. Eiffel

  16. ELisp

  17. Erlang

  18. F#

  19. Factor

  20. Forth

  21. Fortran

  22. Go

  23. Groovy

  24. Haskell

  25. Haxe

  26. Io

  27. J

  28. Java

  29. Javascript

  30. Lua

  31. Mathematica

  32. Matlab

  33. Mozart-Oz

  34. Objective C

  35. O'Caml

  36. Pascal

  37. Perl

  38. PHP

  39. Prolog

  40. Python

  41. R

  42. REBOL

  43. Ruby

  44. Scala

  45. Scheme

  46. Shell

  47. Smalltalk

  48. Standard ML

  49. TCL

  50. Visual Basic 


每种语言对应的页面大概如下:

  1. 说明在这系列活动中有多少人响应了该语言

  2. 罗列该语言在哪些statement的rank较高或者较低,各10条,从这些statements中我们很容易了解该语言适合做什么以及不适合做什么

  3. 然后列出和这种语言最像以及最不像的5种语言,方面比较

  4. 提供和某种语言比较的功能,选择某种语言既可以比较

  5. 关于这个语言的所有陈述(排名越高说明赞成的人越多),从这个可以进一步了解该语言,当然更好的了解语言的方法应该是去使用它,但是可能不是每个人都能够这么做


举个python的例子,如下

Python




Based on 87822 responses from 7295 people, we've built up the following picture of Python











All statements



  1. I would use this language for casual scripting

  2. This language would be good for teaching children to write software

  3. This language is good for beginners

  4. Code written in this language is very readable

  5. I find this language easy to prototype in

  6. I would use this language as a scripting language embedded inside a larger application

  7. It is easy to tell at a glance what code in this language does

  8. This language excels at text processing

  9. I would use this language to write a command-line app

  10. This language is well suited for an agile development approach using short iterations.

  11. I would use this language for a web project

  12. This language has a good community

  13. This language has a good library distribution mechanism.

  14. This language encourages writing code that is easy to maintain.

  15. Libraries in this language tend to be well documented.

  16. This language has a wide variety of agreed-upon conventions, which are generally adhered to reasonably well, and which increase my productivity

  17. This language is best for very small projects

  18. I would use this language for a desktop GUI project

  19. The resources for learning this language are of high quality

  20. This is a high level language

  21. This language is expressive

  22. I find code written in this language very elegant

  23. This language is good for scientific computing

  24. Third-party libraries are readily available, well-documented, and of high quality

  25. I often write things in this language with the intent of rewriting them in something else later

  26. This language encourages writing reusable code.

  27. I use this language out of choice

  28. I usually use this language on solo projects

  29. I enjoy using this language

  30. This language has well-organized libraries with consistent, carefully thought-out interfaces

  31. I can imagine this will be a popular language in twenty years time

  32. This language is very flexible

  33. I can imagine using this language in my day job

  34. I rarely have difficulty abstracting patterns I find in my code

  35. This language is well documented

  36. I regularly use this language

  37. There are many good open-source tools for this language

  38. I would use this language for writing server programs

  39. There is a wide variety of open source code written in this language

  40. I would like to write more of this language than I currently do

  41. It is easy to debug programs written in this language when it goes wrong

  42. This language excels at symbolic manipulation

  43. This language has unusual features that I often miss when using other languages

  44. Programs written in this language tend to play well with others

  45. When I run into problems my colleagues can provide me with immediate help with this language

  46. Code written in this language tends to be terse

  47. I still discover new features of this language on a fairly regular basis

  48. This language has a very coherent design

  49. Code written in this language will usually run in all the major implementations if it runs in one of them.

  50. I would list this language on my resume

  51. I would recommend most programmers learn this language, regardless of whether they have a specific need for it

  52. This language is good for numeric computing

  53. I usually use this language on projects with many other members

  54. This language is best for very large projects

  55. This language has a very rigid idea of how things should be done

  56. This language has a very dogmatic community

  57. This language is likely to have a strong influence on future languages

  58. When I write code in this language I can be very sure it is correct

  59. Learning this language significantly changed how I use other languages.

  60. I know many other people who use this language

  61. I know this language well

  62. Code written in this language tends to be very reliable

  63. There are many good tools for this language

  64. Learning this language improved my ability as a programmer

  65. This language is easier to use for it's problem domain by removing unneeded expressiveness (such as not being Turing complete).

  66. This language is good for distributed computing

  67. I would use this language for mobile applications

  68. This language has a high quality implementation

  69. This language is likely to be a passing fad

  70. I use many applications written in this language

  71. If this language didn't exist, I would have trouble finding a satisfactory replacement

  72. This language is large

  73. This language matches it's problem domain particularly well.

  74. It's unusual for me to discover unfamiliar features

  75. This is a mainstream language

  76. This language has many features which feel "tacked on"

  77. I find it easy to write efficient code in this language

  78. This language is built on a small core of orthogonal features

  79. This language is likely to be around for a very long time

  80. The semantics of this language are much different than other languages I know.

  81. This language excels at concurrency

  82. This language is minimal

  83. There are many good commercial tools for this language

  84. I use a lot of code written in this language which I really don't want to have to make changes to

  85. I would use this language for writing embedded programs

  86. This language is frequently used for applications it isn't suitable for

  87. If my code in this language successfully compiles, there is a good chance my code is correct.

  88. I would use this language for writing programs for an embedded hardware platform

  89. This language allows me to write programs where I know exactly what they are doing under the hood

  90. I enjoy playing with this language but would never use it for "real code"

  91. This language has a niche in which it is great

  92. Programs written in this language will usually work in future versions of the language

  93. It is too easy to write code in this language that looks like it does one thing but actually does something else

  94. I am sometimes embarrassed to admit to my peers that I know this language

  95. Code written in this language tends to be verbose

  96. Programs written in this language tend to be efficient

  97. This language is suitable for real-time applications

  98. I am reluctant to admit to knowing this language

  99. I learned this language early in my career as a programmer

  100. The thought that I may still be using this language in twenty years time fills me with dread

  101. This language has a strong static type system

  102. This is a low level language

  103. Developers who primarily use this language often burn out after a few years

  104. This language has a niche outside of which I would not use it

  105. I often feel like I am not smart enough to write this language

  106. This language makes it easy to shoot yourself in the foot

  107. This language has an annoying syntax

  108. There is a lot of accidental complexity when writing code in this language

  109. I often get angry when writing code in this language

  110. Writing code in this language is a lot of work

  111. This language is unusually bad for beginners





Saturday, November 26, 2011

Could Not Find HelloAndroid.apk! win7

开始折腾下android,第一个遇到的问题自然要记下

按照官方的教程一步一步走来,helloword居然不能work,错误如下
Could Not Find HelloAndroid.apk!

这里这里都搜到了相关问题的描述,不过在我这里不work,因为我没有相应的路径,不过欣慰的是他的部分方法我拿过来用就ok了
把我的市区设置成美帝的市区时区,然后run helloworld 就ok了,随后既可以将市区时区更改回来:)

 

Thursday, November 3, 2011

bash 条件表达式 'test' '[' '[['

一直以来对test、[、和[[没有深研究,今天代码被挑刺了,就在这个几点上,因此决定好好在研究一下,翻了几个blog,同时又看了Bash Reference,本文的文字主要摘自Bash Reference,外加自己的一点解释、以及例子等。

首先看到6.4 Bash Conditional Expressions

Bash Conditional Expressions


开头说到条件表达式通常用于 [[ compound command and the test and [builtin commands.

Expressions may be unary or binary. Unary expressions are often used to examine the status of a file. There are string operators and numeric comparison operators as well(用于检查文件的status,以及字符串操作和算术比较). If the fileargument to one of the primaries is of the form/dev/fd/N, then file descriptor N is checked. If the fileargument to one of the primaries is one of/dev/stdin,/dev/stdout, or/dev/stderr, file descriptor 0, 1, or 2, respectively, is checked.

When used with ‘[[’, The ‘<’ and ‘>’ operators sort lexicographically using the current locale(此时基于字典序).

Unless otherwise specified, primaries that operate on files follow symbolic links and operate on the target of the link, rather than the link itself.

文件
-a file
True if file exists.
-b file
True if file exists and is a block special file.
-c file
True if file exists and is a character special file.
-d file
True if file exists and is a directory.
-e file
True if file exists.
-f file
True if file exists and is a regular file.
-g file
True if file exists and its set-group-id bit is set.
-h file
True if file exists and is a symbolic link.
-k file
True if file exists and its "sticky" bit is set.
-p file
True if file exists and is a named pipe (FIFO).
-r file
True if file exists and is readable.
-s file
True if file exists and has a size greater than zero.
-t fd
True if file descriptor fd is open and refers to a terminal.
-u file
True if file exists and its set-user-id bit is set.
-w file
True if file exists and is writable.
-x file
True if file exists and is executable.
-O file
True if file exists and is owned by the effective user id.
-G file
True if file exists and is owned by the effective group id.
-L file
True if file exists and is a symbolic link.
-S file
True if file exists and is a socket.
-N file
True if file exists and has been modified since it was last read.
file1 -nt file2
True if file1 is newer (according to modification date) than file2, or if file1 exists and file2 does not.
file1 -ot file2
True if file1 is older than file2, or if file2 exists and file1 does not.
file1 -ef file2
True if file1 and file2 refer to the same device and inode numbers.
操作符
-o optname
True if shell option optnameis enabled. The list of options appears in the description of the-ooption to the set builtin (see The Set Builtin).
字符串
-z string
True if the length of string is zero.
-n string
string
True if the length of string is non-zero.
string1 == string2
string1 = string2
True if the strings are equal.=’ should be used with the test command for posix conformance.
string1 != string2
True if the strings are not equal.
string1 < string2
True if string1 sorts before string2 lexicographically.
string1 > string2
True if string1 sorts after string2 lexicographically.
算术
arg1 OP arg2
OPis one of ‘-eq’, ‘-ne’, ‘-lt’, ‘-le’, ‘-gt’, or ‘-ge
’. These arithmetic binary operators return true if arg1 is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to arg2, respectively. Arg1 and arg2 may be positive or negative integers(必须是整数).
下面就依次来看看'[['和'test' '['

先看'[['


位于3.2.4.2 Conditional Constructs

[[...]]

          [[ expression ]]

Return a status of 0 or 1 depending on the evaluation of the conditional expression expression. Expressions are composed of the primaries described below in Bash Conditional Expressions. Word splitting and filename expansion are not performed on the words between the ‘[[’ and ‘]]’; tilde expansion, parameter and variable expansion, arithmetic expansion, command substitution, process substitution, and quote removal are performed. Conditional operators such as ‘-f’ must be unquoted to be recognized as primaries.

When used with ‘[[’, The ‘<’ and ‘>’ operators sort lexicographically using the current locale.

When the ‘==’ and ‘!=’ operators are used, the string to the right of the operator is considered a pattern and matched according to the rules described below in Pattern Matching. If the shell option nocasematch (see the description of shopt in The Shopt Builtin) is enabled, the match is performed without regard to the case of alphabetic characters. The return value is 0 if the string matches (‘==’) or does not match (‘!=’)the pattern, and 1 otherwise. Any part of the pattern may be quoted to force it to be matched as a string.

An additional binary operator, ‘=~’, is available, with the same precedence as ‘==’ and ‘!=’. When it is used, the string to the right of the operator is considered an extended regular expression and matched accordingly (as in regex3)). The return value is 0 if the string matches the pattern, and 1 otherwise. If the regular expression is syntactically incorrect, the conditional expression's return value is 2. If the shell option nocasematch (see the description of shopt in The Shopt Builtin) is enabled, the match is performed without regard to the case of alphabetic characters. Any part of the pattern may be quoted to force it to be matched as a string. Substrings matched by parenthesized subexpressions within the regular expression are saved in the array variable BASH_REMATCH. The element of BASH_REMATCH with index 0 is the portion of the string matching the entire regular expression. The element of BASH_REMATCH with index n is the portion of the string matching the nth parenthesized subexpression.

Expressions may be combined using the following operators, listed in decreasing order of precedence:

( expression )
Returns the value of expression. This may be used to override the normal precedence of operators.
! expression
True if expression is false.
expression1 && expression2
True if both expression1 and expression2 are true.
expression1 || expression2
True if either expression1 or expression2 is true.
The && and || operators do not evaluate expression2 if the value of expression1 is sufficient to determine the return value of the entire conditional expression.


再来看 'test'和'['


位于4.1 Bourne Shell Builtins

test和[是等价的,不过用的时候形式不一样如if  test expression,和if [ expression ]

Evaluate a conditional expression expr. Each operator and operand must be a separate argument. Expressions are composed of the primaries described below in Bash Conditional Expressions. testdoes not accept any options, nor does it accept and ignore an argument of--as signifying the end of options.When the [ form is used, the last argument to the command must be a ].Expressions may be combined using the following operators, listed in decreasing order of precedence. The evaluation depends on the number of arguments; see below.
! expr
True if expr is false.
( expr )
Returns the value of expr. This may be used to override the normal precedence of operators.
expr1 -a expr2
True if both expr1 and expr2 are true.
expr1 -o expr2
True if either expr1 or expr2 is true.
The test and [ builtins evaluate conditional expressions using a set of rules based on the number of arguments.这里把test其实是一个函数,根据不同的参数个数来判断出结果

0 arguments
The expression is false.
1 argument
The expression is true if and only if the argument is not null.
2 arguments
If the first argument is ‘!’, the expression is true if and only if the second argument is null. If the first argument is one of the unary conditional operators (see Bash Conditional Expressions), the expression is true if the unary test is true. If the first argument is not a valid unary operator, the expression is false.
3 arguments
If the second argument is one of the binary conditional operators (see Bash Conditional Expressions), the result of the expression is the result of the binary test using the first and third arguments as operands. The ‘-a’ and ‘-o’ operators are considered binary operators when there are three arguments. If the first argument is ‘!’, the value is the negation of the two-argument test using the second and third arguments. If the first argument is exactly ‘(’ and the third argument is exactly ‘)’, the result is the one-argument test of the second argument. Otherwise, the expression is false.
4 arguments
If the first argument is ‘!’, the result is the negation of the three-argument expression composed of the remaining arguments. Otherwise, the expression is parsed and evaluated according to precedence using the rules listed above.
5 or more arguments
The expression is parsed and evaluated according to precedence using the rules listed above.

[ ]和[[ ]]的区别


这两者的大部分功能都是一样的,但是后者[[]]比前者[]更加完善。

1.[[有扩展功能,通配符匹配(==和!=)和正则表达式匹配(=~),这在上面讲解[[的时候专门有两个段落说明

==和!=的右操作符为pattern进行3.5.8.1 Pattern Matching

这个时候,假如要比较的右侧字符串中有特殊字符如*,则需要用"引起来

[shell]
$ [[ "GFW" == G*W ]]
$ echo $?
0
$ [[ "GFW" == "G*W" ]]
$ echo $?
1
[/shell]

=~的右操作符作为正则表达式,这里正则的引号可用可不用

[shell]
$ [[ "GFW" =~ "G*W" ]]
$ echo $?
0
$ [[ "GFW" =~ 'G*W' ]]
$ echo $?
0
$ [[ "GFW" =~ G*W ]]
$ echo $?
0
[/shell]

2.[的二元操作符的右操作符(通常是某变量)不能为空(null),当然可以通过加引号来解决

首先使用[如下:

[shell]
$ [ "GFW" == $fuck  ]
bash: [: GFW: unary operator expected
[/shell]

就会报错,而加了引号就没有问题了,这个时候当作一个空字符串处理

[shell]
$ [ "GFW" == "$fuck" ]
$ echo $?
1
[/shell]

假如是使用[[就不存在该问题,加不加引号都ok,如下:

[shell]
$ [[ "GFW" == $fuck ]]
$ echo $?
1
$ [[ "GFW" == "$fuck" ]]
$ echo $?
1
[/shell]

另外还有个(( ))


((...))

          (( expression ))

The arithmetic expression is evaluated according to the rules described below (see Shell Arithmetic). If the value of the expression is non-zero, the return status is 0; otherwise the return status is 1. This is exactly equivalent to
          let "expression"

See Bash Builtins, for a full description of the let builtin.

Shell Arithmetic如下

The shell allows arithmetic expressions to be evaluated, as one of the shell expansions or by the letand the

-ioption to the declarebuiltins.

Evaluation is done in fixed-width integers with no check for overflow, though division by 0 is trapped and flagged as an error. The operators and their precedence, associativity, and values are the same as in the C language. The following list of operators is grouped into levels of equal-precedence operators. The levels are listed in order of decreasing precedence.

id++ id--
variable post-increment and post-decrement
++id --id
variable pre-increment and pre-decrement
- +
unary minus and plus
! ~
logical and bitwise negation
**
exponentiation
* / %
multiplication, division, remainder
+ -
addition, subtraction
<< >>
left and right bitwise shifts
<= >= < >
comparison
== !=
equality and inequality
&
bitwise AND
^
bitwise exclusive OR
|
bitwise OR
&&
logical AND
||
logical OR
expr ? expr : expr
conditional operator
= *= /= %= += -= <<= >>= &= ^= |=
assignment
expr1 , expr2
comma
Shell variables are allowed as operands; parameter expansion is performed before the expression is evaluated. Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax. A shell variable that is null or unset evaluates to 0 when referenced by name without using the parameter expansion syntax. The value of a variable is evaluated as an arithmetic expression when it is referenced, or when a variable which has been given the integerattribute using ‘declare -i’ is assigned a value. A null value evaluates to 0. A shell variable need not have its integer attribute turned on to be used in an expression.

Constants with a leading 0 are interpreted as octal numbers. A leading ‘0x’ or ‘0X’ denotes hexadecimal. Otherwise, numbers take the form [base#]n, where base is a decimal number between 2 and 64 representing the arithmetic base, and n is a number in that base. If base#is omitted, then base 10 is used. The digits greater than 9 are represented by the lowercase letters, the uppercase letters, ‘@’, and ‘_’, in that order. If base is less than or equal to 36, lowercase and uppercase letters may be used interchangeably to represent numbers between 10 and 35.

Operators are evaluated in order of precedence. Sub-expressions in parentheses are evaluated first and may override the precedence rules above.