summaryrefslogtreecommitdiffhomepage
path: root/zh-cn
diff options
context:
space:
mode:
Diffstat (limited to 'zh-cn')
-rw-r--r--zh-cn/bash-cn.html.markdown148
-rw-r--r--zh-cn/brainfuck-cn.html.markdown70
-rw-r--r--[-rwxr-xr-x]zh-cn/c-cn.html.markdown394
-rw-r--r--zh-cn/clojure-cn.html.markdown368
-rw-r--r--zh-cn/clojure-macro-cn.html.markdown152
-rw-r--r--zh-cn/coffeescript-cn.html.markdown101
-rw-r--r--zh-cn/common-lisp-cn.html.markdown624
-rw-r--r--zh-cn/csharp-cn.html.markdown798
-rw-r--r--zh-cn/css-cn.html.markdown212
-rw-r--r--[-rwxr-xr-x]zh-cn/elisp-cn.html.markdown4
-rw-r--r--zh-cn/elixir-cn.html.markdown399
-rw-r--r--[-rwxr-xr-x]zh-cn/git-cn.html.markdown4
-rw-r--r--zh-cn/go-cn.html.markdown286
-rw-r--r--[-rwxr-xr-x]zh-cn/haskell-cn.html.markdown0
-rw-r--r--[-rwxr-xr-x]zh-cn/java-cn.html.markdown2
-rw-r--r--[-rwxr-xr-x]zh-cn/javascript-cn.html.markdown2
-rw-r--r--zh-cn/lua-cn.html.markdown413
-rw-r--r--zh-cn/perl-cn.html.markdown152
-rw-r--r--[-rwxr-xr-x]zh-cn/php-cn.html.markdown6
-rw-r--r--[-rwxr-xr-x]zh-cn/python-cn.html.markdown277
-rw-r--r--zh-cn/r-cn.html.markdown541
-rw-r--r--zh-cn/racket-cn.html.markdown608
-rw-r--r--zh-cn/ruby-cn.html.markdown8
-rw-r--r--zh-cn/scala-cn.html.markdown1
-rw-r--r--zh-cn/visualbasic-cn.html.markdown274
25 files changed, 5609 insertions, 235 deletions
diff --git a/zh-cn/bash-cn.html.markdown b/zh-cn/bash-cn.html.markdown
new file mode 100644
index 00000000..6afa659a
--- /dev/null
+++ b/zh-cn/bash-cn.html.markdown
@@ -0,0 +1,148 @@
+---
+category: tool
+tool: bash
+contributors:
+ - ["Max Yankov", "https://github.com/golergka"]
+ - ["Darren Lin", "https://github.com/CogBear"]
+ - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"]
+translators:
+ - ["Chunyang Xu", "https://github.com/XuChunyang"]
+filename: LearnBash-cn.sh
+lang: zh-cn
+---
+
+Bash 是一个为 GNU 计划编写的 Unix shell,是 Linux 和 Mac OS X 下的默认 shell。
+以下大多数例子可以作为脚本的一部分运行,也可直接在 shell 下交互执行。
+
+[更多信息](http://www.gnu.org/software/bash/manual/bashref.html)
+
+```bash
+#!/bin/bash
+# 脚本的第一行叫 shebang,用来告知系统如何执行该脚本:
+# 参见: http://en.wikipedia.org/wiki/Shebang_(Unix)
+# 如你所见,注释以 # 开头,shebang 也是注释。
+
+# 显示 “Hello world!”
+echo Hello, world!
+
+# 每一句指令以换行或分号隔开:
+echo 'This is the first line'; echo 'This is the second line'
+
+# 声明一个变量:
+VARIABLE="Some string"
+
+# 下面是错误的做法:
+VARIABLE = "Some string"
+# Bash 会把 VARIABLE 当做一个指令,由于找不到该指令,因此这里会报错。
+
+
+# 使用变量:
+echo $VARIABLE
+echo "$VARIABLE"
+echo '$VARIABLE'
+# 当你赋值 (assign) 、导出 (export),或者以其他方式使用变量时,变量名前不加 $。
+# 如果要使用变量的值, 则要加 $。
+# 注意: ' (单引号) 不会展开变量(即会屏蔽掉变量)。
+
+
+# 在变量内部进行字符串代换
+echo ${VARIABLE/Some/A}
+# 会把 VARIABLE 中首次出现的 "some" 替换成 “A”。
+
+# 内置变量:
+# 下面的内置变量很有用
+echo "Last program return value: $?"
+echo "Script's PID: $$"
+echo "Number of arguments: $#"
+echo "Scripts arguments: $@"
+echo "Scripts arguments separeted in different variables: $1 $2..."
+
+# 读取输入:
+echo "What's your name?"
+read NAME # 这里不需要声明新变量
+echo Hello, $NAME!
+
+# 通常的 if 结构看起来像这样:
+# 'man test' 可查看更多的信息
+if [ $NAME -ne $USER ]
+then
+ echo "Your name is you username"
+else
+ echo "Your name isn't you username"
+fi
+
+# 根据上一个指令执行结果决定是否执行下一个指令
+echo "Always executed" || echo "Only executed if first command fail"
+echo "Always executed" && echo "Only executed if first command does NOT fail"
+
+# 表达式的格式如下:
+echo $(( 10 + 5 ))
+
+# 与其他编程语言不同的是,bash 运行时依赖上下文。比如,使用 ls 时,列出当前目录。
+ls
+
+# 指令可以带有选项:
+ls -l # 列出文件和目录的详细信息
+
+# 前一个指令的输出可以当作后一个指令的输入。grep 用来匹配字符串。
+# 用下面的指令列出当前目录下所有的 txt 文件:
+ls -l | grep "\.txt"
+
+# 重定向可以到输出,输入和错误输出。
+python2 hello.py < "input.in"
+python2 hello.py > "output.out"
+python2 hello.py 2> "error.err"
+# > 会覆盖已存在的文件, >> 会以累加的方式输出文件中。
+
+# 一个指令可用 $( ) 嵌套在另一个指令内部:
+# 以下的指令会打印当前目录下的目录和文件总数
+echo "There are $(ls | wc -l) items here."
+
+# Bash 的 case 语句与 Java 和 C++ 中的 switch 语句类似:
+case "$VARIABLE" in
+ # 列出需要匹配的字符串
+ 0) echo "There is a zero.";;
+ 1) echo "There is a one.";;
+ *) echo "It is not null.";;
+esac
+
+# 循环遍历给定的参数序列:
+# 变量$VARIABLE 的值会被打印 3 次。
+# 注意 ` ` 和 $( ) 等价。seq 返回长度为 3 的数组。
+for VARIABLE in `seq 3`
+do
+ echo "$VARIABLE"
+done
+
+# 你也可以使用函数
+# 定义函数:
+function foo ()
+{
+ echo "Arguments work just like script arguments: $@"
+ echo "And: $1 $2..."
+ echo "This is a function"
+ return 0
+}
+
+# 更简单的方法
+bar ()
+{
+ echo "Another way to declare functions!"
+ return 0
+}
+
+# 调用函数
+foo "My name is" $NAME
+
+# 有很多有用的指令需要学习:
+tail -n 10 file.txt
+# 打印 file.txt 的最后 10 行
+head -n 10 file.txt
+# 打印 file.txt 的前 10 行
+sort file.txt
+# 将 file.txt 按行排序
+uniq -d file.txt
+# 报告或忽略重复的行,用选项 -d 打印重复的行
+cut -d ',' -f 1 file.txt
+# 打印每行中 ',' 之前内容
+```
diff --git a/zh-cn/brainfuck-cn.html.markdown b/zh-cn/brainfuck-cn.html.markdown
new file mode 100644
index 00000000..a6f3fa09
--- /dev/null
+++ b/zh-cn/brainfuck-cn.html.markdown
@@ -0,0 +1,70 @@
+---
+language: brainfuck
+lang: zh-cn
+contributors:
+ - ["Prajit Ramachandran", "http://prajitr.github.io/"]
+ - ["Mathias Bynens", "http://mathiasbynens.be/"]
+translators:
+ - ["lyuehh", "https://github.com/lyuehh"]
+---
+
+Brainfuck 是一个极小的只有8个指令的图灵完全的编程语言。
+
+```
+除"><+-.,[]"之外的的任何字符都会被忽略 (不包含双引号)。
+
+Brainfuck 包含一个有30,000个单元为0的数组,和
+一个数据指针指向当前的单元。
+
+8个指令如下:
++ : 指针指向的单元的值加1
+- : 指针指向的单元的值减1
+> : 将指针移动到下一个单元(右边的元素)
+< : 将指针移动到上一个单元(左边的元素)
+. : 打印当前单元的内容的ASCII值 (比如 65 = 'A').
+, : 读取一个字符到当前的单元
+[ : 如果当前单元的值是0,则向后调转到对应的]处
+] : 如果当前单元的值不是0,则向前跳转到对应的[处
+
+[ 和 ] 组成了一个while循环。很明显,它们必须配对。
+
+让我们看一些基本的brainfuck 程序。
+
+++++++ [ > ++++++++++ < - ] > +++++ .
+
+这个程序打印字母'A'。首先,它把 #1 增加到6,使用它来作为循环条件,
+然后,进入循环,将指针移动到 #2 ,将 #2 的值增加到10,然后
+移动回 #1,将单元 #1 的值减1,然后继续。循环共进行了6次。
+
+这时,我们在 #1,它的值为0,#2 的值为60,我们移动到
+#2,将 #2 的内容加上5,然后将 #2 的内容打印出来,65在
+ASCII中表示'A', 所以'A'就会被打印出来。
+
+
+, [ > + < - ] > .
+
+这个程序从用户的输入中读取一个字符,然后把它复制到 #1。
+然后我们开始一个循环,移动到 #2,将 #2 的值加1,再移动回 #1,将 #1
+的值减1,直到 #1的值为0,这样 #2 里就保存了 #1 的旧值,循环结束时我们
+在 #1,这时我们移动到 #2,然后把字符以ASCII打印出来。
+
+而且要记住的一点就是,空格在这里只是为了可读性,你可以将他们写成这样:
+
+,[>+<-]>.
+
+试着思考一下这段程序是干什么的:
+
+,>,< [ > [ >+ >+ << -] >> [- << + >>] <<< -] >>
+
+这段程序从输入接收2个参数,然后将他们相乘。
+
+先读取2个输入,然后开始外层循环,以 #1 作为终止条件,然后将指针移动到
+#2,然后开始 #2 的内层循环,将 #3 加1。但是这里有一个小问题,在内层
+循环结束的时候,#2 的值是0了,那么下次执行外层循环的时候,就有问题了。
+为了解决这个问题,我们可以增加 #4 的值,然后把 #4 的值复制到 #2,
+最后结果就保存在 #3 中了。
+```
+好了这就是brainfuck了。也没那么难,是吧?为了好玩,你可以写你自己的
+brainfuck程序,或者用其他语言写一个brainfuck的解释器,解释器非常容易
+实现,但是如果你是一个自虐狂的话,你可以尝试用brainfuck写一个brainfuk的
+解释器。
diff --git a/zh-cn/c-cn.html.markdown b/zh-cn/c-cn.html.markdown
index b4bff8fc..223f6e35 100755..100644
--- a/zh-cn/c-cn.html.markdown
+++ b/zh-cn/c-cn.html.markdown
@@ -5,37 +5,52 @@ contributors:
- ["Adam Bard", "http://adambard.com/"]
translators:
- ["Chenbo Li", "http://binarythink.net/"]
+ - ["Jakukyo Friel", "http://weakish.github.io"]
lang: zh-cn
---
C语言在今天仍然是高性能计算的主要选择。
-C大概是大多数程序员用到的最接近底层的语言了,但是C语言本身不仅可以用来提升程序运行的速度
-注意看看C语言的文档,你就会知道C语言在内存管理方面的强大也是其他语言无法比拟的。
+C大概是大多数程序员用到的最接近底层的语言了,C语言原生的速度就很高了,但是别忘了C的手动内存管理,它会让你将性能发挥到极致。
```c
-// 用“//”来实现单行注释
+// 单行注释以//开始。(仅适用于C99或更新的版本。)
/*
-多行注释是这个样子的
+多行注释是这个样子的。(C89也适用。)
*/
+// 常数: #define 关键词
+#define DAYS_IN_YEAR 365
+
+// 以枚举的方式定义常数
+enum days {SUN = 1, MON, TUE, WED, THU, FRI, SAT};
+// MON自动被定义为2,TUE被定义为3,以此类推。
+
// 用#include来导入头文件
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
-// 函数的标签(signature)应该放在.h文件中,并且引入到程序顶部
-// 也可以直接放到你的.c文件的最上面
-void function_1();
-void function_2();
+// <尖括号>间的文件名是C标准库的头文件。
+// 标准库以外的头文件,使用双引号代替尖括号。
+#include "my_header.h"
+
+// 函数的签名可以事先在.h文件中定义,
+// 也可以直接在.c文件的头部定义。
+void function_1(char c);
+void function_2(void);
-// c程序的入口是一个返回值为int型的函数,名字叫做main
+// 如果函数出现在main()之后,那么必须在main()之前
+// 先声明一个函数原型
+int add_two_ints(int x1, int x2); // 函数原型
+
+// 你的程序的入口是一个返回值为整型的main函数
int main() {
-// 用printf来实现标准输出,这种输出也可以用格式来控制
-// %d 代表一个整数, \n 代表一个新行
-printf("%d\n", 0); // => 输出 0
+// 用printf打印到标准输出,可以设定格式,
+// %d 代表整数, \n 代表换行
+printf("%d\n", 0); // => 打印 0
// 所有的语句都要以分号结束
///////////////////////////////////////
@@ -69,18 +84,29 @@ double x_double = 0.0;
// 整数类型也可以有无符号的类型表示。这样这些变量就无法表示负数
// 但是无符号整数所能表示的范围就可以比原来的整数大一些
-unsigned char ux_char;
unsigned short ux_short;
unsigned int ux_int;
unsigned long long ux_long_long;
+// 单引号内的字符是机器的字符集中的整数。
+'0' // => 在ASCII字符集中是48
+'A' // => 在ASCII字符集中是65
+
// char类型一定会占用1个字节,但是其他的类型却会因具体机器的不同而各异
// sizeof(T) 可以返回T类型在运行的机器上占用多少个字节
// 这样你的代码就可以在各处正确运行了
-// 比如
-printf("%lu\n", sizeof(int)); // => 4 (字长为4的机器上)
-
-// 数组必须要在开始被初始化为特定的长度
+// sizeof(obj)返回表达式(变量、字面量等)的尺寸
+printf("%zu\n", sizeof(int)); // => 4 (大多数的机器字长为4)
+
+// 如果`sizeof`的参数是一个表达式,那么这个参数不会被演算(VLA例外,见下)
+// 它产生的值是编译期的常数
+int a = 1;
+// size_t是一个无符号整型,表示对象的尺寸,至少2个字节
+size_t size = sizeof(a++); // a++ 不会被演算
+printf("sizeof(a++) = %zu where a = %d\n", size, a);
+// 打印 "sizeof(a++) = 4 where a = 1" (在32位架构上)
+
+// 数组必须要被初始化为具体的长度
char my_char_array[20]; // 这个数组占据 1 * 20 = 20 个字节
int my_int_array[20]; // 这个数组占据 4 * 20 = 80 个字节
// (这里我们假设字长为4)
@@ -89,48 +115,83 @@ int my_int_array[20]; // 这个数组占据 4 * 20 = 80 个字节
// 可以用下面的方法把数组初始化为0:
char my_array[20] = {0};
-// 对数组任意存取就像其他语言的方式 -- 其实是其他的语言像C
+// 索引数组和其他语言类似 -- 好吧,其实是其他的语言像C
my_array[0]; // => 0
// 数组是可变的,其实就是内存的映射!
my_array[1] = 2;
printf("%d\n", my_array[1]); // => 2
+// 在C99 (C11中是可选特性),变长数组(VLA)也可以声明长度。
+// 其长度不用是编译期常量。
+printf("Enter the array size: "); // 询问用户数组长度
+char buf[0x100];
+fgets(buf, sizeof buf, stdin);
+
+// stroul 将字符串解析为无符号整数
+size_t size = strtoul(buf, NULL, 10);
+int var_length_array[size]; // 声明VLA
+printf("sizeof array = %zu\n", sizeof var_length_array);
+
+// 上述程序可能的输出为:
+// > Enter the array size: 10
+// > sizeof array = 40
+
// 字符串就是以 NUL (0x00) 这个字符结尾的字符数组,
-// 这个字符可以用'\0'来表示.
-// (在字符串字面值中我们不必输入这个字符,编译器会自动添加的)
+// NUL可以用'\0'来表示.
+// (在字符串字面量中我们不必输入这个字符,编译器会自动添加的)
char a_string[20] = "This is a string";
printf("%s\n", a_string); // %s 可以对字符串进行格式化
-
/*
也许你会注意到 a_string 实际上只有16个字节长.
第17个字节是一个空字符(NUL)
-而第18, 19 和 20 个字符的值是不确定的。
+而第18, 19 和 20 个字符的值是未定义。
*/
printf("%d\n", a_string[16]); // => 0
+// byte #17值为0(18,19,20同样为0)
+
+// 单引号间的字符是字符字面量
+// 它的类型是`int`,而 *不是* `char`
+// (由于历史原因)
+int cha = 'a'; // 合法
+char chb = 'a'; // 同样合法 (隐式类型转换
+
+// 多维数组
+int multi_array[2][5] = {
+ {1, 2, 3, 4, 5},
+ {6, 7, 8, 9, 0}
+ }
+// 获取元素
+int array_int = multi_array[0][2]; // => 3
///////////////////////////////////////
// 操作符
///////////////////////////////////////
-int i1 = 1, i2 = 2; // 多个变量声明的简写
+// 多个变量声明的简写
+int i1 = 1, i2 = 2;
float f1 = 1.0, f2 = 2.0;
-// 算数运算
+int a, b, c;
+a = b = c = 0;
+
+// 算数运算直截了当
i1 + i2; // => 3
i2 - i1; // => 1
i2 * i1; // => 2
-i1 / i2; // => 0 (0.5 会被化整为 0)
+i1 / i2; // => 0 (0.5,但会被化整为 0)
f1 / f2; // => 0.5, 也许会有很小的误差
+// 浮点数和浮点数运算都是近似值
// 取余运算
11 % 3; // => 2
-// 比较操作符我们也很熟悉, 但是有一点,C中没有布尔类型
+// 你多半会觉得比较操作符很熟悉, 不过C中没有布尔类型
// 而是用整形替代
-// 0 就是 false, 其他的就是 true. (比较操作符的返回值则仅有0和1)
+// (C99中有_Bool或bool。)
+// 0为假, 其他均为真. (比较操作符的返回值总是返回0或1)
3 == 2; // => 0 (false)
3 != 2; // => 1 (true)
3 > 2; // => 1
@@ -138,7 +199,14 @@ f1 / f2; // => 0.5, 也许会有很小的误差
2 <= 2; // => 1
2 >= 2; // => 1
-// 逻辑运算符需要作用于整数
+// C不是Python —— 连续比较不合法
+int a = 1;
+// 错误
+int between_0_and_2 = 0 < a < 2;
+// 正确
+int between_0_and_2 = 0 < a && a < 2;
+
+// 逻辑运算符适用于整数
!3; // => 0 (非)
!0; // => 1
1 && 1; // => 1 (且)
@@ -146,6 +214,20 @@ f1 / f2; // => 0.5, 也许会有很小的误差
0 || 1; // => 1 (或)
0 || 0; // => 0
+// 条件表达式 ( ? : )
+int a = 5;
+int b = 10;
+int z;
+z = (a > b) ? a : b; // 10 “若a > b返回a,否则返回b。”
+
+// 增、减
+char *s = "iLoveC"
+int j = 0;
+s[j++]; // "i" 返回s的第j项,然后增加j的值。
+j = 0;
+s[++j]; // => "L" 增加j的值,然后返回s的第j项。
+// j-- 和 --j 同理
+
// 位运算
~0x0F; // => 0xF0 (取反)
0x0F & 0xF0; // => 0x00 (和)
@@ -154,6 +236,13 @@ f1 / f2; // => 0.5, 也许会有很小的误差
0x01 << 1; // => 0x02 (左移1位)
0x02 >> 1; // => 0x01 (右移1位)
+// 对有符号整数进行移位操作要小心 —— 以下未定义:
+// 有符号整数位移至符号位 int a = 1 << 32
+// 左移位一个负数 int a = -1 << 2
+// 移位超过或等于该类型数值的长度
+// int a = 1 << 32; // 假定int32位
+
+
///////////////////////////////////////
// 控制结构
///////////////////////////////////////
@@ -168,17 +257,17 @@ if (0) {
// While循环
int ii = 0;
-while (ii < 10) {
+while (ii < 10) { // 任何非0的值均为真
printf("%d, ", ii++); // ii++ 在取值过后自增
-} // => 输出 "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "
+} // => 打印 "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "
printf("\n");
int kk = 0;
do {
printf("%d, ", kk);
-} while (++kk < 10); // ++kk 先自增,在被取值
-// => 输出 "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "
+} while (++kk < 10); // ++kk 先自增,再被取值
+// => 打印 "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "
printf("\n");
@@ -186,29 +275,55 @@ printf("\n");
int jj;
for (jj=0; jj < 10; jj++) {
printf("%d, ", jj);
-} // => 输出 "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "
+} // => 打印 "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "
printf("\n");
+// *****注意*****:
+// 循环和函数必须有主体部分,如果不需要主体部分:
+int i;
+ for (i = 0; i <= 5; i++) {
+ ; // 使用分号表达主体(null语句)
+}
+
+// 多重分支:switch()
+switch (some_integral_expression) {
+case 0: // 标签必须是整数常量表达式
+ do_stuff();
+ break; // 如果不使用break,控制结构会继续执行下面的标签
+case 1:
+ do_something_else();
+ break;
+default:
+ // 假设 `some_integral_expression` 不匹配任何标签
+ fputs("error!\n", stderr);
+ exit(-1);
+ break;
+ }
+
///////////////////////////////////////
// 类型转换
///////////////////////////////////////
// 在C中每个变量都有类型,你可以将变量的类型进行转换
+// (有一定限制)
-int x_hex = 0x01; // 可以用16进制赋值
+int x_hex = 0x01; // 可以用16进制字面量赋值
// 在类型转换时,数字本身的值会被保留下来
-printf("%d\n", x_hex); // => 输出 1
-printf("%d\n", (short) x_hex); // => 输出 1
-printf("%d\n", (char) x_hex); // => 输出 1
+printf("%d\n", x_hex); // => 打印 1
+printf("%d\n", (short) x_hex); // => 打印 1
+printf("%d\n", (char) x_hex); // => 打印 1
// 类型转换时可能会造成溢出,而且不会抛出警告
-printf("%d\n", (char) 257); // => 1 (char的最大值为255)
+printf("%d\n", (char) 257); // => 1 (char的最大值为255,假定char为8位长)
+
+// 使用<limits.h>提供的CHAR_MAX、SCHAR_MAX和UCHAR_MAX宏可以确定`char`、`signed_char`和`unisigned char`的最大值。
+
// 整数型和浮点型可以互相转换
-printf("%f\n", (float)100); // %f 表示单精度浮点
-printf("%lf\n", (double)100); // %lf 表示双精度浮点
+printf("%f\n", (float)100); // %f 格式化单精度浮点
+printf("%lf\n", (double)100); // %lf 格式化双精度浮点
printf("%d\n", (char)100.0);
///////////////////////////////////////
@@ -216,67 +331,89 @@ printf("%d\n", (char)100.0);
///////////////////////////////////////
// 指针变量是用来储存内存地址的变量
-// 指针变量的定义也会告诉你指向的地址的变量的类型
-// 你也可以得到某个变量的地址,并对它们进行操作
+// 指针变量的声明也会告诉它所指向的数据的类型
+// 你可以使用得到你的变量的地址,并把它们搞乱,;-)
int x = 0;
printf("%p\n", &x); // 用 & 来获取变量的地址
-// (%p 表示一个指针)
-// => 输出某个内存地址
+// (%p 格式化一个类型为 void *的指针)
+// => 打印某个内存地址
-// 指针类型在定义是需要以*结束
-int* px; // px是一个指向int型的指针
+// 指针类型在声明中以*开头
+int* px, not_a_pointer; // px是一个指向int型的指针
px = &x; // 把x的地址保存到px中
-printf("%p\n", px); // => 输出内存中的某个地址
+printf("%p\n", (void *)px); // => 输出内存中的某个地址
+printf("%zu, %zu\n", sizeof(px), sizeof(not_a_pointer));
+// => 在64位系统上打印“8, 4”。
-// 要得到某个指针指向的内容的值,可以在指针前加一个*来取得(去引用)
+// 要得到某个指针指向的内容的值,可以在指针前加一个*来取得(取消引用)
+// 注意: 是的,这可能让人困惑,'*'在用来声明一个指针的同时取消引用它。
printf("%d\n", *px); // => 输出 0, 即x的值
// 你也可以改变指针所指向的值
-// 此时你需要在*运算符后添加一个括号,因为++比*的优先级更高
-(*px)++; // 把px所指向的值增加2
+// 此时你需要取消引用上添加括号,因为++比*的优先级更高
+(*px)++; // 把px所指向的值增加1
printf("%d\n", *px); // => 输出 1
printf("%d\n", x); // => 输出 1
-int x_array[20]; // 数组是分配一系列连续空间的常用方式
+// 数组是分配一系列连续空间的常用方式
+int x_array[20];
int xx;
for (xx=0; xx<20; xx++) {
x_array[xx] = 20 - xx;
} // 初始化 x_array 为 20, 19, 18,... 2, 1
-// 生命一个变量为指向整型的指针类型,并初始化为指向x_array
+// 声明一个整型的指针,并初始化为指向x_array
int* x_ptr = x_array;
// x_ptr现在指向了数组的第一个元素(即整数20).
-
-// 事实上数组本身就是指向它的第一个元素的指针
-printf("%d\n", *(x_ptr)); // => 输出 20
-printf("%d\n", x_array[0]); // => 输出 20
+// 这是因为数组通常衰减为指向它们的第一个元素的指针。
+// 例如,当一个数组被传递给一个函数或者绑定到一个指针时,
+//它衰减为(隐式转化为)一个指针。
+// 例外: 当数组是`&`操作符的参数:
+int arr[10];
+int (*ptr_to_arr)[10] = &arr; // &arr的类型不是`int *`!
+ // 它的类型是指向数组的指针(数组由10个int组成)
+// 或者当数组是字符串字面量(初始化字符数组)
+char arr[] = "foobarbazquirk";
+// 或者当它是`sizeof`或`alignof`操作符的参数时:
+int arr[10];
+int *ptr = arr; // 等价于 int *ptr = &arr[0];
+printf("%zu, %zu\n", sizeof arr, sizeof ptr); // 应该会输出"40, 4"或"40, 8"
// 指针的增减多少是依据它本身的类型而定的
-printf("%d\n", *(x_ptr + 1)); // => 输出 19
-printf("%d\n", x_array[1]); // => 输出 19
+// (这被称为指针算术)
+printf("%d\n", *(x_ptr + 1)); // => 打印 19
+printf("%d\n", x_array[1]); // => 打印 19
// 你也可以通过标准库函数malloc来实现动态分配
-// 这个函数接受一个代表容量的参数
-// 系统会从堆区分配指定容量字节大小的空间
-int* my_ptr = (int*) malloc(sizeof(int) * 20);
+// 这个函数接受一个代表容量的参数,参数类型为`size_t`
+// 系统一般会从堆区分配指定容量字节大小的空间
+// (在一些系统,例如嵌入式系统中这点不一定成立
+// C标准对此未置一词。)
+int *my_ptr = malloc(sizeof(*my_ptr) * 20);
for (xx=0; xx<20; xx++) {
- *(my_ptr + xx) = 20 - xx; // my_ptr[xx] = 20-xx 也可以
-} // 初始化内存为 20, 19, 18, 17... 2, 1 (as ints)
+ *(my_ptr + xx) = 20 - xx; // my_ptr[xx] = 20-xx
+} // 初始化内存为 20, 19, 18, 17... 2, 1 (类型为int)
-// 如果对一些未分配的内存取值则会得到未知的结果
+// 对未分配的内存进行取消引用会产生未定义的结果
printf("%d\n", *(my_ptr + 21)); // => 谁知道会输出什么
-// 当你通过malloc得到一块区域后,你需要释放它
+// malloc分配的区域需要手动释放
// 否则没人能够再次使用这块内存,直到程序结束为止
free(my_ptr);
// 字符串通常是字符数组,但是经常用字符指针表示
-// 指针:
-char* my_str = "This is my very own string";
-
+// (它是指向数组的第一个元素的指针)
+// 一个优良的实践是使用`const char *`来引用一个字符串字面量,
+// 因为字符串字面量不应当被修改(即"foo"[0] = 'a'犯了大忌)
+const char* my_str = "This is my very own string";
printf("%c\n", *my_str); // => 'T'
+// 如果字符串是数组,(多半是用字符串字面量初始化的)
+// 情况就不一样了,字符串位于可写的内存中
+char foo[] = "foo";
+foo[0] = 'a'; // 这是合法的,foo现在包含"aoo"
+
function_1();
} // main函数结束
@@ -292,16 +429,21 @@ int add_two_ints(int x1, int x2){
}
/*
-函数是按值传递的, 但是你可以通过传递参数来传递引用,这样函数就可以更改值
+函数是按值传递的。当调用一个函数的时候,传递给函数的参数
+是原有值的拷贝(数组除外)。你在函数内对参数所进行的操作
+不会改变该参数原有的值。
+
+但是你可以通过指针来传递引用,这样函数就可以更改值
例子:字符串本身翻转
*/
// 类型为void的函数没有返回值
-void str_reverse(char* str_in){
+void str_reverse(char *str_in){
char tmp;
- int ii=0, len = strlen(str_in); // Strlen 是C标准库函数
- for(ii=0; ii<len/2; ii++){
+ int ii = 0;
+ size_t len = strlen(str_in); // `strlen()`` 是C标准库函数
+ for(ii = 0; ii < len / 2; ii++){
tmp = str_in[ii];
str_in[ii] = str_in[len - ii - 1]; // 从倒数第ii个开始
str_in[len - ii - 1] = tmp;
@@ -314,6 +456,20 @@ str_reverse(c);
printf("%s\n", c); // => ".tset a si sihT"
*/
+// 如果引用函数之外的变量,必须使用extern关键字
+int i = 0;
+void testFunc() {
+ extern int i; // 使用外部变量 i
+}
+
+// 使用static确保external变量为源文件私有
+static int i = 0; // 其他使用 testFunc()的文件无法访问变量i
+void testFunc() {
+ extern int i;
+}
+//**你同样可以声明函数为static**
+
+
///////////////////////////////////////
// 用户自定义类型和结构
///////////////////////////////////////
@@ -322,12 +478,16 @@ printf("%s\n", c); // => ".tset a si sihT"
typedef int my_type;
my_type my_type_var = 0;
-// 结构是一系列数据的集合
+// struct是数据的集合,成员依序分配,按照
+// 编写的顺序
struct rectangle {
int width;
int height;
};
+// 一般而言,以下断言不成立:
+// sizeof(struct rectangle) == sizeof(int) + sizeof(int)
+//这是因为structure成员之间可能存在潜在的间隙(为了对齐)[1]
void function_1(){
@@ -338,12 +498,12 @@ void function_1(){
my_rec.height = 20;
// 你也可以声明指向结构体的指针
- struct rectangle* my_rec_ptr = &my_rec;
+ struct rectangle *my_rec_ptr = &my_rec;
- // 通过取值来改变结构体的成员...
+ // 通过取消引用来改变结构体的成员...
(*my_rec_ptr).width = 30;
- // ... 或者用 -> 操作符作为简写
+ // ... 或者用 -> 操作符作为简写提高可读性
my_rec_ptr->height = 10; // Same as (*my_rec_ptr).height = 10;
}
@@ -354,18 +514,25 @@ int area(rect r){
return r.width * r.height;
}
+// 如果struct较大,你可以通过指针传递,避免
+// 复制整个struct。
+int area(const rect *r)
+{
+ return r->width * r->height;
+}
+
///////////////////////////////////////
// 函数指针
///////////////////////////////////////
/*
在运行时,函数本身也被存放到某块内存区域当中
-函数指针就像其他指针一样, 但却可以被用来直接调用函数,
-并且可以被四处传递(就像回调函数那样)
-但是,定义的语法有可能在一开始会有些误解
+函数指针就像其他指针一样(不过是存储一个内存地址) 但却可以被用来直接调用函数,
+并且可以四处传递回调函数
+但是,定义的语法初看令人有些迷惑
例子:通过指针调用str_reverse
*/
-void str_reverse_through_pointer(char * str_in) {
+void str_reverse_through_pointer(char *str_in) {
// 定义一个函数指针 f.
void (*f)(char *); // 签名一定要与目标函数相同
f = &str_reverse; // 将函数的地址在运行时赋给指针
@@ -374,7 +541,7 @@ void str_reverse_through_pointer(char * str_in) {
}
/*
-只要函数签名是正确的,任何时候都能将正确的函数赋给某个函数指针
+只要函数签名是正确的,任何时候都能将任何函数赋给某个函数指针
为了可读性和简洁性,函数指针经常和typedef搭配使用:
*/
@@ -384,12 +551,73 @@ typedef void (*my_fnp_type)(char *);
// ...
// my_fnp_type f;
+// 特殊字符
+'\a' // bell
+'\n' // 换行
+'\t' // tab
+'\v' // vertical tab
+'\f' // formfeed
+'\r' // 回车
+'\b' // 退格
+'\0' // null,通常置于字符串的最后。
+ // hello\n\0. 按照惯例,\0用于标记字符串的末尾。
+'\\' // 反斜杠
+'\?' // 问号
+'\'' // 单引号
+'\"' // 双引号
+'\xhh' // 十六进制数字. 例子: '\xb' = vertical tab
+'\ooo' // 十进制数字. 例子: '\013' = vertical tab
+
+// 打印格式:
+"%d" // 整数
+"%3d" // 3位以上整数 (右对齐文本)
+"%s" // 字符串
+"%f" // float
+"%ld" // long
+"%3.2f" // 左3位以上、右2位以上十进制浮
+"%7.4s" // (字符串同样适用)
+"%c" // 字母
+"%p" // 指针
+"%x" // 十六进制
+"%o" // 十进制
+"%%" // 打印 %
+
+///////////////////////////////////////
+// 演算优先级
+///////////////////////////////////////
+//---------------------------------------------------//
+// 操作符 | 组合 //
+//---------------------------------------------------//
+// () [] -> . | 从左到右 //
+// ! ~ ++ -- + = *(type)sizeof | 从右到左 //
+// * / % | 从左到右 //
+// + - | 从左到右 //
+// << >> | 从左到右 //
+// < <= > >= | 从左到右 //
+// == != | 从左到右 //
+// & | 从左到右 //
+// ^ | 从左到右 //
+// | | 从左到右 //
+// && | 从左到右 //
+// || | 从左到右 //
+// ?: | 从右到左 //
+// = += -= *= /= %= &= ^= |= <<= >>= | 从右到左 //
+// , | 从左到右 //
+//---------------------------------------------------//
+
```
## 更多阅读
-最好找一本 [K&R, aka "The C Programming Language", “C程序设计语言”](https://en.wikipedia.org/wiki/The_C_Programming_Language)
+最好找一本 [K&R, aka "The C Programming Language", “C程序设计语言”](https://en.wikipedia.org/wiki/The_C_Programming_Language)。它是关于C最重要的一本书,由C的创作者撰写。不过需要留意的是它比较古老了,因此有些不准确的地方。
+
-其他一些比较好的资源 [Learn C the hard way](http://c.learncodethehardway.org/book/)
+另一个比较好的资源是 [Learn C the hard way](http://c.learncodethehardway.org/book/)
+如果你有问题,请阅读[compl.lang.c Frequently Asked Questions](http://c-faq.com/)。
+
+使用合适的空格、缩进,保持一致的代码风格非常重要。可读性强的代码比聪明的代码、高速的代码更重要。可以参考下[Linux内核编码风格](https://www.kernel.org/doc/Documentation/CodingStyle)
+。
除了这些,多多Google吧
+
+[1] http://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member
diff --git a/zh-cn/clojure-cn.html.markdown b/zh-cn/clojure-cn.html.markdown
new file mode 100644
index 00000000..fa241384
--- /dev/null
+++ b/zh-cn/clojure-cn.html.markdown
@@ -0,0 +1,368 @@
+---
+language: clojure
+filename: learnclojure-cn.clj
+contributors:
+ - ["Adam Bard", "http://adambard.com/"]
+translators:
+ - ["Bill Zhang", "http://jingege.github.io/"]
+lang: zh-cn
+---
+
+Clojure是运行在JVM上的Lisp家族中的一员。她比Common Lisp更强调纯[函数式编程](https://en.wikipedia.org/wiki/Functional_programming),且自发布时便包含了一组工具来处理状态。
+
+这种组合让她能十分简单且自动地处理并发问题。
+
+(你需要使用Clojure 1.2或更新的发行版)
+
+```clojure
+; 注释以分号开始。
+
+; Clojure代码由一个个form组成, 即写在小括号里的由空格分开的一组语句。
+; Clojure解释器会把第一个元素当做一个函数或者宏来调用,其余的被认为是参数。
+
+; Clojure代码的第一条语句一般是用ns来指定当前的命名空间。
+(ns learnclojure)
+
+; 更基本的例子:
+
+; str会使用所有参数来创建一个字符串
+(str "Hello" " " "World") ; => "Hello World"
+
+; 数学计算比较直观
+(+ 1 1) ; => 2
+(- 2 1) ; => 1
+(* 1 2) ; => 2
+(/ 2 1) ; => 2
+
+; 等号是 =
+(= 1 1) ; => true
+(= 2 1) ; => false
+
+; 逻辑非
+(not true) ; => false
+
+; 嵌套的form工作起来应该和你预想的一样
+(+ 1 (- 3 2)) ; = 1 + (3 - 2) => 2
+
+; 类型
+;;;;;;;;;;;;;
+
+; Clojure使用Java的Object来描述布尔值、字符串和数字
+; 用函数 `class` 来查看具体的类型
+(class 1) ; 整形默认是java.lang.Long类型
+(class 1.); 浮点默认是java.lang.Double类型的
+(class ""); String是java.lang.String类型的,要用双引号引起来
+(class false) ; 布尔值是java.lang.Boolean类型的
+(class nil); "null"被称作nil
+
+; 如果你想创建一组数据字面量,用单引号(')来阻止form被解析和求值
+'(+ 1 2) ; => (+ 1 2)
+; (单引号是quote的简写形式,故上式等价于(quote (+ 1 2)))
+
+; 可以对一个引用列表求值
+(eval '(+ 1 2)) ; => 3
+
+; 集合(Collection)和序列
+;;;;;;;;;;;;;;;;;;;
+
+; List的底层实现是链表,Vector的底层实现是数组
+; 二者也都是java类
+(class [1 2 3]); => clojure.lang.PersistentVector
+(class '(1 2 3)); => clojure.lang.PersistentList
+
+; list本可以写成(1 2 3), 但必须用引用来避免被解释器当做函数来求值。
+; (list 1 2 3)等价于'(1 2 3)
+
+; 集合其实就是一组数据
+; List和Vector都是集合:
+(coll? '(1 2 3)) ; => true
+(coll? [1 2 3]) ; => true
+
+; 序列 (seqs) 是数据列表的抽象描述
+; 只有列表才可称作序列。
+(seq? '(1 2 3)) ; => true
+(seq? [1 2 3]) ; => false
+
+; 序列被访问时只需要提供一个值,所以序列可以被懒加载——也就意味着可以定义一个无限序列:
+(range 4) ; => (0 1 2 3)
+(range) ; => (0 1 2 3 4 ...) (无限序列)
+(take 4 (range)) ; (0 1 2 3)
+
+; cons用以向列表或向量的起始位置添加元素
+(cons 4 [1 2 3]) ; => (4 1 2 3)
+(cons 4 '(1 2 3)) ; => (4 1 2 3)
+
+; conj将以最高效的方式向集合中添加元素。
+; 对于列表,数据会在起始位置插入,而对于向量,则在末尾位置插入。
+(conj [1 2 3] 4) ; => [1 2 3 4]
+(conj '(1 2 3) 4) ; => (4 1 2 3)
+
+; 用concat来合并列表或向量
+(concat [1 2] '(3 4)) ; => (1 2 3 4)
+
+; 用filter来过滤集合中的元素,用map来根据指定的函数来映射得到一个新的集合
+(map inc [1 2 3]) ; => (2 3 4)
+(filter even? [1 2 3]) ; => (2)
+
+; recuce使用函数来规约集合
+(reduce + [1 2 3 4])
+; = (+ (+ (+ 1 2) 3) 4)
+; => 10
+
+; reduce还能指定一个初始参数
+(reduce conj [] '(3 2 1))
+; = (conj (conj (conj [] 3) 2) 1)
+; => [3 2 1]
+
+; 函数
+;;;;;;;;;;;;;;;;;;;;;
+
+; 用fn来创建函数。函数的返回值是最后一个表达式的值
+(fn [] "Hello World") ; => fn
+
+; (你需要再嵌套一组小括号来调用它)
+((fn [] "Hello World")) ; => "Hello World"
+
+; 你可以用def来创建一个变量(var)
+(def x 1)
+x ; => 1
+
+; 将函数定义为一个变量(var)
+(def hello-world (fn [] "Hello World"))
+(hello-world) ; => "Hello World"
+
+; 你可用defn来简化函数的定义
+(defn hello-world [] "Hello World")
+
+; 中括号内的内容是函数的参数。
+(defn hello [name]
+ (str "Hello " name))
+(hello "Steve") ; => "Hello Steve"
+
+; 你还可以用这种简写的方式来创建函数:
+(def hello2 #(str "Hello " %1))
+(hello2 "Fanny") ; => "Hello Fanny"
+
+; 函数也可以有多个参数列表。
+(defn hello3
+ ([] "Hello World")
+ ([name] (str "Hello " name)))
+(hello3 "Jake") ; => "Hello Jake"
+(hello3) ; => "Hello World"
+
+; 可以定义变参函数,即把&后面的参数全部放入一个序列
+(defn count-args [& args]
+ (str "You passed " (count args) " args: " args))
+(count-args 1 2 3) ; => "You passed 3 args: (1 2 3)"
+
+; 可以混用定参和变参(用&来界定)
+(defn hello-count [name & args]
+ (str "Hello " name ", you passed " (count args) " extra args"))
+(hello-count "Finn" 1 2 3)
+; => "Hello Finn, you passed 3 extra args"
+
+
+; 哈希表
+;;;;;;;;;;
+
+; 基于hash的map和基于数组的map(即arraymap)实现了相同的接口,hashmap查询起来比较快,
+; 但不保证元素的顺序。
+(class {:a 1 :b 2 :c 3}) ; => clojure.lang.PersistentArrayMap
+(class (hash-map :a 1 :b 2 :c 3)) ; => clojure.lang.PersistentHashMap
+
+; arraymap在足够大的时候,大多数操作会将其自动转换成hashmap,
+; 所以不用担心(对大的arraymap的查询性能)。
+
+; map支持很多类型的key,但推荐使用keyword类型
+; keyword类型和字符串类似,但做了一些优化。
+(class :a) ; => clojure.lang.Keyword
+
+(def stringmap {"a" 1, "b" 2, "c" 3})
+stringmap ; => {"a" 1, "b" 2, "c" 3}
+
+(def keymap {:a 1, :b 2, :c 3})
+keymap ; => {:a 1, :c 3, :b 2}
+
+; 顺便说一下,map里的逗号是可有可无的,作用只是提高map的可读性。
+
+; 从map中查找元素就像把map名作为函数调用一样。
+(stringmap "a") ; => 1
+(keymap :a) ; => 1
+
+; 可以把keyword写在前面来从map中查找元素。
+(:b keymap) ; => 2
+
+; 但不要试图用字符串类型的key来这么做。
+;("a" stringmap)
+; => Exception: java.lang.String cannot be cast to clojure.lang.IFn
+
+; 查找不存在的key会返回nil。
+(stringmap "d") ; => nil
+
+; 用assoc函数来向hashmap里添加元素
+(def newkeymap (assoc keymap :d 4))
+newkeymap ; => {:a 1, :b 2, :c 3, :d 4}
+
+; 但是要记住的是clojure的数据类型是不可变的!
+keymap ; => {:a 1, :b 2, :c 3}
+
+; 用dissoc来移除元素
+(dissoc keymap :a :b) ; => {:c 3}
+
+; 集合(Set)
+;;;;;;
+
+(class #{1 2 3}) ; => clojure.lang.PersistentHashSet
+(set [1 2 3 1 2 3 3 2 1 3 2 1]) ; => #{1 2 3}
+
+; 用conj新增元素
+(conj #{1 2 3} 4) ; => #{1 2 3 4}
+
+; 用disj移除元素
+(disj #{1 2 3} 1) ; => #{2 3}
+
+; 把集合当做函数调用来检查元素是否存在:
+(#{1 2 3} 1) ; => 1
+(#{1 2 3} 4) ; => nil
+
+; 在clojure.sets模块下有很多相关函数。
+
+; 常用的form
+;;;;;;;;;;;;;;;;;
+
+; clojure里的逻辑控制结构都是用宏(macro)实现的,这在语法上看起来没什么不同。
+(if false "a" "b") ; => "b"
+(if false "a") ; => nil
+
+; 用let来创建临时的绑定变量。
+(let [a 1 b 2]
+ (> a b)) ; => false
+
+; 用do将多个语句组合在一起依次执行
+(do
+ (print "Hello")
+ "World") ; => "World" (prints "Hello")
+
+; 函数定义里有一个隐式的do
+(defn print-and-say-hello [name]
+ (print "Saying hello to " name)
+ (str "Hello " name))
+(print-and-say-hello "Jeff") ;=> "Hello Jeff" (prints "Saying hello to Jeff")
+
+; let也是如此
+(let [name "Urkel"]
+ (print "Saying hello to " name)
+ (str "Hello " name)) ; => "Hello Urkel" (prints "Saying hello to Urkel")
+
+; 模块
+;;;;;;;;;;;;;;;
+
+; 用use来导入模块里的所有函数
+(use 'clojure.set)
+
+; 然后就可以使用set相关的函数了
+(intersection #{1 2 3} #{2 3 4}) ; => #{2 3}
+(difference #{1 2 3} #{2 3 4}) ; => #{1}
+
+; 你也可以从一个模块里导入一部分函数。
+(use '[clojure.set :only [intersection]])
+
+; 用require来导入一个模块
+(require 'clojure.string)
+
+; 用/来调用模块里的函数
+; 下面是从模块`clojure.string`里调用`blank?`函数。
+(clojure.string/blank? "") ; => true
+
+; 在`import`里你可以给模块名指定一个较短的别名。
+(require '[clojure.string :as str])
+(str/replace "This is a test." #"[a-o]" str/upper-case) ; => "THIs Is A tEst."
+; (#""用来表示一个正则表达式)
+
+; 你可以在一个namespace定义里用:require的方式来require(或use,但最好不要用)模块。
+; 这样的话你无需引用模块列表。
+(ns test
+ (:require
+ [clojure.string :as str]
+ [clojure.set :as set]))
+
+; Java
+;;;;;;;;;;;;;;;;;
+
+; Java有大量的优秀的库,你肯定想学会如何用clojure来使用这些Java库。
+
+; 用import来导入java类
+(import java.util.Date)
+
+; 也可以在ns定义里导入
+(ns test
+ (:import java.util.Date
+ java.util.Calendar))
+
+; 用类名末尾加`.`的方式来new一个Java对象
+(Date.) ; <a date object>
+
+; 用`.`操作符来调用方法,或者用`.method`的简化方式。
+(. (Date.) getTime) ; <a timestamp>
+(.getTime (Date.)) ; 和上例一样。
+
+; 用`/`调用静态方法
+(System/currentTimeMillis) ; <a timestamp> (system is always present)
+
+; 用`doto`来更方便的使用(可变)类。
+(import java.util.Calendar)
+(doto (Calendar/getInstance)
+ (.set 2000 1 1 0 0 0)
+ .getTime) ; => A Date. set to 2000-01-01 00:00:00
+
+; STM
+;;;;;;;;;;;;;;;;;
+
+; 软件内存事务(Software Transactional Memory)被clojure用来处理持久化的状态。
+; clojure里内置了一些结构来使用STM。
+; atom是最简单的。给它传一个初始值
+(def my-atom (atom {}))
+
+; 用`swap!`更新atom。
+; `swap!`会以atom的当前值为第一个参数来调用一个指定的函数,
+; `swap`其余的参数作为该函数的第二个参数。
+(swap! my-atom assoc :a 1) ; Sets my-atom to the result of (assoc {} :a 1)
+(swap! my-atom assoc :b 2) ; Sets my-atom to the result of (assoc {:a 1} :b 2)
+
+; 用`@`读取atom的值
+my-atom ;=> Atom<#...> (返回Atom对象)
+@my-atom ; => {:a 1 :b 2}
+
+; 下例是一个使用atom实现的简单计数器
+(def counter (atom 0))
+(defn inc-counter []
+ (swap! counter inc))
+
+(inc-counter)
+(inc-counter)
+(inc-counter)
+(inc-counter)
+(inc-counter)
+
+@counter ; => 5
+
+; 其他STM相关的结构是ref和agent.
+; Refs: http://clojure.org/refs
+; Agents: http://clojure.org/agents
+```
+
+### 进阶读物
+
+本文肯定不足以讲述关于clojure的一切,但是希望足以让你迈出第一步。
+
+Clojure.org官网有很多文章:
+[http://clojure.org/](http://clojure.org/)
+
+Clojuredocs.org有大多数核心函数的文档,还带了示例哦:
+[http://clojuredocs.org/quickref/Clojure%20Core](http://clojuredocs.org/quickref/Clojure%20Core)
+
+4Clojure是个很赞的用来练习clojure/FP技能的地方:
+[http://www.4clojure.com/](http://www.4clojure.com/)
+
+Clojure-doc.org (你没看错)有很多入门级的文章:
+[http://clojure-doc.org/](http://clojure-doc.org/)
diff --git a/zh-cn/clojure-macro-cn.html.markdown b/zh-cn/clojure-macro-cn.html.markdown
new file mode 100644
index 00000000..9324841e
--- /dev/null
+++ b/zh-cn/clojure-macro-cn.html.markdown
@@ -0,0 +1,152 @@
+---
+language: "clojure macros"
+filename: learnclojuremacros-zh.clj
+contributors:
+ - ["Adam Bard", "http://adambard.com/"]
+translators:
+ - ["Jakukyo Friel", "http://weakish.github.io"]
+lang: zh-cn
+---
+
+和所有Lisp一样,Clojure内在的[同构性](https://en.wikipedia.org/wiki/Homoiconic)使得你可以穷尽语言的特性,编写生成代码的子过程——“宏”。宏是一种按需调制语言的强大方式。
+
+小心!可以用函数完成的事用宏去实现可不是什么好事。你应该仅在需要控制参数是否或者何时eval的时候使用宏。
+
+你应该熟悉Clojure.确保你了解[Y分钟学Clojure](http://learnxinyminutes.com/docs/zh-cn/clojure-cn/)中的所有内容。
+
+```clojure
+;; 使用defmacro定义宏。宏应该输出一个可以作为clojure代码演算的列表。
+;;
+;; 以下宏的效果和直接写(reverse "Hello World")一致。
+
+(defmacro my-first-macro []
+ (list reverse "Hello World"))
+
+;; 使用macroexpand或macroexpand-1查看宏的结果。
+;;
+;; 注意,调用需要引用。
+(macroexpand '(my-first-macro))
+;; -> (#<core$reverse clojure.core$reverse@xxxxxxxx> "Hello World")
+
+;; 你可以直接eval macroexpand的结果
+(eval (macroexpand '(my-first-macro)))
+; -> (\d \l \o \r \W \space \o \l \l \e \H)
+
+;; 不过一般使用以下形式,更简短,更像函数:
+(my-first-macro) ; -> (\d \l \o \r \W \space \o \l \l \e \H)
+
+;; 创建宏的时候可以使用更简短的引用形式来创建列表
+(defmacro my-first-quoted-macro []
+ '(reverse "Hello World"))
+
+(macroexpand '(my-first-quoted-macro))
+;; -> (reverse "Hello World")
+;; 注意reverse不再是一个函数对象,而是一个符号。
+
+;; 宏可以传入参数。
+(defmacro inc2 [arg]
+ (list + 2 arg))
+
+(inc2 2) ; -> 4
+
+;; 不过,如果你尝试配合使用引用列表,会导致错误,
+;; 因为参数也会被引用。
+;; 为了避免这个问题,clojure提供了引用宏的另一种方式:`
+;; 在`之内,你可以使用~获得外圈作用域的变量。
+(defmacro inc2-quoted [arg]
+ `(+ 2 ~arg))
+
+(inc2-quoted 2)
+
+;; 你可以使用通常的析构参数。用~@展开列表中的变量。
+(defmacro unless [arg & body]
+ `(if (not ~arg)
+ (do ~@body))) ; 别忘了 do!
+
+(macroexpand '(unless true (reverse "Hello World")))
+
+;; ->
+;; (if (clojure.core/not true) (do (reverse "Hello World")))
+
+;; 当第一个参数为假时,(unless)会演算、返回主体。
+;; 否则返回nil。
+
+(unless true "Hello") ; -> nil
+(unless false "Hello") ; -> "Hello"
+
+;; 需要小心,宏会搞乱你的变量
+(defmacro define-x []
+ '(do
+ (def x 2)
+ (list x)))
+
+(def x 4)
+(define-x) ; -> (2)
+(list x) ; -> (2)
+
+;; 使用gensym来获得独有的标识符
+(gensym 'x) ; -> x1281 (or some such thing)
+
+(defmacro define-x-safely []
+ (let [sym (gensym 'x)]
+ `(do
+ (def ~sym 2)
+ (list ~sym))))
+
+(def x 4)
+(define-x-safely) ; -> (2)
+(list x) ; -> (4)
+
+;; 你可以在 ` 中使用 # 为每个符号自动生成gensym
+(defmacro define-x-hygenically []
+ `(do
+ (def x# 2)
+ (list x#)))
+
+(def x 4)
+(define-x-hygenically) ; -> (2)
+(list x) ; -> (4)
+
+;; 通常会配合宏使用帮助函数。
+;; 让我们创建一些帮助函数来支持(无聊的)算术语法:
+
+(declare inline-2-helper)
+(defn clean-arg [arg]
+ (if (seq? arg)
+ (inline-2-helper arg)
+ arg))
+
+(defn apply-arg
+ "Given args [x (+ y)], return (+ x y)"
+ [val [op arg]]
+ (list op val (clean-arg arg)))
+
+(defn inline-2-helper
+ [[arg1 & ops-and-args]]
+ (let [ops (partition 2 ops-and-args)]
+ (reduce apply-arg (clean-arg arg1) ops)))
+
+;; 在创建宏前,我们可以先测试
+(inline-2-helper '(a + (b - 2) - (c * 5))) ; -> (- (+ a (- b 2)) (* c 5))
+
+; 然而,如果我们希望它在编译期执行,就需要创建宏
+(defmacro inline-2 [form]
+ (inline-2-helper form)))
+
+(macroexpand '(inline-2 (1 + (3 / 2) - (1 / 2) + 1)))
+; -> (+ (- (+ 1 (/ 3 2)) (/ 1 2)) 1)
+
+(inline-2 (1 + (3 / 2) - (1 / 2) + 1))
+; -> 3 (事实上,结果是3N, 因为数字被转化为带/的有理分数)
+```
+
+## 扩展阅读
+
+[Clojure for the Brave and True](http://www.braveclojure.com/)系列的编写宏
+http://www.braveclojure.com/writing-macros/
+
+官方文档
+http://clojure.org/macros
+
+何时使用宏?
+http://dunsmor.com/lisp/onlisp/onlisp_12.html
diff --git a/zh-cn/coffeescript-cn.html.markdown b/zh-cn/coffeescript-cn.html.markdown
new file mode 100644
index 00000000..44561541
--- /dev/null
+++ b/zh-cn/coffeescript-cn.html.markdown
@@ -0,0 +1,101 @@
+---
+language: coffeescript
+contributors:
+ - ["Tenor Biel", "http://github.com/L8D"]
+ - ["Xavier Yao", "http://github.com/xavieryao"]
+translators:
+ - ["Xavier Yao", "http://github.com/xavieryao"]
+filename: coffeescript-cn.coffee
+lang: zh-cn
+---
+
+CoffeeScript是逐句编译为JavaScript的一种小型语言,且没有运行时的解释器。
+作为JavaScript的替代品之一,CoffeeScript旨在编译人类可读、美观优雅且速度不输原生的代码,
+且编译后的代码可以在任何JavaScript运行时正确运行。
+
+参阅 [CoffeeScript官方网站](http://coffeescript.org/)以获取CoffeeScript的完整教程。
+
+``` coffeescript
+# CoffeeScript是一种很潮的编程语言,
+# 它紧随众多现代编程语言的趋势。
+# 因此正如Ruby和Python,CoffeeScript使用井号标记注释。
+
+###
+大段落注释以此为例,可以被直接编译为 '/ *' 和 '* /' 包裹的JavaScript代码。
+
+在继续之前你需要了解JavaScript的基本概念。
+
+示例中 => 后为编译后的JavaScript代码
+###
+
+# 赋值:
+number = 42 #=> var number = 42;
+opposite = true #=> var opposite = true;
+
+# 条件:
+number = -42 if opposite #=> if(opposite) { number = -42; }
+
+# 函数:
+square = (x) -> x * x #=> var square = function(x) { return x * x; }
+
+fill = (container, liquid = "coffee") ->
+ "Filling the #{container} with #{liquid}..."
+#=>var fill;
+#
+#fill = function(container, liquid) {
+# if (liquid == null) {
+# liquid = "coffee";
+# }
+# return "Filling the " + container + " with " + liquid + "...";
+#};
+
+# 区间:
+list = [1..5] #=> var list = [1, 2, 3, 4, 5];
+
+# 对象:
+math =
+ root: Math.sqrt
+ square: square
+ cube: (x) -> x * square x
+#=> var math = {
+# "root": Math.sqrt,
+# "square": square,
+# "cube": function(x) { return x * square(x); }
+#}
+
+# Splats:
+race = (winner, runners...) ->
+ print winner, runners
+#=>race = function() {
+# var runners, winner;
+# winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
+# return print(winner, runners);
+#};
+
+# 存在判断:
+alert "I knew it!" if elvis?
+#=> if(typeof elvis !== "undefined" && elvis !== null) { alert("I knew it!"); }
+
+# 数组推导:
+cubes = (math.cube num for num in list)
+#=>cubes = (function() {
+# var _i, _len, _results;
+# _results = [];
+# for (_i = 0, _len = list.length; _i < _len; _i++) {
+# num = list[_i];
+# _results.push(math.cube(num));
+# }
+# return _results;
+# })();
+
+foods = ['broccoli', 'spinach', 'chocolate']
+eat food for food in foods when food isnt 'chocolate'
+#=>foods = ['broccoli', 'spinach', 'chocolate'];
+#
+#for (_k = 0, _len2 = foods.length; _k < _len2; _k++) {
+# food = foods[_k];
+# if (food !== 'chocolate') {
+# eat(food);
+# }
+#}
+```
diff --git a/zh-cn/common-lisp-cn.html.markdown b/zh-cn/common-lisp-cn.html.markdown
new file mode 100644
index 00000000..c4dc3274
--- /dev/null
+++ b/zh-cn/common-lisp-cn.html.markdown
@@ -0,0 +1,624 @@
+---
+language: "Common Lisp"
+filename: commonlisp-cn.lisp
+contributors:
+ - ["Paul Nathan", "https://github.com/pnathan"]
+translators:
+ - ["Mac David", "http://macdavid313.com"]
+ - ["mut0u", "http://github.com/mut0u"]
+lang: zh-cn
+---
+
+ANSI Common Lisp 是一个广泛通用于各个工业领域的、支持多种范式的编程语言。
+这门语言也经常被引用作“可编程的编程语言”(可以写代码的代码)。
+
+免费的经典的入门书籍[《实用 Common Lisp 编程》](http://www.gigamonkeys.com/book/)
+
+另外还有一本热门的近期出版的
+[Land of Lisp](http://landoflisp.com/).
+
+```scheme
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;; 0. 语法
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;;; 一般形式
+
+;; Lisp有两个基本的语法单元:原子(atom),以及S-表达式。
+;; 一般的,一组S-表达式被称为“组合式”。
+
+10 ; 一个原子; 它对自身进行求值
+
+:THING ;同样是一个原子;它被求值为一个符号 :thing
+
+t ;还是一个原子,代表逻辑真值。
+
+(+ 1 2 3 4) ; 一个S-表达式。
+
+'(4 :foo t) ;同样是一个S-表达式。
+
+
+;;; 注释
+
+;; 一个分号开头的注释表示仅用于此行(单行);两个分号开头的则表示一个所谓标准注释;
+;; 三个分号开头的意味着段落注释;
+;; 而四个分号开头的注释用于文件头注释(译者注:即对该文件的说明)。
+
+#| 块注释
+ 可以涵盖多行,而且...
+ #|
+ 他们可以被嵌套!
+ |#
+|#
+
+;;; 运行环境
+
+;; 有很多不同的Common Lisp的实现;并且大部分的实现是一致(可移植)的。
+;; 对于入门学习来说,CLISP是个不错的选择。
+
+;; 可以通过QuickLisp.org的Quicklisp系统管理你的库。
+
+;; 通常,使用文本编辑器和“REPL”来开发Common Lisp;
+;; (译者注:“REPL”指读取-求值-打印循环)。
+;; “REPL”允许对程序进行交互式的运行、调试,就好像在系统“现场”操作。
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;; 1. 基本数据类型以及运算符
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;;; 符号
+
+'foo ; => FOO 注意到这个符号被自动转换成大写了。
+
+;; `intern`由一个给定的字符串而创建相应的符号
+
+(intern "AAAA") ; => AAAA
+
+(intern "aaa") ; => |aaa|
+
+;;; 数字
+9999999999999999999999 ; 整型数
+#b111 ; 二进制 => 7
+#o111 ; 八进制 => 73
+#x111 ; 十六进制 => 273
+3.14159s0 ; 单精度
+3.14159d0 ; 双精度
+1/2 ; 分数
+#C(1 2) ; 复数
+
+
+;; 使用函数时,应当写成这样的形式:(f x y z ...);
+;; 其中,f是一个函数(名),x, y, z为参数;
+;; 如果你想创建一个“字面”意义上(即不求值)的列表, 只需使用单引号 ' ,
+;; 从而避免接下来的表达式被求值。即,只“引用”这个数据(而不求值)。
+'(+ 1 2) ; => (+ 1 2)
+;; 你同样也可以手动地调用一个函数(译者注:即使用函数对象来调用函数):
+(funcall #'+ 1 2 3) ; => 6
+;; 一些算术运算符
+(+ 1 1) ; => 2
+(- 8 1) ; => 7
+(* 10 2) ; => 20
+(expt 2 3) ; => 8
+(mod 5 2) ; => 1
+(/ 35 5) ; => 7
+(/ 1 3) ; => 1/3
+(+ #C(1 2) #C(6 -4)) ; => #C(7 -2)
+
+ ;;; 布尔运算
+t ; 逻辑真(任何不是nil的值都被视为真值)
+nil ; 逻辑假,或者空列表
+(not nil) ; => t
+(and 0 t) ; => t
+(or 0 nil) ; => 0
+
+ ;;; 字符
+#\A ; => #\A
+#\λ ; => #\GREEK_SMALL_LETTER_LAMDA(希腊字母Lambda的小写)
+#\u03BB ; => #\GREEK_SMALL_LETTER_LAMDA(Unicode形式的小写希腊字母Lambda)
+
+;;; 字符串被视为一个定长字符数组
+"Hello, world!"
+"Benjamin \"Bugsy\" Siegel" ;反斜杠用作转义字符
+
+;; 可以拼接字符串
+(concatenate 'string "Hello " "world!") ; => "Hello world!"
+
+;; 一个字符串也可被视作一个字符序列
+(elt "Apple" 0) ; => #\A
+
+;; `format`被用于格式化字符串
+(format nil "~a can be ~a" "strings" "formatted")
+
+;; 利用`format`打印到屏幕上是非常简单的
+;;(译者注:注意到第二个参数是t,不同于刚刚的nil);~% 代表换行符
+(format t "Common Lisp is groovy. Dude.~%")
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 2. 变量
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 你可以通过`defparameter`创建一个全局(动态)变量
+;; 变量名可以是除了:()[]{}",'`;#|\ 这些字符之外的其他任何字符
+
+;; 动态变量名应该由*号开头与结尾!
+;; (译者注:这个只是一个习惯)
+
+(defparameter *some-var* 5)
+*some-var* ; => 5
+
+;; 你也可以使用Unicode字符:
+(defparameter *AΛB* nil)
+
+
+;; 访问一个在之前从未被绑定的变量是一种不规范的行为(即使依然是可能发生的);
+;; 不要尝试那样做。
+
+
+;; 局部绑定:在(let ...)语句内,'me'被绑定到"dance with you"上。
+;; `let`总是返回在其作用域内最后一个表达式的值
+
+(let ((me "dance with you"))
+ me)
+;; => "dance with you"
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 3. 结构体和集合
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;; 结构体
+(defstruct dog name breed age)
+(defparameter *rover*
+ (make-dog :name "rover"
+ :breed "collie"
+ :age 5))
+*rover* ; => #S(DOG :NAME "rover" :BREED "collie" :AGE 5)
+
+(dog-p *rover*) ; => t ;; ewww)
+(dog-name *rover*) ; => "rover"
+
+;; Dog-p,make-dog,以及 dog-name都是由defstruct创建的!
+
+;;; 点对单元(Pairs)
+;; `cons`可用于生成一个点对单元, 利用`car`以及`cdr`将分别得到第一个和第二个元素
+(cons 'SUBJECT 'VERB) ; => '(SUBJECT . VERB)
+(car (cons 'SUBJECT 'VERB)) ; => SUBJECT
+(cdr (cons 'SUBJECT 'VERB)) ; => VERB
+
+;;; 列表
+
+;; 所有列表都是由点对单元构成的“链表”。它以'nil'(或者'())作为列表的最后一个元素。
+(cons 1 (cons 2 (cons 3 nil))) ; => '(1 2 3)
+;; `list`是一个生成列表的便利途径
+(list 1 2 3) ; => '(1 2 3)
+;; 并且,一个引用也可被用做字面意义上的列表值
+'(1 2 3) ; => '(1 2 3)
+
+;; 同样的,依然可以用`cons`来添加一项到列表的起始位置
+(cons 4 '(1 2 3)) ; => '(4 1 2 3)
+
+;; 而`append`也可用于连接两个列表
+(append '(1 2) '(3 4)) ; => '(1 2 3 4)
+
+;; 或者使用`concatenate`
+
+(concatenate 'list '(1 2) '(3 4))
+
+;; 列表是一种非常核心的数据类型,所以有非常多的处理列表的函数
+;; 例如:
+(mapcar #'1+ '(1 2 3)) ; => '(2 3 4)
+(mapcar #'+ '(1 2 3) '(10 20 30)) ; => '(11 22 33)
+(remove-if-not #'evenp '(1 2 3 4)) ; => '(2 4)
+(every #'evenp '(1 2 3 4)) ; => nil
+(some #'oddp '(1 2 3 4)) ; => T
+(butlast '(subject verb object)) ; => (SUBJECT VERB)
+
+
+;;; 向量
+
+;; 向量的字面意义是一个定长数组
+;;(译者注:此处所谓“字面意义”,即指#(......)的形式,下文还会出现)
+#(1 2 3) ; => #(1 2 3)
+
+;; 使用`concatenate`来将两个向量首尾连接在一起
+(concatenate 'vector #(1 2 3) #(4 5 6)) ; => #(1 2 3 4 5 6)
+
+;;; 数组
+
+;; 向量和字符串只不过是数组的特例
+
+;; 二维数组
+
+(make-array (list 2 2))
+
+;; (make-array '(2 2)) 也是可以的
+
+; => #2A((0 0) (0 0))
+
+(make-array (list 2 2 2))
+
+; => #3A(((0 0) (0 0)) ((0 0) (0 0)))
+
+;; 注意:数组的默认初始值是可以指定的
+;; 下面是如何指定的示例:
+
+(make-array '(2) :initial-element 'unset)
+
+; => #(UNSET UNSET)
+
+;; 若想获取数组[1][1][1]上的元素:
+(aref (make-array (list 2 2 2)) 1 1 1)
+
+; => 0
+
+;;; 变长向量
+
+;; 若将变长向量打印出来,那么它的字面意义上的值和定长向量的是一样的
+
+(defparameter *adjvec* (make-array '(3) :initial-contents '(1 2 3)
+ :adjustable t :fill-pointer t))
+
+*adjvec* ; => #(1 2 3)
+
+;; 添加新的元素:
+(vector-push-extend 4 *adjvec*) ; => 3
+
+*adjvec* ; => #(1 2 3 4)
+
+
+
+;;; 不怎么严谨地说,集合也可被视为列表
+
+(set-difference '(1 2 3 4) '(4 5 6 7)) ; => (3 2 1)
+(intersection '(1 2 3 4) '(4 5 6 7)) ; => 4
+(union '(1 2 3 4) '(4 5 6 7)) ; => (3 2 1 4 5 6 7)
+(adjoin 4 '(1 2 3 4)) ; => (1 2 3 4)
+
+;; 然而,你可能想使用一个更好的数据结构,而并非一个链表
+
+;;; 在Common Lisp中,“字典”和哈希表的实现是一样的。
+
+;; 创建一个哈希表
+(defparameter *m* (make-hash-table))
+
+;; 给定键,设置对应的值
+(setf (gethash 'a *m*) 1)
+
+;; (通过键)检索对应的值
+(gethash 'a *m*) ; => 1, t
+
+;; 注意此处有一细节:Common Lisp往往返回多个值。`gethash`返回的两个值是t,代表找到了这个元素;返回nil表示没有找到这个元素。
+;;(译者注:返回的第一个值表示给定的键所对应的值或者nil;)
+;;(第二个是一个布尔值,表示在哈希表中是否存在这个给定的键)
+;; 例如,如果可以找到给定的键所对应的值,则返回一个t,否则返回nil
+
+;; 由给定的键检索一个不存在的值,则返回nil
+;;(译者注:这个nil是第一个nil,第二个nil其实是指该键在哈希表中也不存在)
+ (gethash 'd *m*) ;=> nil, nil
+
+;; 给定一个键,你可以指定其对应的默认值:
+(gethash 'd *m* :not-found) ; => :NOT-FOUND
+
+;; 在此,让我们看一看怎样处理`gethash`的多个返回值。
+
+(multiple-value-bind
+ (a b)
+ (gethash 'd *m*)
+ (list a b))
+; => (NIL NIL)
+
+(multiple-value-bind
+ (a b)
+ (gethash 'a *m*)
+ (list a b))
+; => (1 T)
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 3. 函数
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;; 使用`lambda`来创建一个匿名函数。
+;; 一个函数总是返回其形式体内最后一个表达式的值。
+;; 将一个函数对象打印出来后的形式是多种多样的...
+
+(lambda () "Hello World") ; => #<FUNCTION (LAMBDA ()) {1004E7818B}>
+
+;; 使用`funcall`来调用lambda函数
+(funcall (lambda () "Hello World")) ; => "Hello World"
+
+;; 或者使用`apply`
+(apply (lambda () "Hello World") nil) ; => "Hello World"
+
+;; 显式地定义一个函数(译者注:即非匿名的)
+(defun hello-world ()
+ "Hello World")
+(hello-world) ; => "Hello World"
+
+;; 刚刚上面函数名"hello-world"后的()其实是函数的参数列表
+(defun hello (name)
+ (format nil "Hello, ~a " name))
+
+(hello "Steve") ; => "Hello, Steve"
+
+;; 函数可以有可选形参并且其默认值都为nil
+
+(defun hello (name &optional from)
+ (if from
+ (format t "Hello, ~a, from ~a" name from)
+ (format t "Hello, ~a" name)))
+
+ (hello "Jim" "Alpacas") ;; => Hello, Jim, from Alpacas
+
+;; 你也可以指定那些可选形参的默认值
+(defun hello (name &optional (from "The world"))
+ (format t "Hello, ~a, from ~a" name from))
+
+(hello "Steve")
+; => Hello, Steve, from The world
+
+(hello "Steve" "the alpacas")
+; => Hello, Steve, from the alpacas
+
+
+;; 当然,你也可以设置所谓关键字形参;
+;; 关键字形参往往比可选形参更具灵活性。
+
+(defun generalized-greeter (name &key (from "the world") (honorific "Mx"))
+ (format t "Hello, ~a ~a, from ~a" honorific name from))
+
+(generalized-greeter "Jim") ; => Hello, Mx Jim, from the world
+
+(generalized-greeter "Jim" :from "the alpacas you met last summer" :honorific "Mr")
+; => Hello, Mr Jim, from the alpacas you met last summer
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 4. 等式
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;; Common Lisp具有一个十分复杂的用于判断等价的系统,下面只是其中一部分的例子
+
+;; 若要比较数值是否等价,使用`=`
+(= 3 3.0) ; => t
+(= 2 1) ; => nil
+
+;; 若要比较对象的类型,则使用`eql`
+;;(译者注:抱歉,翻译水平实在有限,下面是我个人的补充说明)
+;;(`eq` 返回真,如果对象的内存地址相等)
+;;(`eql` 返回真,如果两个对象内存地址相等,或者对象的类型相同,并且值相等)
+;;(例如同为整形数或浮点数,并且他们的值相等时,二者`eql`等价)
+;;(想要弄清`eql`,其实有必要先了解`eq`)
+;;([可以参考](http://stackoverflow.com/questions/547436/whats-the-difference-between-eq-eql-equal-and-equalp-in-common-lisp))
+;;(可以去CLHS上分别查看两者的文档)
+;;(另外,《实用Common Lisp编程》的4.8节也提到了两者的区别)
+(eql 3 3) ; => t
+(eql 3 3.0) ; => nil
+(eql (list 3) (list 3)) ; => nil
+
+;; 对于列表、字符串、以及位向量,使用`equal`
+(equal (list 'a 'b) (list 'a 'b)) ; => t
+(equal (list 'a 'b) (list 'b 'a)) ; => nil
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 5. 控制流
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;;; 条件判断语句
+
+(if t ; “test”,即判断语句
+ "this is true" ; “then”,即判断条件为真时求值的表达式
+ "this is false") ; “else”,即判断条件为假时求值的表达式
+; => "this is true"
+
+;; 在“test”(判断)语句中,所有非nil或者非()的值都被视为真值
+(member 'Groucho '(Harpo Groucho Zeppo)) ; => '(GROUCHO ZEPPO)
+(if (member 'Groucho '(Harpo Groucho Zeppo))
+ 'yep
+ 'nope)
+; => 'YEP
+
+;; `cond`将一系列测试语句串联起来,并对相应的表达式求值
+(cond ((> 2 2) (error "wrong!"))
+ ((< 2 2) (error "wrong again!"))
+ (t 'ok)) ; => 'OK
+
+;; 对于给定值的数据类型,`typecase`会做出相应地判断
+(typecase 1
+ (string :string)
+ (integer :int))
+
+; => :int
+
+;;; 迭代
+
+;; 当然,递归是肯定被支持的:
+
+(defun walker (n)
+ (if (zerop n)
+ :walked
+ (walker (1- n))))
+
+(walker) ; => :walked
+
+;; 而大部分场合下,我们使用`DOLIST`或者`LOOP`来进行迭代
+
+
+(dolist (i '(1 2 3 4))
+ (format t "~a" i))
+
+; => 1234
+
+(loop for i from 0 below 10
+ collect i)
+
+; => (0 1 2 3 4 5 6 7 8 9)
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 6. 可变性
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;; 使用`setf`可以对一个已经存在的变量进行赋值;
+;; 事实上,刚刚在哈希表的例子中我们已经示范过了。
+
+(let ((variable 10))
+ (setf variable 2))
+ ; => 2
+
+
+;; 所谓好的Lisp编码风格就是为了减少使用破坏性函数,防止发生副作用。
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 7. 类与对象
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;; 我们就不写什么有关动物的类了,下面给出的人力车的类
+
+(defclass human-powered-conveyance ()
+ ((velocity
+ :accessor velocity
+ :initarg :velocity)
+ (average-efficiency
+ :accessor average-efficiency
+ :initarg :average-efficiency))
+ (:documentation "A human powered conveyance"))
+
+;; `defclass`,后面接类名,以及超类列表
+;; 再接着是槽的列表(槽有点像Java里的成员变量),最后是一些可选的特性
+;; 例如文档说明“:documentation”
+
+;; 如果超类列表为空,则默认该类继承于“standard-object”类(standard-object又是T的子类)
+;; 这种默认行为是可以改变的,但你最好有一定的基础并且知道自己到底在干什么;
+;; 参阅《The Art of the Metaobject Protocol》来了解更多信息。
+
+(defclass bicycle (human-powered-conveyance)
+ ((wheel-size
+ :accessor wheel-size
+ :initarg :wheel-size
+ :documentation "Diameter of the wheel.")
+ (height
+ :accessor height
+ :initarg :height)))
+
+(defclass recumbent (bicycle)
+ ((chain-type
+ :accessor chain-type
+ :initarg :chain-type)))
+
+(defclass unicycle (human-powered-conveyance) nil)
+
+(defclass canoe (human-powered-conveyance)
+ ((number-of-rowers
+ :accessor number-of-rowers
+ :initarg :number-of-rowers)))
+
+
+;; 在REPL中对human-powered-conveyance类调用`DESCRIBE`后结果如下:
+
+(describe 'human-powered-conveyance)
+
+; COMMON-LISP-USER::HUMAN-POWERED-CONVEYANCE
+; [symbol]
+;
+; HUMAN-POWERED-CONVEYANCE names the standard-class #<STANDARD-CLASS
+; HUMAN-POWERED-CONVEYANCE>:
+; Documentation:
+; A human powered conveyance
+; Direct superclasses: STANDARD-OBJECT
+; Direct subclasses: UNICYCLE, BICYCLE, CANOE
+; Not yet finalized.
+; Direct slots:
+; VELOCITY
+; Readers: VELOCITY
+; Writers: (SETF VELOCITY)
+; AVERAGE-EFFICIENCY
+; Readers: AVERAGE-EFFICIENCY
+; Writers: (SETF AVERAGE-EFFICIENCY)
+
+;; 注意到这些有用的返回信息——Common Lisp一直是一个交互式的系统。
+
+;; 若要定义一个方法;
+;; 注意,我们计算自行车轮子周长时使用了这样一个公式:C = d * pi
+
+(defmethod circumference ((object bicycle))
+ (* pi (wheel-size object)))
+
+;; pi在Common Lisp中已经是一个内置的常量。
+
+;; 假设我们已经知道了效率值(“efficiency value”)和船桨数大概呈对数关系;
+;; 那么效率值的定义应当在构造器/初始化过程中就被完成。
+
+;; 下面是一个Common Lisp构造实例时初始化实例的例子:
+
+(defmethod initialize-instance :after ((object canoe) &rest args)
+ (setf (average-efficiency object) (log (1+ (number-of-rowers object)))))
+
+;; 接着初构造一个实例并检查平均效率...
+
+(average-efficiency (make-instance 'canoe :number-of-rowers 15))
+; => 2.7725887
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 8. 宏
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;; 宏可以让你扩展语法
+
+;; 例如,Common Lisp并没有自带WHILE循环——所以让我们自己来为他添加一个;
+;; 如果按照汇编程序的直觉来看,我们会这样写:
+
+(defmacro while (condition &body body)
+ "While `condition` is true, `body` is executed.
+
+`condition` is tested prior to each execution of `body`"
+ (let ((block-name (gensym)))
+ `(tagbody
+ (unless ,condition
+ (go ,block-name))
+ (progn
+ ,@body)
+ ,block-name)))
+
+;; 让我们来看看它的高级版本:
+
+(defmacro while (condition &body body)
+ "While `condition` is true, `body` is executed.
+
+`condition` is tested prior to each execution of `body`"
+ `(loop while ,condition
+ do
+ (progn
+ ,@body)))
+
+;; 然而,在一个比较现代化的编译环境下,这样的WHILE是没有必要的;
+;; LOOP形式的循环和这个WHILE同样的好,并且更易于阅读。
+
+;; 注意反引号'`',逗号','以及'@'这三个符号;
+;; 反引号'`'是一种所谓“quasiquote”的引用类型的运算符,有了它,之后的逗号“,”才有意义。
+;; 逗号“,”意味着解除引用(unquote,即开始求值);
+;; “@”符号则表示将当前的参数插入到当前整个列表中。
+;;(译者注:要想真正用好、用对这三个符号,需要下一番功夫)
+;;(甚至光看《实用 Common Lisp 编程》中关于宏的介绍都是不够的)
+;;(建议再去读一读Paul Graham的两本著作《ANSI Common Lisp》和《On Lisp》)
+
+;; 函数`gensym`创建一个唯一的符号——这个符号确保不会出现在其他任何地方。
+;; 这样做是因为,宏是在编译期展开的
+;; 而在宏中声明的变量名极有可能和常规代码中使用的变量名发生冲突。
+
+;; 可以去《实用 Common Lisp 编程》中阅读更多有关宏的内容。
+```
+
+
+## 拓展阅读
+
+[继续阅读《实用 Common Lisp 编程》一书](http://www.gigamonkeys.com/book/)
+
+
+## 致谢
+
+非常感谢Scheme社区的人们,我基于他们的成果得以迅速的写出这篇有关Common Lisp的快速入门
+同时也感谢
+- [Paul Khuong](https://github.com/pkhuong) ,他提出了很多有用的点评。
+
+##译者寄语
+“祝福那些将思想镶嵌在重重括号之内的人们。”
diff --git a/zh-cn/csharp-cn.html.markdown b/zh-cn/csharp-cn.html.markdown
new file mode 100644
index 00000000..a3cda5b3
--- /dev/null
+++ b/zh-cn/csharp-cn.html.markdown
@@ -0,0 +1,798 @@
+---
+language: c#
+contributors:
+ - ["Irfan Charania", "https://github.com/irfancharania"]
+ - ["Max Yankov", "https://github.com/golergka"]
+ - ["Melvyn Laïly", "http://x2a.yt"]
+ - ["Shaun McCarthy", "http://www.shaunmccarthy.com"]
+translators:
+ - ["Jakukyo Friel", "http://weakish.github.io"]
+filename: LearnCSharp-cn.cs
+lang: zh-cn
+---
+
+
+C#是一个优雅的、类型安全的面向对象语言。使用C#,开发者可以在.NET框架下构建安全、健壮的应用程序。
+
+[更多关于C#的介绍](http://msdn.microsoft.com/en-us/library/vstudio/z1zx9t92.aspx)
+
+```c#
+// 单行注释以 // 开始
+/*
+多行注释是这样的
+*/
+/// <summary>
+/// XML文档注释
+/// </summary>
+
+// 声明应用用到的命名空间
+using System;
+using System.Collections.Generic;
+using System.Data.Entity;
+using System.Dynamic;
+using System.Linq;
+using System.Linq.Expressions;
+using System.Net;
+using System.Threading.Tasks;
+using System.IO;
+
+// 定义作用域,将代码组织成包
+namespace Learning
+{
+ // 每个 .cs 文件至少需要包含一个和文件名相同的类
+ // 你可以不这么干,但是这样不好。
+ public class LearnCSharp
+ {
+ // 基本语法 - 如果你以前用过 Java 或 C++ 的话,可以直接跳到后文「有趣的特性」
+ public static void Syntax()
+ {
+ // 使用 Console.WriteLine 打印信息
+ Console.WriteLine("Hello World");
+ Console.WriteLine(
+ "Integer: " + 10 +
+ " Double: " + 3.14 +
+ " Boolean: " + true);
+
+ // 使用 Console.Write 打印,不带换行符号
+ Console.Write("Hello ");
+ Console.Write("World");
+
+ ///////////////////////////////////////////////////
+ // 类型和变量
+ //
+ // 使用 <type> <name> 定义变量
+ ///////////////////////////////////////////////////
+
+ // Sbyte - 有符号 8-bit 整数
+ // (-128 <= sbyte <= 127)
+ sbyte fooSbyte = 100;
+
+ // Byte - 无符号 8-bit 整数
+ // (0 <= byte <= 255)
+ byte fooByte = 100;
+
+ // Short - 16-bit 整数
+ // 有符号 - (-32,768 <= short <= 32,767)
+ // 无符号 - (0 <= ushort <= 65,535)
+ short fooShort = 10000;
+ ushort fooUshort = 10000;
+
+ // Integer - 32-bit 整数
+ int fooInt = 1; // (-2,147,483,648 <= int <= 2,147,483,647)
+ uint fooUint = 1; // (0 <= uint <= 4,294,967,295)
+
+ // Long - 64-bit 整数
+ long fooLong = 100000L; // (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807)
+ ulong fooUlong = 100000L; // (0 <= ulong <= 18,446,744,073,709,551,615)
+ // 数字默认为 int 或 uint (取决于尺寸)
+ // 使用 L 标明变量值类型为long 或 ulong
+
+ // Double - 双精度 64-bit IEEE 754 浮点数
+ double fooDouble = 123.4; // 精度: 15-16 位
+
+ // Float - 单精度 32-bit IEEE 754 浮点数
+ float fooFloat = 234.5f; // 精度: 7 位
+ // 使用 f 标明变量值类型为float
+
+ // Decimal - 128-bits 数据类型,比其他浮点类型精度更高
+ // 适合财务、金融
+ decimal fooDecimal = 150.3m;
+
+ // 布尔值 - true & false
+ bool fooBoolean = true; // 或 false
+
+ // Char - 单个 16-bit Unicode 字符
+ char fooChar = 'A';
+
+ // 字符串 -- 和前面的基本类型不同,字符串不是值,而是引用。
+ // 这意味着你可以将字符串设为null。
+ string fooString = "\"escape\" quotes and add \n (new lines) and \t (tabs)";
+ Console.WriteLine(fooString);
+
+ // 你可以通过索引访问字符串的每个字符:
+ char charFromString = fooString[1]; // => 'e'
+ // 字符串不可修改: fooString[1] = 'X' 是行不通的;
+
+ // 根据当前的locale设定比较字符串,大小写不敏感
+ string.Compare(fooString, "x", StringComparison.CurrentCultureIgnoreCase);
+
+ // 基于sprintf的字符串格式化
+ string fooFs = string.Format("Check Check, {0} {1}, {0} {1:0.0}", 1, 2);
+
+ // 日期和格式
+ DateTime fooDate = DateTime.Now;
+ Console.WriteLine(fooDate.ToString("hh:mm, dd MMM yyyy"));
+
+ // 使用 @ 符号可以创建跨行的字符串。使用 "" 来表示 "
+ string bazString = @"Here's some stuff
+on a new line! ""Wow!"", the masses cried";
+
+ // 使用const或read-only定义常量
+ // 常量在编译期演算
+ const int HOURS_I_WORK_PER_WEEK = 9001;
+
+ ///////////////////////////////////////////////////
+ // 数据结构
+ ///////////////////////////////////////////////////
+
+ // 数组 - 从0开始计数
+ // 声明数组时需要确定数组长度
+ // 声明数组的格式如下:
+ // <datatype>[] <var name> = new <datatype>[<array size>];
+ int[] intArray = new int[10];
+
+ // 声明并初始化数组的其他方式:
+ int[] y = { 9000, 1000, 1337 };
+
+ // 访问数组的元素
+ Console.WriteLine("intArray @ 0: " + intArray[0]);
+ // 数组可以修改
+ intArray[1] = 1;
+
+ // 列表
+ // 列表比数组更常用,因为列表更灵活。
+ // 声明列表的格式如下:
+ // List<datatype> <var name> = new List<datatype>();
+ List<int> intList = new List<int>();
+ List<string> stringList = new List<string>();
+ List<int> z = new List<int> { 9000, 1000, 1337 }; // i
+ // <>用于泛型 - 参考下文
+
+ // 列表无默认值
+ // 访问列表元素时必须首先添加元素
+ intList.Add(1);
+ Console.WriteLine("intList @ 0: " + intList[0]);
+
+ // 其他数据结构:
+ // 堆栈/队列
+ // 字典 (哈希表的实现)
+ // 哈希集合
+ // 只读集合
+ // 元组 (.Net 4+)
+
+ ///////////////////////////////////////
+ // 操作符
+ ///////////////////////////////////////
+ Console.WriteLine("\n->Operators");
+
+ int i1 = 1, i2 = 2; // 多重声明的简写形式
+
+ // 算术直截了当
+ Console.WriteLine(i1 + i2 - i1 * 3 / 7); // => 3
+
+ // 取余
+ Console.WriteLine("11%3 = " + (11 % 3)); // => 2
+
+ // 比较操作符
+ Console.WriteLine("3 == 2? " + (3 == 2)); // => false
+ Console.WriteLine("3 != 2? " + (3 != 2)); // => true
+ Console.WriteLine("3 > 2? " + (3 > 2)); // => true
+ Console.WriteLine("3 < 2? " + (3 < 2)); // => false
+ Console.WriteLine("2 <= 2? " + (2 <= 2)); // => true
+ Console.WriteLine("2 >= 2? " + (2 >= 2)); // => true
+
+ // 位操作符
+ /*
+ ~ 取反
+ << 左移(有符号)
+ >> 右移(有符号)
+ & 与
+ ^ 异或
+ | 或
+ */
+
+ // 自增、自减
+ int i = 0;
+ Console.WriteLine("\n->Inc/Dec-rementation");
+ Console.WriteLine(i++); //i = 1. 事后自增
+ Console.WriteLine(++i); //i = 2. 事先自增
+ Console.WriteLine(i--); //i = 1. 事后自减
+ Console.WriteLine(--i); //i = 0. 事先自减
+
+ ///////////////////////////////////////
+ // 控制结构
+ ///////////////////////////////////////
+ Console.WriteLine("\n->Control Structures");
+
+ // 类似C的if语句
+ int j = 10;
+ if (j == 10)
+ {
+ Console.WriteLine("I get printed");
+ }
+ else if (j > 10)
+ {
+ Console.WriteLine("I don't");
+ }
+ else
+ {
+ Console.WriteLine("I also don't");
+ }
+
+ // 三元表达式
+ // 简单的 if/else 语句可以写成:
+ // <条件> ? <真> : <假>
+ string isTrue = (true) ? "True" : "False";
+
+ // While 循环
+ int fooWhile = 0;
+ while (fooWhile < 100)
+ {
+ //迭代 100 次, fooWhile 0->99
+ fooWhile++;
+ }
+
+ // Do While 循环
+ int fooDoWhile = 0;
+ do
+ {
+ //迭代 100 次, fooDoWhile 0->99
+ fooDoWhile++;
+ } while (fooDoWhile < 100);
+
+ //for 循环结构 => for(<初始条件>; <条件>; <步>)
+ for (int fooFor = 0; fooFor < 10; fooFor++)
+ {
+ //迭代10次, fooFor 0->9
+ }
+
+ // foreach循环
+ // foreach 循环结构 => foreach(<迭代器类型> <迭代器> in <可枚举结构>)
+ // foreach 循环适用于任何实现了 IEnumerable 或 IEnumerable<T> 的对象。
+ // .Net 框架下的集合类型(数组, 列表, 字典...)
+ // 都实现了这些接口
+ // (下面的代码中,ToCharArray()可以删除,因为字符串同样实现了IEnumerable)
+ foreach (char character in "Hello World".ToCharArray())
+ {
+ //迭代字符串中的所有字符
+ }
+
+ // Switch 语句
+ // switch 适用于 byte、short、char和int 数据类型。
+ // 同样适用于可枚举的类型
+ // 包括字符串类, 以及一些封装了原始值的类:
+ // Character、Byte、Short和Integer。
+ int month = 3;
+ string monthString;
+ switch (month)
+ {
+ case 1:
+ monthString = "January";
+ break;
+ case 2:
+ monthString = "February";
+ break;
+ case 3:
+ monthString = "March";
+ break;
+ // 你可以一次匹配多个case语句
+ // 但是你在添加case语句后需要使用break
+ // (否则你需要显式地使用goto case x语句)
+ case 6:
+ case 7:
+ case 8:
+ monthString = "Summer time!!";
+ break;
+ default:
+ monthString = "Some other month";
+ break;
+ }
+
+ ///////////////////////////////////////
+ // 转换、指定数据类型
+ ///////////////////////////////////////
+
+ // 转换类型
+
+ // 转换字符串为整数
+ // 转换失败会抛出异常
+ int.Parse("123");//返回整数类型的"123"
+
+ // TryParse会尝试转换类型,失败时会返回缺省类型
+ // 例如 0
+ int tryInt;
+ if (int.TryParse("123", out tryInt)) // Funciton is boolean
+ Console.WriteLine(tryInt); // 123
+
+ // 转换整数为字符串
+ // Convert类提供了一系列便利转换的方法
+ Convert.ToString(123);
+ // or
+ tryInt.ToString();
+ }
+
+ ///////////////////////////////////////
+ // 类
+ ///////////////////////////////////////
+ public static void Classes()
+ {
+ // 参看文件尾部的对象声明
+
+ // 使用new初始化对象
+ Bicycle trek = new Bicycle();
+
+ // 调用对象的方法
+ trek.SpeedUp(3); // 你应该一直使用setter和getter方法
+ trek.Cadence = 100;
+
+ // 查看对象的信息.
+ Console.WriteLine("trek info: " + trek.Info());
+
+ // 实例化一个新的Penny Farthing
+ PennyFarthing funbike = new PennyFarthing(1, 10);
+ Console.WriteLine("funbike info: " + funbike.Info());
+
+ Console.Read();
+ } // 结束main方法
+
+ // 终端程序 终端程序必须有一个main方法作为入口
+ public static void Main(string[] args)
+ {
+ OtherInterestingFeatures();
+ }
+
+ //
+ // 有趣的特性
+ //
+
+ // 默认方法签名
+
+ public // 可见性
+ static // 允许直接调用类,无需先创建实例
+ int, //返回值
+ MethodSignatures(
+ int maxCount, // 第一个变量,类型为整型
+ int count = 0, // 如果没有传入值,则缺省值为0
+ int another = 3,
+ params string[] otherParams // 捕获其他参数
+ )
+ {
+ return -1;
+ }
+
+ // 方法可以重名,只要签名不一样
+ public static void MethodSignature(string maxCount)
+ {
+ }
+
+ //泛型
+ // TKey和TValue类由用用户调用函数时指定。
+ // 以下函数模拟了Python的SetDefault
+ public static TValue SetDefault<TKey, TValue>(
+ IDictionary<TKey, TValue> dictionary,
+ TKey key,
+ TValue defaultItem)
+ {
+ TValue result;
+ if (!dictionary.TryGetValue(key, out result))
+ return dictionary[key] = defaultItem;
+ return result;
+ }
+
+ // 你可以限定传入值的范围
+ public static void IterateAndPrint<T>(T toPrint) where T: IEnumerable<int>
+ {
+ // 我们可以进行迭代,因为T是可枚举的
+ foreach (var item in toPrint)
+ // ittm为整数
+ Console.WriteLine(item.ToString());
+ }
+
+ public static void OtherInterestingFeatures()
+ {
+ // 可选参数
+ MethodSignatures(3, 1, 3, "Some", "Extra", "Strings");
+ MethodSignatures(3, another: 3); // 显式指定参数,忽略可选参数
+
+ // 扩展方法
+ int i = 3;
+ i.Print(); // 参见下面的定义
+
+ // 可为null的类型 对数据库交互、返回值很有用
+ // 任何值类型 (i.e. 不为类) 添加后缀 ? 后会变为可为null的值
+ // <类型>? <变量名> = <值>
+ int? nullable = null; // Nullable<int> 的简写形式
+ Console.WriteLine("Nullable variable: " + nullable);
+ bool hasValue = nullable.HasValue; // 不为null时返回真
+ // ?? 是用于指定默认值的语法糖
+ // 以防变量为null的情况
+ int notNullable = nullable ?? 0; // 0
+
+ // 变量类型推断 - 你可以让编译器推断变量类型:
+ var magic = "编译器确定magic是一个字符串,所以仍然是类型安全的";
+ // magic = 9; // 不工作,因为magic是字符串,而不是整数。
+
+ // 泛型
+ //
+ var phonebook = new Dictionary<string, string>() {
+ {"Sarah", "212 555 5555"} // 在电话簿中加入新条目
+ };
+
+ // 调用上面定义为泛型的SETDEFAULT
+ Console.WriteLine(SetDefault<string,string>(phonebook, "Shaun", "No Phone")); // 没有电话
+ // 你不用指定TKey、TValue,因为它们会被隐式地推导出来
+ Console.WriteLine(SetDefault(phonebook, "Sarah", "No Phone")); // 212 555 5555
+
+ // lambda表达式 - 允许你用一行代码搞定函数
+ Func<int, int> square = (x) => x * x; // 最后一项为返回值
+ Console.WriteLine(square(3)); // 9
+
+ // 可抛弃的资源管理 - 让你很容易地处理未管理的资源
+ // 大多数访问未管理资源 (文件操作符、设备上下文, etc.)的对象
+ // 都实现了IDisposable接口。
+ // using语句会为你清理IDisposable对象。
+ using (StreamWriter writer = new StreamWriter("log.txt"))
+ {
+ writer.WriteLine("这里没有什么可疑的东西");
+ // 在作用域的结尾,资源会被回收
+ // (即使有异常抛出,也一样会回收)
+ }
+
+ // 并行框架
+ // http://blogs.msdn.com/b/csharpfaq/archive/2010/06/01/parallel-programming-in-net-framework-4-getting-started.aspx
+ var websites = new string[] {
+ "http://www.google.com", "http://www.reddit.com",
+ "http://www.shaunmccarthy.com"
+ };
+ var responses = new Dictionary<string, string>();
+
+ // 为每个请求新开一个线程
+ // 在运行下一步前合并结果
+ Parallel.ForEach(websites,
+ new ParallelOptions() {MaxDegreeOfParallelism = 3}, // max of 3 threads
+ website =>
+ {
+ // Do something that takes a long time on the file
+ using (var r = WebRequest.Create(new Uri(website)).GetResponse())
+ {
+ responses[website] = r.ContentType;
+ }
+ });
+
+ // 直到所有的请求完成后才会运行下面的代码
+ foreach (var key in responses.Keys)
+ Console.WriteLine("{0}:{1}", key, responses[key]);
+
+ // 动态对象(配合其他语言使用很方便)
+ dynamic student = new ExpandoObject();
+ student.FirstName = "First Name"; // 不需要先定义类!
+
+ // 你甚至可以添加方法(接受一个字符串,输出一个字符串)
+ student.Introduce = new Func<string, string>(
+ (introduceTo) => string.Format("Hey {0}, this is {1}", student.FirstName, introduceTo));
+ Console.WriteLine(student.Introduce("Beth"));
+
+ // IQUERYABLE<T> - 几乎所有的集合都实现了它,
+ // 带给你 Map / Filter / Reduce 风格的方法
+ var bikes = new List<Bicycle>();
+ bikes.Sort(); // Sorts the array
+ bikes.Sort((b1, b2) => b1.Wheels.CompareTo(b2.Wheels)); // 根据车轮数排序
+ var result = bikes
+ .Where(b => b.Wheels > 3) // 筛选 - 可以连锁使用 (返回IQueryable)
+ .Where(b => b.IsBroken && b.HasTassles)
+ .Select(b => b.ToString()); // Map - 这里我们使用了select,所以结果是IQueryable<string>
+
+ var sum = bikes.Sum(b => b.Wheels); // Reduce - 计算集合中的轮子总数
+
+ // 创建一个包含基于自行车的一些参数生成的隐式对象的列表
+ var bikeSummaries = bikes.Select(b=>new { Name = b.Name, IsAwesome = !b.IsBroken && b.HasTassles });
+ // 很难演示,但是编译器在代码编译完成前就能推导出以上对象的类型
+ foreach (var bikeSummary in bikeSummaries.Where(b => b.IsAwesome))
+ Console.WriteLine(bikeSummary.Name);
+
+ // ASPARALLEL
+ // 邪恶的特性 —— 组合了linq和并行操作
+ var threeWheelers = bikes.AsParallel().Where(b => b.Wheels == 3).Select(b => b.Name);
+ // 以上代码会并发地运行。会自动新开线程,分别计算结果。
+ // 适用于多核、大数据量的场景。
+
+ // LINQ - 将IQueryable<T>映射到存储,延缓执行
+ // 例如 LinqToSql 映射数据库, LinqToXml 映射XML文档
+ var db = new BikeRespository();
+
+ // 执行被延迟了,这对于查询数据库来说很好
+ var filter = db.Bikes.Where(b => b.HasTassles); // 不运行查询
+ if (42 > 6) // 你可以不断地增加筛选,包括有条件的筛选,例如用于“高级搜索”功能
+ filter = filter.Where(b => b.IsBroken); // 不运行查询
+
+ var query = filter
+ .OrderBy(b => b.Wheels)
+ .ThenBy(b => b.Name)
+ .Select(b => b.Name); // 仍然不运行查询
+
+ // 现在运行查询,运行查询的时候会打开一个读取器,所以你迭代的是一个副本
+ foreach (string bike in query)
+ Console.WriteLine(result);
+
+
+
+ }
+
+ } // 结束LearnCSharp类
+
+ // 你可以在同一个 .cs 文件中包含其他类
+
+ public static class Extensions
+ {
+ // 扩展函数
+ public static void Print(this object obj)
+ {
+ Console.WriteLine(obj.ToString());
+ }
+ }
+ // 声明类的语法:
+ // <public/private/protected/internal> class <类名>{
+ // //数据字段, 构造器, 内部函数.
+ / // 在Java中函数被称为方法。
+ // }
+
+ public class Bicycle
+ {
+ // 自行车的字段、变量
+ public int Cadence // Public: 任何地方都可以访问
+ {
+ get // get - 定义获取属性的方法
+ {
+ return _cadence;
+ }
+ set // set - 定义设置属性的方法
+ {
+ _cadence = value; // value是被传递给setter的值
+ }
+ }
+ private int _cadence;
+
+ protected virtual int Gear // 类和子类可以访问
+ {
+ get; // 创建一个自动属性,无需成员字段
+ set;
+ }
+
+ internal int Wheels // Internal:在同一程序集内可以访问
+ {
+ get;
+ private set; // 可以给get/set方法添加修饰符
+ }
+
+ int _speed; // 默认为private: 只可以在这个类内访问,你也可以使用`private`关键词
+ public string Name { get; set; }
+
+ // enum类型包含一组常量
+ // 它将名称映射到值(除非特别说明,是一个整型)
+ // enmu元素的类型可以是byte、sbyte、short、ushort、int、uint、long、ulong。
+ // enum不能包含相同的值。
+ public enum BikeBrand
+ {
+ AIST,
+ BMC,
+ Electra = 42, //你可以显式地赋值
+ Gitane // 43
+ }
+ // 我们在Bicycle类中定义的这个类型,所以它是一个内嵌类型。
+ // 这个类以外的代码应当使用`Bicycle.Brand`来引用。
+
+ public BikeBrand Brand; // 声明一个enum类型之后,我们可以声明这个类型的字段
+
+ // 静态方法的类型为自身,不属于特定的对象。
+ // 你无需引用对象就可以访问他们。
+ // Console.WriteLine("Bicycles created: " + Bicycle.bicyclesCreated);
+ static public int BicyclesCreated = 0;
+
+ // 只读值在运行时确定
+ // 它们只能在声明或构造器内被赋值
+ readonly bool _hasCardsInSpokes = false; // read-only private
+
+ // 构造器是创建类的一种方式
+ // 下面是一个默认的构造器
+ public Bicycle()
+ {
+ this.Gear = 1; // 你可以使用关键词this访问对象的成员
+ Cadence = 50; // 不过你并不总是需要它
+ _speed = 5;
+ Name = "Bontrager";
+ Brand = BikeBrand.AIST;
+ BicyclesCreated++;
+ }
+
+ // 另一个构造器的例子(包含参数)
+ public Bicycle(int startCadence, int startSpeed, int startGear,
+ string name, bool hasCardsInSpokes, BikeBrand brand)
+ : base() // 首先调用base
+ {
+ Gear = startGear;
+ Cadence = startCadence;
+ _speed = startSpeed;
+ Name = name;
+ _hasCardsInSpokes = hasCardsInSpokes;
+ Brand = brand;
+ }
+
+ // 构造器可以连锁使用
+ public Bicycle(int startCadence, int startSpeed, BikeBrand brand) :
+ this(startCadence, startSpeed, 0, "big wheels", true, brand)
+ {
+ }
+
+ // 函数语法
+ // <public/private/protected> <返回值> <函数名称>(<参数>)
+
+ // 类可以为字段实现 getters 和 setters 方法 for their fields
+ // 或者可以实现属性(C#推荐使用这个)
+ // 方法的参数可以有默认值
+ // 在有默认值的情况下,调用方法的时候可以省略相应的参数
+ public void SpeedUp(int increment = 1)
+ {
+ _speed += increment;
+ }
+
+ public void SlowDown(int decrement = 1)
+ {
+ _speed -= decrement;
+ }
+
+ // 属性可以访问和设置值
+ // 当只需要访问数据的时候,考虑使用属性。
+ // 属性可以定义get和set,或者是同时定义两者
+ private bool _hasTassles; // private variable
+ public bool HasTassles // public accessor
+ {
+ get { return _hasTassles; }
+ set { _hasTassles = value; }
+ }
+
+ // 你可以在一行之内定义自动属性
+ // 这个语法会自动创建后备字段
+ // 你可以给getter或setter设置访问修饰符
+ // 以便限制它们的访问
+ public bool IsBroken { get; private set; }
+
+ // 属性的实现可以是自动的
+ public int FrameSize
+ {
+ get;
+ // 你可以给get或set指定访问修饰符
+ // 以下代码意味着只有Bicycle类可以调用Framesize的set
+ private set;
+ }
+
+ //显示对象属性的方法
+ public virtual string Info()
+ {
+ return "Gear: " + Gear +
+ " Cadence: " + Cadence +
+ " Speed: " + _speed +
+ " Name: " + Name +
+ " Cards in Spokes: " + (_hasCardsInSpokes ? "yes" : "no") +
+ "\n------------------------------\n"
+ ;
+ }
+
+ // 方法可以是静态的。通常用于辅助方法。
+ public static bool DidWeCreateEnoughBycles()
+ {
+ // 在静态方法中,你只能引用类的静态成员
+ return BicyclesCreated > 9000;
+ } // 如果你的类只需要静态成员,考虑将整个类作为静态类。
+
+
+ } // Bicycle类结束
+
+ // PennyFarthing是Bicycle的一个子类
+ class PennyFarthing : Bicycle
+ {
+ // (Penny Farthings是一种前轮很大的自行车。没有齿轮。)
+
+ // 调用父构造器
+ public PennyFarthing(int startCadence, int startSpeed) :
+ base(startCadence, startSpeed, 0, "PennyFarthing", true, BikeBrand.Electra)
+ {
+ }
+
+ protected override int Gear
+ {
+ get
+ {
+ return 0;
+ }
+ set
+ {
+ throw new ArgumentException("你不可能在PennyFarthing上切换齿轮");
+ }
+ }
+
+ public override string Info()
+ {
+ string result = "PennyFarthing bicycle ";
+ result += base.ToString(); // 调用父方法
+ return result;
+ }
+ }
+
+ // 接口只包含成员的签名,而没有实现。
+ interface IJumpable
+ {
+ void Jump(int meters); // 所有接口成员是隐式地公开的
+ }
+
+ interface IBreakable
+ {
+ bool Broken { get; } // 接口可以包含属性、方法和事件
+ }
+
+ // 类只能继承一个类,但是可以实现任意数量的接口
+ {
+ int damage = 0;
+
+ public void Jump(int meters)
+ {
+ damage += meters;
+ }
+
+ public bool Broken
+ {
+ get
+ {
+ return damage > 100;
+ }
+ }
+ }
+
+ /// <summary>
+ /// 连接数据库,一个 LinqToSql的示例。
+ /// EntityFramework Code First 很棒 (类似 Ruby的 ActiveRecord, 不过是双向的)
+ /// http://msdn.microsoft.com/en-us/data/jj193542.aspx
+ /// </summary>
+ public class BikeRespository : DbSet
+ {
+ public BikeRespository()
+ : base()
+ {
+ }
+
+ public DbSet<Bicycle> Bikes { get; set; }
+ }
+} // 结束 Namespace
+```
+
+## 没有涉及到的主题
+
+ * Flags
+ * Attributes
+ * 静态属性
+ * Exceptions, Abstraction
+ * ASP.NET (Web Forms/MVC/WebMatrix)
+ * Winforms
+ * Windows Presentation Foundation (WPF)
+
+## 扩展阅读
+
+ * [DotNetPerls](http://www.dotnetperls.com)
+ * [C# in Depth](http://manning.com/skeet2)
+ * [Programming C#](http://shop.oreilly.com/product/0636920024064.do)
+ * [LINQ](http://shop.oreilly.com/product/9780596519254.do)
+ * [MSDN Library](http://msdn.microsoft.com/en-us/library/618ayhy6.aspx)
+ * [ASP.NET MVC Tutorials](http://www.asp.net/mvc/tutorials)
+ * [ASP.NET Web Matrix Tutorials](http://www.asp.net/web-pages/tutorials)
+ * [ASP.NET Web Forms Tutorials](http://www.asp.net/web-forms/tutorials)
+ * [Windows Forms Programming in C#](http://www.amazon.com/Windows-Forms-Programming-Chris-Sells/dp/0321116208)
+ * [C# Coding Conventions](http://msdn.microsoft.com/en-us/library/vstudio/ff926074.aspx)
diff --git a/zh-cn/css-cn.html.markdown b/zh-cn/css-cn.html.markdown
new file mode 100644
index 00000000..dc6dcc4f
--- /dev/null
+++ b/zh-cn/css-cn.html.markdown
@@ -0,0 +1,212 @@
+---
+language: css
+contributors:
+ - ["Mohammad Valipour", "https://github.com/mvalipour"]
+ - ["Marco Scannadinari", "https://github.com/marcoms"]
+translators:
+ - ["Jakukyo Friel", "https://weakish.github.io"]
+lang: zh-cn
+filename: learncss-cn.css
+---
+
+早期的web没有样式,只是单纯的文本。通过CSS,可以实现网页样式和内容的分离。
+
+简单来说,CSS可以指定HTML页面上的元素所使用的样式。
+
+和其他语言一样,CSS有很多版本。最新的版本是CSS 3. CSS 2.0兼容性最好。
+
+你可以使用[dabblet](http://dabblet.com/)来在线测试CSS的效果。
+
+```css
+/* 注释 */
+
+/* ####################
+ ## 选择器
+ ####################*/
+
+/* 一般而言,CSS的声明语句非常简单。 */
+选择器 { 属性: 值; /* 更多属性...*/ }
+
+/* 选择器用于指定页面上的元素。
+
+针对页面上的所有元素。 */
+* { color:red; }
+
+/*
+假定页面上有这样一个元素
+
+<div class='some-class class2' id='someId' attr='value' />
+*/
+
+/* 你可以通过类名来指定它 */
+.some-class { }
+
+/* 给出所有类名 */
+.some-class.class2 { }
+
+/* 标签名 */
+div { }
+
+/* id */
+#someId { }
+
+/* 由于元素包含attr属性,因此也可以通过这个来指定 */
+[attr] { font-size:smaller; }
+
+/* 以及有特定值的属性 */
+[attr='value'] { font-size:smaller; }
+
+/* 通过属性的值的开头指定 */
+[attr^='val'] { font-size:smaller; }
+
+/* 通过属性的值的结尾来指定 */
+[attr$='ue'] { font-size:smaller; }
+
+/* 通过属性的值的部分来指定 */
+[attr~='lu'] { font-size:smaller; }
+
+
+/* 你可以把这些全部结合起来,注意不同部分间不应该有空格,否则会改变语义 */
+div.some-class[attr$='ue'] { }
+
+/* 你也可以通过父元素来指定。*/
+
+/* 某个元素是另一个元素的直接子元素 */
+div.some-parent > .class-name {}
+
+/* 或者通过该元素的祖先元素 */
+div.some-parent .class-name {}
+
+/* 注意,去掉空格后语义就不同了。
+你能说出哪里不同么? */
+div.some-parent.class-name {}
+
+/* 你可以选择某元素前的相邻元素 */
+.i-am-before + .this-element { }
+
+/* 某元素之前的同级元素(相邻或不相邻) */
+.i-am-any-before ~ .this-element {}
+
+/* 伪类允许你基于页面的行为指定元素(而不是基于页面结构) */
+
+/* 例如,当鼠标悬停在某个元素上时 */
+:hover {}
+
+/* 已访问过的链接*/
+:visited {}
+
+/* 未访问过的链接*/
+:link {}
+
+/* 当前焦点的input元素 */
+:focus {}
+
+
+/* ####################
+ ## 属性
+ ####################*/
+
+选择器 {
+
+ /* 单位 */
+ width: 50%; /* 百分比 */
+ font-size: 2em; /* 当前字体大小的两倍 */
+ width: 200px; /* 像素 */
+ font-size: 20pt; /* 点 */
+ width: 5cm; /* 厘米 */
+ width: 50mm; /* 毫米 */
+ width: 5in; /* 英尺 */
+
+ /* 颜色 */
+ background-color: #F6E; /* 短16位 */
+ background-color: #F262E2; /* 长16位 */
+ background-color: tomato; /* 颜色名称 */
+ background-color: rgb(255, 255, 255); /* rgb */
+ background-color: rgb(10%, 20%, 50%); /* rgb 百分比 */
+ background-color: rgba(255, 0, 0, 0.3); /* rgb 加透明度 */
+
+ /* 图片 */
+ background-image: url(/path-to-image/image.jpg);
+
+ /* 字体 */
+ font-family: Arial;
+ font-family: "Courier New"; /* 使用双引号包裹含空格的字体名称 */
+ font-family: "Courier New", Trebuchet, Arial; /* 如果第一个
+ 字体没找到,浏览器会使用第二个字体,一次类推 */
+}
+
+```
+
+## 使用
+
+CSS文件使用 `.css` 后缀。
+
+```xml
+<!-- 你需要在文件的 <head> 引用CSS文件 -->
+<link rel='stylesheet' type='text/css' href='filepath/filename.css' />
+
+<!-- 你也可以在标记中内嵌CSS。不过强烈建议不要这么干。 -->
+<style>
+ 选择器 { 属性:值; }
+</style>
+
+<!-- 也可以直接使用元素的style属性。
+这是你最不该干的事情。 -->
+<div style='property:value;'>
+</div>
+
+```
+
+## 优先级
+
+同一个元素可能被多个不同的选择器指定,因此可能会有冲突。
+
+假定CSS是这样的:
+
+```css
+/*A*/
+p.class1[attr='value']
+
+/*B*/
+p.class1 {}
+
+/*C*/
+p.class2 {}
+
+/*D*/
+p {}
+
+/*E*/
+p { property: value !important; }
+
+```
+
+然后标记语言为:
+
+```xml
+<p style='/*F*/ property:value;' class='class1 class2' attr='value'>
+</p>
+```
+
+那么将会按照下面的顺序应用风格:
+
+
+* `E` 优先级最高,因为它使用了 `!important`,除非很有必要,尽量避免使用这个。
+* `F` 其次,因为它是嵌入的风格。
+* `A` 其次,因为它比其他指令更具体。
+* `C` 其次,虽然它的具体程度和`B`一样,但是它在`B`之后。
+* 接下来是 `B`。
+* 最后是 `D`。
+
+## 兼容性
+
+CSS2 的绝大部分特性兼容各种浏览器和设备。现在 CSS3 的兼容性也越来越好了。
+但是兼容性问题仍然是需要留意的一个问题。
+
+[QuirksMode CSS](http://www.quirksmode.org/css/)是关于这方面最好的资源。
+
+## 扩展阅读
+
+* [理解CSS的风格优先级: 特定性, 继承和层叠](http://www.vanseodesign.com/css/css-specificity-inheritance-cascaade/)
+* [QuirksMode CSS](http://www.quirksmode.org/css/)
+* [Z-Index - The stacking context](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Understanding_z_index/The_stacking_context)
diff --git a/zh-cn/elisp-cn.html.markdown b/zh-cn/elisp-cn.html.markdown
index d303c2e8..06f38d77 100755..100644
--- a/zh-cn/elisp-cn.html.markdown
+++ b/zh-cn/elisp-cn.html.markdown
@@ -132,7 +132,7 @@ lang: zh-cn
;; `C-xC-e' 这时屏幕上会显示两个窗口,而光标此时位于*test* buffer内
;; 用鼠标单击上面的buffer就会使光标移回。
-;; 或者你可以使用 `C-xo' 是的光标跳到另一个窗口中
+;; 或者你可以使用 `C-xo' 使得光标跳到另一个窗口中
;; 你可以用 `progn'命令将s式结合起来:
(progn
@@ -219,7 +219,7 @@ lang: zh-cn
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
-;; 我们将一些名字存到列表中;
+;; 我们将一些名字存到列表中:
(setq list-of-names '("Sarah" "Chloe" "Mathilde"))
;; 用 `car'来取得第一个名字:
diff --git a/zh-cn/elixir-cn.html.markdown b/zh-cn/elixir-cn.html.markdown
new file mode 100644
index 00000000..daee8d3c
--- /dev/null
+++ b/zh-cn/elixir-cn.html.markdown
@@ -0,0 +1,399 @@
+---
+language: elixir
+contributors:
+ - ["Joao Marques", "http://github.com/mrshankly"]
+translators:
+ - ["lidashuang", "http://github.com/lidashuang"]
+filename: learnelixir-cn.ex
+lang: zh-cn
+---
+
+Elixir 是一门构建在Erlang VM 之上的函数式编程语言。Elixir 完全兼容 Erlang,
+另外还提供了更标准的语法,特性。
+
+```elixir
+
+# 这是单行注释, 注释以井号开头
+
+# 没有多行注释
+# 但你可以堆叠多个注释。
+
+# elixir shell 使用命令 `iex` 进入。
+# 编译模块使用 `elixirc` 命令。
+
+# 如果安装正确,这些命令都会在环境变量里
+
+## ---------------------------
+## -- 基本类型
+## ---------------------------
+
+# 数字
+3 # 整型
+0x1F # 整型
+3.0 # 浮点类型
+
+# 原子(Atoms),以 `:`开头
+:hello # atom
+
+# 元组(Tuple) 在内存中的存储是连续的
+{1,2,3} # tuple
+
+# 使用`elem`函数访问元组(tuple)里的元素:
+elem({1, 2, 3}, 0) #=> 1
+
+# 列表(list)
+[1,2,3] # list
+
+# 可以用下面的方法访问列表的头尾元素:
+[head | tail] = [1,2,3]
+head #=> 1
+tail #=> [2,3]
+
+# 在elixir,就像在Erlang, `=` 表示模式匹配 (pattern matching)
+# 不是赋值。
+#
+# 这表示会用左边的模式(pattern)匹配右侧
+#
+# 上面的例子中访问列表的头部和尾部就是这样工作的。
+
+# 当左右两边不匹配时,会返回error, 在这个
+# 例子中,元组大小不一样。
+# {a, b, c} = {1, 2} #=> ** (MatchError) no match of right hand side value: {1,2}
+
+# 还有二进制类型 (binaries)
+<<1,2,3>> # binary
+
+# 字符串(Strings) 和 字符列表(char lists)
+"hello" # string
+'hello' # char list
+
+# 多行字符串
+"""
+I'm a multi-line
+string.
+"""
+#=> "I'm a multi-line\nstring.\n"
+
+# 所有的字符串(Strings)以UTF-8编码:
+"héllò" #=> "héllò"
+
+# 字符串(Strings)本质就是二进制类型(binaries), 字符列表(char lists)本质是列表(lists)
+<<?a, ?b, ?c>> #=> "abc"
+[?a, ?b, ?c] #=> 'abc'
+
+# 在 elixir中,`?a`返回 `a` 的 ASCII 整型值
+?a #=> 97
+
+# 合并列表使用 `++`, 对于二进制类型则使用 `<>`
+[1,2,3] ++ [4,5] #=> [1,2,3,4,5]
+'hello ' ++ 'world' #=> 'hello world'
+
+<<1,2,3>> <> <<4,5>> #=> <<1,2,3,4,5>>
+"hello " <> "world" #=> "hello world"
+
+## ---------------------------
+## -- 操作符(Operators)
+## ---------------------------
+
+# 一些数学运算
+1 + 1 #=> 2
+10 - 5 #=> 5
+5 * 2 #=> 10
+10 / 2 #=> 5.0
+
+# 在 elixir 中,操作符 `/` 返回值总是浮点数。
+
+# 做整数除法使用 `div`
+div(10, 2) #=> 5
+
+# 为了得到余数使用 `rem`
+rem(10, 3) #=> 1
+
+# 还有 boolean 操作符: `or`, `and` and `not`.
+# 第一个参数必须是boolean 类型
+true and true #=> true
+false or true #=> true
+# 1 and true #=> ** (ArgumentError) argument error
+
+# Elixir 也提供了 `||`, `&&` 和 `!` 可以接受任意的类型
+# 除了`false` 和 `nil` 其它都会被当作true.
+1 || true #=> 1
+false && 1 #=> false
+nil && 20 #=> nil
+
+!true #=> false
+
+# 比较有: `==`, `!=`, `===`, `!==`, `<=`, `>=`, `<` 和 `>`
+1 == 1 #=> true
+1 != 1 #=> false
+1 < 2 #=> true
+
+# `===` 和 `!==` 在比较整型和浮点类型时更为严格:
+1 == 1.0 #=> true
+1 === 1.0 #=> false
+
+# 我们也可以比较两种不同的类型:
+1 < :hello #=> true
+
+# 总的排序顺序定义如下:
+# number < atom < reference < functions < port < pid < tuple < list < bit string
+
+# 引用Joe Armstrong :“实际的顺序并不重要,
+# 但是,一个整体排序是否经明确界定是非常重要的。”
+
+## ---------------------------
+## -- 控制结构(Control Flow)
+## ---------------------------
+
+# `if` 表达式
+if false do
+ "This will never be seen"
+else
+ "This will"
+end
+
+# 还有 `unless`
+unless true do
+ "This will never be seen"
+else
+ "This will"
+end
+
+# 在Elixir中,很多控制结构都依赖于模式匹配
+
+# `case` 允许我们把一个值与多种模式进行比较:
+case {:one, :two} do
+ {:four, :five} ->
+ "This won't match"
+ {:one, x} ->
+ "This will match and assign `x` to `:two`"
+ _ ->
+ "This will match any value"
+end
+
+# 模式匹配时,如果不需要某个值,通用的做法是把值 匹配到 `_`
+# 例如,我们只需要要列表的头元素:
+[head | _] = [1,2,3]
+head #=> 1
+
+# 下面的方式效果一样,但可读性更好
+[head | _tail] = [:a, :b, :c]
+head #=> :a
+
+# `cond` 可以检测多种不同的分支
+# 使用 `cond` 代替多个`if` 表达式嵌套
+cond do
+ 1 + 1 == 3 ->
+ "I will never be seen"
+ 2 * 5 == 12 ->
+ "Me neither"
+ 1 + 2 == 3 ->
+ "But I will"
+end
+
+# 经常可以看到最后一个条件等于'true',这将总是匹配。
+cond do
+ 1 + 1 == 3 ->
+ "I will never be seen"
+ 2 * 5 == 12 ->
+ "Me neither"
+ true ->
+ "But I will (this is essentially an else)"
+end
+
+# `try/catch` 用于捕获被抛出的值, 它也支持 `after` 子句,
+# 无论是否值被捕获,after 子句都会被调用
+# `try/catch`
+try do
+ throw(:hello)
+catch
+ message -> "Got #{message}."
+after
+ IO.puts("I'm the after clause.")
+end
+#=> I'm the after clause
+# "Got :hello"
+
+## ---------------------------
+## -- 模块和函数(Modules and Functions)
+## ---------------------------
+
+# 匿名函数 (注意点)
+square = fn(x) -> x * x end
+square.(5) #=> 25
+
+
+# 也支持接收多个子句和卫士(guards).
+# Guards 可以进行模式匹配
+# Guards 使用 `when` 关键字指明:
+f = fn
+ x, y when x > 0 -> x + y
+ x, y -> x * y
+end
+
+f.(1, 3) #=> 4
+f.(-1, 3) #=> -3
+
+# Elixir 提供了很多内建函数
+# 在默认作用域都是可用的
+is_number(10) #=> true
+is_list("hello") #=> false
+elem({1,2,3}, 0) #=> 1
+
+# 你可以在一个模块里定义多个函数,定义函数使用 `def`
+defmodule Math do
+ def sum(a, b) do
+ a + b
+ end
+
+ def square(x) do
+ x * x
+ end
+end
+
+Math.sum(1, 2) #=> 3
+Math.square(3) #=> 9
+
+# 保存到 `math.ex`,使用 `elixirc` 编译你的 Math 模块
+# 在终端里: elixirc math.ex
+
+# 在模块中可以使用`def`定义函数,使用 `defp` 定义私有函数
+# 使用`def` 定义的函数可以被其它模块调用
+# 私有函数只能在本模块内调用
+defmodule PrivateMath do
+ def sum(a, b) do
+ do_sum(a, b)
+ end
+
+ defp do_sum(a, b) do
+ a + b
+ end
+end
+
+PrivateMath.sum(1, 2) #=> 3
+# PrivateMath.do_sum(1, 2) #=> ** (UndefinedFunctionError)
+
+
+# 函数定义同样支持 guards 和 多重子句:
+defmodule Geometry do
+ def area({:rectangle, w, h}) do
+ w * h
+ end
+
+ def area({:circle, r}) when is_number(r) do
+ 3.14 * r * r
+ end
+end
+
+Geometry.area({:rectangle, 2, 3}) #=> 6
+Geometry.area({:circle, 3}) #=> 28.25999999999999801048
+# Geometry.area({:circle, "not_a_number"})
+#=> ** (FunctionClauseError) no function clause matching in Geometry.area/1
+
+#由于不变性,递归是Elixir的重要组成部分
+defmodule Recursion do
+ def sum_list([head | tail], acc) do
+ sum_list(tail, acc + head)
+ end
+
+ def sum_list([], acc) do
+ acc
+ end
+end
+
+Recursion.sum_list([1,2,3], 0) #=> 6
+
+# Elixir 模块支持属性,模块内建了一些属性,你也可以自定义属性
+defmodule MyMod do
+ @moduledoc """
+ 内置的属性,模块文档
+ """
+
+ @my_data 100 # 自定义属性
+ IO.inspect(@my_data) #=> 100
+end
+
+## ---------------------------
+## -- 记录和异常(Records and Exceptions)
+## ---------------------------
+
+# 记录就是把特定值关联到某个名字的结构体
+defrecord Person, name: nil, age: 0, height: 0
+
+joe_info = Person.new(name: "Joe", age: 30, height: 180)
+#=> Person[name: "Joe", age: 30, height: 180]
+
+# 访问name的值
+joe_info.name #=> "Joe"
+
+# 更新age的值
+joe_info = joe_info.age(31) #=> Person[name: "Joe", age: 31, height: 180]
+
+# 使用 `try` `rescue` 进行异常处理
+try do
+ raise "some error"
+rescue
+ RuntimeError -> "rescued a runtime error"
+ _error -> "this will rescue any error"
+end
+
+# 所有的异常都有一个message
+try do
+ raise "some error"
+rescue
+ x in [RuntimeError] ->
+ x.message
+end
+
+## ---------------------------
+## -- 并发(Concurrency)
+## ---------------------------
+
+# Elixir 依赖于 actor并发模型。在Elixir编写并发程序的三要素:
+# 创建进程,发送消息,接收消息
+
+# 启动一个新的进程使用`spawn`函数,接收一个函数作为参数
+
+f = fn -> 2 * 2 end #=> #Function<erl_eval.20.80484245>
+spawn(f) #=> #PID<0.40.0>
+
+
+# `spawn` 函数返回一个pid(进程标识符),你可以使用pid向进程发送消息。
+# 使用 `<-` 操作符发送消息。
+# 我们需要在进程内接收消息,要用到 `receive` 机制。
+
+defmodule Geometry do
+ def area_loop do
+ receive do
+ {:rectangle, w, h} ->
+ IO.puts("Area = #{w * h}")
+ area_loop()
+ {:circle, r} ->
+ IO.puts("Area = #{3.14 * r * r}")
+ area_loop()
+ end
+ end
+end
+
+# 编译这个模块,在shell中创建一个进程,并执行 `area_looop` 函数。
+pid = spawn(fn -> Geometry.area_loop() end) #=> #PID<0.40.0>
+
+# 发送一个消息给 `pid`, 会在receive语句进行模式匹配
+pid <- {:rectangle, 2, 3}
+#=> Area = 6
+# {:rectangle,2,3}
+
+pid <- {:circle, 2}
+#=> Area = 12.56000000000000049738
+# {:circle,2}
+
+# shell也是一个进程(process), 你可以使用`self`获取当前 pid
+self() #=> #PID<0.27.0>
+```
+
+## 参考文献
+
+* [Getting started guide](http://elixir-lang.org/getting_started/1.html) from [elixir webpage](http://elixir-lang.org)
+* [Elixir Documentation](http://elixir-lang.org/docs/master/)
+* ["Learn You Some Erlang for Great Good!"](http://learnyousomeerlang.com/) by Fred Hebert
+* "Programming Erlang: Software for a Concurrent World" by Joe Armstrong
diff --git a/zh-cn/git-cn.html.markdown b/zh-cn/git-cn.html.markdown
index 86952eba..4ef3ffb8 100755..100644
--- a/zh-cn/git-cn.html.markdown
+++ b/zh-cn/git-cn.html.markdown
@@ -219,7 +219,7 @@ $ git diff
# 显示索引和最近一次提交的不同
$ git diff --cached
-# 显示宫缩目录和最近一次提交的不同
+# 显示工作目录和最近一次提交的不同
$ git diff HEAD
```
@@ -349,7 +349,7 @@ $ git reset --hard 31f2bb1
### rm
-和add相反,从工作空间中去掉某个文件爱你
+和add相反,从工作空间中去掉某个文件
```bash
# 移除 HelloWorld.c
diff --git a/zh-cn/go-cn.html.markdown b/zh-cn/go-cn.html.markdown
new file mode 100644
index 00000000..4a87dc21
--- /dev/null
+++ b/zh-cn/go-cn.html.markdown
@@ -0,0 +1,286 @@
+---
+language: Go
+lang: zh-cn
+filename: learngo-cn.go
+contributors:
+ - ["Sonia Keys", "https://github.com/soniakeys"]
+ - ["pantaovay", "https://github.com/pantaovay"]
+ - ["lidashuang", "https://github.com/lidashuang"]
+
+---
+
+发明Go语言是出于更好地完成工作的需要。Go不是计算机科学的最新发展潮流,但它却提供了解决现实问题的最新最快的方法。
+
+Go拥有命令式语言的静态类型,编译很快,执行也很快,同时加入了对于目前多核CPU的并发计算支持,也有相应的特性来实现大规模编程。
+
+Go语言有非常棒的标准库,还有一个充满热情的社区。
+
+```go
+// 单行注释
+/* 多行
+ 注释 */
+
+// 导入包的子句在每个源文件的开头。
+// Main比较特殊,它用来声明可执行文件,而不是一个库。
+package main
+
+// Import语句声明了当前文件引用的包。
+import (
+ "fmt" // Go语言标准库中的包
+ "net/http" // 一个web服务器包
+ "strconv" // 字符串转换
+)
+
+// 函数声明:Main是程序执行的入口。
+// 不管你喜欢还是不喜欢,反正Go就用了花括号来包住函数体。
+func main() {
+ // 往标准输出打印一行。
+ // 用包名fmt限制打印函数。
+ fmt.Println("Hello world!")
+
+ // 调用当前包的另一个函数。
+ beyondHello()
+}
+
+// 函数可以在括号里加参数。
+// 如果没有参数的话,也需要一个空括号。
+func beyondHello() {
+ var x int // 变量声明,变量必须在使用之前声明。
+ x = 3 // 变量赋值。
+ // 可以用:=来偷懒,它自动把变量类型、声明和赋值都搞定了。
+ y := 4
+ sum, prod := learnMultiple(x, y) // 返回多个变量的函数
+ fmt.Println("sum:", sum, "prod:", prod) // 简单输出
+ learnTypes() // 少于y分钟,学的更多!
+}
+
+// 多变量和多返回值的函数
+func learnMultiple(x, y int) (sum, prod int) {
+ return x + y, x * y // 返回两个值
+}
+
+// 内置变量类型和关键词
+func learnTypes() {
+ // 短声明给你所想。
+ s := "Learn Go!" // String类型
+
+ s2 := `A "raw" string literal
+can include line breaks.` // 同样是String类型
+
+ // 非ascii字符。Go使用UTF-8编码。
+ g := 'Σ' // rune类型,uint32的别名,使用UTF-8编码
+
+ f := 3.14195 // float64类型,IEEE-754 64位浮点数
+ c := 3 + 4i // complex128类型,内部使用两个float64表示
+
+ // Var变量可以直接初始化。
+ var u uint = 7 // unsigned 无符号变量,但是实现依赖int型变量的长度
+ var pi float32 = 22. / 7
+
+ // 字符转换
+ n := byte('\n') // byte是uint8的别名
+
+ // 数组类型编译的时候大小固定。
+ var a4 [4] int // 有4个int变量的数组,初始为0
+ a3 := [...]int{3, 1, 5} // 有3个int变量的数组,同时进行了初始化
+
+ // Slice 可以动态的增删。Array和Slice各有千秋,但是使用slice的地方更多些。
+ s3 := []int{4, 5, 9} // 和a3相比,这里没有省略号
+ s4 := make([]int, 4) // 分配一个有4个int型变量的slice,全部被初始化为0
+
+ var d2 [][]float64 // 声明而已,什么都没有分配
+ bs := []byte("a slice") // 类型转换的语法
+
+ p, q := learnMemory() // 声明p,q为int型变量的指针
+ fmt.Println(*p, *q) // * 取值
+
+ // Map是动态可增长关联数组,和其他语言中的hash或者字典相似。
+ m := map[string]int{"three": 3, "four": 4}
+ m["one"] = 1
+
+ // 在Go语言中未使用的变量在编译的时候会报错,而不是warning。
+ // 下划线 _ 可以使你“使用”一个变量,但是丢弃它的值。
+ _,_,_,_,_,_,_,_,_ = s2, g, f, u, pi, n, a3, s4, bs
+ // 输出变量
+ fmt.Println(s, c, a4, s3, d2, m)
+
+ learnFlowControl() // 回到流程控制
+}
+
+// Go全面支持垃圾回收。Go有指针,但是不支持指针运算。
+// 你会因为空指针而犯错,但是不会因为增加指针而犯错。
+func learnMemory() (p, q *int) {
+ // 返回int型变量指针p和q
+ p = new(int) // 内置函数new分配内存
+ // 自动将分配的int赋值0,p不再是空的了。
+ s := make([]int, 20) // 给20个int变量分配一块内存
+ s[3] = 7 // 赋值
+ r := -2 // 声明另一个局部变量
+ return &s[3], &r // & 取地址
+}
+
+func expensiveComputation() int {
+ return 1e6
+}
+
+func learnFlowControl() {
+ // If需要花括号,括号就免了
+ if true {
+ fmt.Println("told ya")
+ }
+ // 用go fmt 命令可以帮你格式化代码,所以不用怕被人吐槽代码风格了,
+ // 也不用容忍被人的代码风格。
+ if false {
+ // pout
+ } else {
+ // gloat
+ }
+ // 如果太多嵌套的if语句,推荐使用switch
+ x := 1
+ switch x {
+ case 0:
+ case 1:
+ // 隐式调用break语句,匹配上一个即停止
+ case 2:
+ // 不会运行
+ }
+ // 和if一样,for也不用括号
+ for x := 0; x < 3; x++ { // ++ 自增
+ fmt.Println("iteration", x)
+ }
+ // x在这里还是1。为什么?
+
+ // for 是go里唯一的循环关键字,不过它有很多变种
+ for { // 死循环
+ break // 骗你的
+ continue // 不会运行的
+ }
+ // 和for一样,if中的:=先给y赋值,然后再和x作比较。
+ if y := expensiveComputation(); y > x {
+ x = y
+ }
+ // 闭包函数
+ xBig := func() bool {
+ return x > 100 // x是上面声明的变量引用
+ }
+ fmt.Println("xBig:", xBig()) // true (上面把y赋给x了)
+ x /= 1e5 // x变成10
+ fmt.Println("xBig:", xBig()) // 现在是false
+
+ // 当你需要goto的时候,你会爱死它的!
+ goto love
+love:
+
+ learnInterfaces() // 好东西来了!
+}
+
+// 定义Stringer为一个接口类型,有一个方法String
+type Stringer interface {
+ String() string
+}
+
+// 定义pair为一个结构体,有x和y两个int型变量。
+type pair struct {
+ x, y int
+}
+
+// 定义pair类型的方法,实现Stringer接口。
+func (p pair) String() string { // p被叫做“接收器”
+ // Sprintf是fmt包中的另一个公有函数。
+ // 用 . 调用p中的元素。
+ return fmt.Sprintf("(%d, %d)", p.x, p.y)
+}
+
+func learnInterfaces() {
+ // 花括号用来定义结构体变量,:=在这里将一个结构体变量赋值给p。
+ p := pair{3, 4}
+ fmt.Println(p.String()) // 调用pair类型p的String方法
+ var i Stringer // 声明i为Stringer接口类型
+ i = p // 有效!因为p实现了Stringer接口(类似java中的塑型)
+ // 调用i的String方法,输出和上面一样
+ fmt.Println(i.String())
+
+ // fmt包中的Println函数向对象要它们的string输出,实现了String方法就可以这样使用了。
+ // (类似java中的序列化)
+ fmt.Println(p) // 输出和上面一样,自动调用String函数。
+ fmt.Println(i) // 输出和上面一样。
+
+ learnErrorHandling()
+}
+
+func learnErrorHandling() {
+ // ", ok"用来判断有没有正常工作
+ m := map[int]string{3: "three", 4: "four"}
+ if x, ok := m[1]; !ok { // ok 为false,因为m中没有1
+ fmt.Println("no one there")
+ } else {
+ fmt.Print(x) // 如果x在map中的话,x就是那个值喽。
+ }
+ // 错误可不只是ok,它还可以给出关于问题的更多细节。
+ if _, err := strconv.Atoi("non-int"); err != nil { // _ discards value
+ // 输出"strconv.ParseInt: parsing "non-int": invalid syntax"
+ fmt.Println(err)
+ }
+ // 待会再说接口吧。同时,
+ learnConcurrency()
+}
+
+// c是channel类型,一个并发安全的通信对象。
+func inc(i int, c chan int) {
+ c <- i + 1 // <-把右边的发送到左边的channel。
+}
+
+// 我们将用inc函数来并发地增加一些数字。
+func learnConcurrency() {
+ // 用make来声明一个slice,make会分配和初始化slice,map和channel。
+ c := make(chan int)
+ // 用go关键字开始三个并发的goroutine,如果机器支持的话,还可能是并行执行。
+ // 三个都被发送到同一个channel。
+ go inc(0, c) // go is a statement that starts a new goroutine.
+ go inc(10, c)
+ go inc(-805, c)
+ // 从channel中独处结果并打印。
+ // 打印出什么东西是不可预知的。
+ fmt.Println(<-c, <-c, <-c) // channel在右边的时候,<-是读操作。
+
+ cs := make(chan string) // 操作string的channel
+ cc := make(chan chan string) // 操作channel的channel
+ go func() { c <- 84 }() // 开始一个goroutine来发送一个新的数字
+ go func() { cs <- "wordy" }() // 发送给cs
+ // Select类似于switch,但是每个case包括一个channel操作。
+ // 它随机选择一个准备好通讯的case。
+ select {
+ case i := <-c: // 从channel接收的值可以赋给其他变量
+ fmt.Println("it's a", i)
+ case <-cs: // 或者直接丢弃
+ fmt.Println("it's a string")
+ case <-cc: // 空的,还没作好通讯的准备
+ fmt.Println("didn't happen.")
+ }
+ // 上面c或者cs的值被取到,其中一个goroutine结束,另外一个一直阻塞。
+
+ learnWebProgramming() // Go很适合web编程,我知道你也想学!
+}
+
+// http包中的一个简单的函数就可以开启web服务器。
+func learnWebProgramming() {
+ // ListenAndServe第一个参数指定了监听端口,第二个参数是一个接口,特定是http.Handler。
+ err := http.ListenAndServe(":8080", pair{})
+ fmt.Println(err) // 不要无视错误。
+}
+
+// 使pair实现http.Handler接口的ServeHTTP方法。
+func (p pair) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ // 使用http.ResponseWriter返回数据
+ w.Write([]byte("You learned Go in Y minutes!"))
+}
+```
+
+## 更进一步
+
+Go的根源在[Go官方网站](http://golang.org/)。
+在那里你可以学习入门教程,通过浏览器交互式地学习,而且可以读到很多东西。
+
+强烈推荐阅读语言定义部分,很简单而且很简洁!(as language definitions go these days.)
+
+学习Go还要阅读Go标准库的源代码,全部文档化了,可读性非常好,可以学到go,go style和go idioms。在文档中点击函数名,源代码就出来了!
diff --git a/zh-cn/haskell-cn.html.markdown b/zh-cn/haskell-cn.html.markdown
index 8d51f144..8d51f144 100755..100644
--- a/zh-cn/haskell-cn.html.markdown
+++ b/zh-cn/haskell-cn.html.markdown
diff --git a/zh-cn/java-cn.html.markdown b/zh-cn/java-cn.html.markdown
index 9422ac2f..f7d319e6 100755..100644
--- a/zh-cn/java-cn.html.markdown
+++ b/zh-cn/java-cn.html.markdown
@@ -322,7 +322,7 @@ class Bicycle {
// 方法声明的语法:
// <作用域> <返回值类型> <方法名>(<参数列表>)
public int getCadence() {
- retur450635425n cadence;
+ return cadence;
}
// void返450635425回值函数没有返回值
diff --git a/zh-cn/javascript-cn.html.markdown b/zh-cn/javascript-cn.html.markdown
index 89fc256e..86ad1d07 100755..100644
--- a/zh-cn/javascript-cn.html.markdown
+++ b/zh-cn/javascript-cn.html.markdown
@@ -401,8 +401,6 @@ if (Object.create === undefined){ // 如果存在则不覆盖
[Mozilla 开发者
网络](https://developer.mozilla.org/en-US/docs/Web/JavaScript) 提供了很好的
Javascript文档,并且由于是wiki,所以你也可以自行编辑来分享你的知识。
-wiki, so as you learn more you can help others out by sharing your own
-knowledge.
MDN的 [A re-introduction to
JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript)
diff --git a/zh-cn/lua-cn.html.markdown b/zh-cn/lua-cn.html.markdown
new file mode 100644
index 00000000..95a94c76
--- /dev/null
+++ b/zh-cn/lua-cn.html.markdown
@@ -0,0 +1,413 @@
+---
+language: lua
+lang: zh-cn
+contributors:
+ - ["Tyler Neylon", "http://tylerneylon.com/"]
+ - ["Rob Hoelz", "http://hoelz.ro"]
+ - ["Jakukyo Friel", "http://weakish.github.io"]
+ - ["Craig Roddin", "craig.roddin@gmail.com"]
+ - ["Amr Tamimi", "https://amrtamimi.com"]
+translators:
+ - ["Jakukyo Friel", "http://weakish.github.io"]
+---
+
+```lua
+-- 单行注释以两个连字符开头
+
+--[[
+ 多行注释
+--]]
+
+----------------------------------------------------
+-- 1. 变量和流程控制
+----------------------------------------------------
+
+num = 42 -- 所有的数字都是双精度浮点型。
+-- 别害怕,64位的双精度浮点型数字中有52位用于
+-- 保存精确的整型值; 对于52位以内的整型值,
+-- 不用担心精度问题。
+
+s = 'walternate' -- 和Python一样,字符串不可变。
+t = "也可以用双引号"
+u = [[ 多行的字符串
+ 以两个方括号
+ 开始和结尾。]]
+t = nil -- 撤销t的定义; Lua 支持垃圾回收。
+
+-- 块使用do/end之类的关键字标识:
+while num < 50 do
+ num = num + 1 -- 不支持 ++ 或 += 运算符。
+end
+
+-- If语句:
+if num > 40 then
+ print('over 40')
+elseif s ~= 'walternate' then -- ~= 表示不等于。
+ -- 像Python一样,用 == 检查是否相等 ;字符串同样适用。
+ io.write('not over 40\n') -- 默认标准输出。
+else
+ -- 默认全局变量。
+ thisIsGlobal = 5 -- 通常使用驼峰。
+
+ -- 如何定义局部变量:
+ local line = io.read() -- 读取标准输入的下一行。
+
+ -- ..操作符用于连接字符串:
+ print('Winter is coming, ' .. line)
+end
+
+-- 未定义的变量返回nil。
+-- 这不是错误:
+foo = anUnknownVariable -- 现在 foo = nil.
+
+aBoolValue = false
+
+--只有nil和false为假; 0和 ''都均为真!
+if not aBoolValue then print('twas false') end
+
+-- 'or'和 'and'短路
+-- 类似于C/js里的 a?b:c 操作符:
+ans = aBoolValue and 'yes' or 'no' --> 'no'
+
+karlSum = 0
+for i = 1, 100 do -- 范围包含两端
+ karlSum = karlSum + i
+end
+
+-- 使用 "100, 1, -1" 表示递减的范围:
+fredSum = 0
+for j = 100, 1, -1 do fredSum = fredSum + j end
+
+-- 通常,范围表达式为begin, end[, step].
+
+-- 循环的另一种结构:
+repeat
+ print('the way of the future')
+ num = num - 1
+until num == 0
+
+----------------------------------------------------
+-- 2. 函数。
+----------------------------------------------------
+
+function fib(n)
+ if n < 2 then return 1 end
+ return fib(n - 2) + fib(n - 1)
+end
+
+-- 支持闭包及匿名函数:
+function adder(x)
+ -- 调用adder时,会创建返回的函数,
+ -- 并且会记住x的值:
+ return function (y) return x + y end
+end
+a1 = adder(9)
+a2 = adder(36)
+print(a1(16)) --> 25
+print(a2(64)) --> 100
+
+-- 返回值、函数调用和赋值都可以
+-- 使用长度不匹配的list。
+-- 不匹配的接收方会被赋值nil;
+-- 不匹配的发送方会被丢弃。
+
+x, y, z = 1, 2, 3, 4
+-- x = 1、y = 2、z = 3, 而 4 会被丢弃。
+
+function bar(a, b, c)
+ print(a, b, c)
+ return 4, 8, 15, 16, 23, 42
+end
+
+x, y = bar('zaphod') --> 打印 "zaphod nil nil"
+-- 现在 x = 4, y = 8, 而值15..42被丢弃。
+
+-- 函数是一等公民,可以是局部的,也可以是全局的。
+-- 以下表达式等价:
+function f(x) return x * x end
+f = function (x) return x * x end
+
+-- 这些也是等价的:
+local function g(x) return math.sin(x) end
+local g; g = function (x) return math.sin(x) end
+-- 'local g'使得g可以自引用。
+
+-- 顺便提下,三角函数以弧度为单位。
+
+-- 用一个字符串参数调用函数,可以省略括号:
+print 'hello' --可以工作。
+
+-- 调用函数时,如果只有一个table参数,
+-- 同样可以省略括号(table详情见下):
+print {} -- 一样可以工作。
+
+----------------------------------------------------
+-- 3. Table。
+----------------------------------------------------
+
+-- Table = Lua唯一的组合数据结构;
+-- 它们是关联数组。
+-- 类似于PHP的数组或者js的对象,
+-- 它们是哈希表或者字典,也可以当初列表使用。
+
+-- 按字典/map的方式使用Table:
+
+-- Dict字面量默认使用字符串类型的key:
+t = {key1 = 'value1', key2 = false}
+
+-- 字符串key可以使用类似js的点标记:
+print(t.key1) -- 打印 'value1'.
+t.newKey = {} -- 添加新的键值对。
+t.key2 = nil -- 从table删除 key2。
+
+-- 使用任何非nil的值作为key:
+u = {['@!#'] = 'qbert', [{}] = 1729, [6.28] = 'tau'}
+print(u[6.28]) -- 打印 "tau"
+
+-- 数字和字符串的key按值匹配的
+-- table按id匹配。
+a = u['@!#'] -- 现在 a = 'qbert'.
+b = u[{}] -- 我们或许期待的是 1729, 但是得到的是nil:
+-- b = nil ,因为没有找到。
+-- 之所以没找到,是因为我们用的key与保存数据时用的不是同
+-- 一个对象。
+-- 所以字符串和数字是移植性更好的key。
+
+-- 只需要一个table参数的函数调用不需要括号:
+function h(x) print(x.key1) end
+h{key1 = 'Sonmi~451'} -- 打印'Sonmi~451'.
+
+for key, val in pairs(u) do -- 遍历Table
+ print(key, val)
+end
+
+-- _G 是一个特殊的table,用于保存所有的全局变量
+print(_G['_G'] == _G) -- 打印'true'.
+
+-- 按列表/数组的方式使用:
+
+-- 列表字面量隐式添加整数键:
+v = {'value1', 'value2', 1.21, 'gigawatts'}
+for i = 1, #v do -- #v 是列表的大小
+ print(v[i]) -- 索引从 1 开始!! 太疯狂了!
+end
+-- 'list'并非真正的类型,v 其实是一个table,
+-- 只不过它用连续的整数作为key,可以像list那样去使用。
+
+----------------------------------------------------
+-- 3.1 元表(metatable) 和元方法(metamethod)。
+----------------------------------------------------
+
+-- table的元表提供了一种机制,支持类似操作符重载的行为。
+-- 稍后我们会看到元表如何支持类似js prototype的行为。
+
+f1 = {a = 1, b = 2} -- 表示一个分数 a/b.
+f2 = {a = 2, b = 3}
+
+-- 这会失败:
+-- s = f1 + f2
+
+metafraction = {}
+function metafraction.__add(f1, f2)
+ sum = {}
+ sum.b = f1.b * f2.b
+ sum.a = f1.a * f2.b + f2.a * f1.b
+ return sum
+end
+
+setmetatable(f1, metafraction)
+setmetatable(f2, metafraction)
+
+s = f1 + f2 -- 调用在f1的元表上的__add(f1, f2) 方法
+
+-- f1, f2 没有关于元表的key,这点和js的prototype不一样。
+-- 因此你必须用getmetatable(f1)获取元表。
+-- 元表是一个普通的table,
+-- 元表的key是普通的Lua中的key,例如__add。
+
+-- 但是下面一行代码会失败,因为s没有元表:
+-- t = s + s
+-- 下面提供的与类相似的模式可以解决这个问题:
+
+-- 元表的__index 可以重载用于查找的点操作符:
+defaultFavs = {animal = 'gru', food = 'donuts'}
+myFavs = {food = 'pizza'}
+setmetatable(myFavs, {__index = defaultFavs})
+eatenBy = myFavs.animal -- 可以工作!感谢元表
+
+-- 如果在table中直接查找key失败,会使用
+-- 元表的__index 递归地重试。
+
+-- __index的值也可以是function(tbl, key)
+-- 这样可以支持自定义查找。
+
+-- __index、__add等的值,被称为元方法。
+-- 这里是一个table元方法的清单:
+
+-- __add(a, b) for a + b
+-- __sub(a, b) for a - b
+-- __mul(a, b) for a * b
+-- __div(a, b) for a / b
+-- __mod(a, b) for a % b
+-- __pow(a, b) for a ^ b
+-- __unm(a) for -a
+-- __concat(a, b) for a .. b
+-- __len(a) for #a
+-- __eq(a, b) for a == b
+-- __lt(a, b) for a < b
+-- __le(a, b) for a <= b
+-- __index(a, b) <fn or a table> for a.b
+-- __newindex(a, b, c) for a.b = c
+-- __call(a, ...) for a(...)
+
+----------------------------------------------------
+-- 3.2 与类相似的table和继承。
+----------------------------------------------------
+
+-- Lua没有内建的类;可以通过不同的方法,利用表和元表
+-- 来实现类。
+
+-- 下面是一个例子,解释在后面:
+
+Dog = {} -- 1.
+
+function Dog:new() -- 2.
+ newObj = {sound = 'woof'} -- 3.
+ self.__index = self -- 4.
+ return setmetatable(newObj, self) -- 5.
+end
+
+function Dog:makeSound() -- 6.
+ print('I say ' .. self.sound)
+end
+
+mrDog = Dog:new() -- 7.
+mrDog:makeSound() -- 'I say woof' -- 8.
+
+-- 1. Dog看上去像一个类;其实它是一个table。
+-- 2. 函数tablename:fn(...) 等价于
+-- 函数tablename.fn(self, ...)
+-- 冒号(:)只是添加了self作为第一个参数。
+-- 阅读7 & 8条 了解self变量是如何得到其值的。
+-- 3. newObj是类Dog的一个实例。
+-- 4. self = 被继承的类。通常self = Dog,不过继承可以改变它。
+-- 如果把newObj的元表和__index都设置为self,
+-- newObj就可以得到self的函数。
+-- 5. 备忘:setmetatable返回其第一个参数。
+-- 6. 冒号(:)的作用和第2条一样,不过这里
+-- self是一个实例,而不是类
+-- 7. 等价于Dog.new(Dog),所以在new()中,self = Dog。
+-- 8. 等价于mrDog.makeSound(mrDog); self = mrDog。
+
+----------------------------------------------------
+
+-- 继承的例子:
+
+LoudDog = Dog:new() -- 1.
+
+function LoudDog:makeSound()
+ s = self.sound .. ' ' -- 2.
+ print(s .. s .. s)
+end
+
+seymour = LoudDog:new() -- 3.
+seymour:makeSound() -- 'woof woof woof' -- 4.
+
+-- 1. LoudDog获得Dog的方法和变量列表。
+-- 2. 因为new()的缘故,self拥有了一个'sound' key,参见第3条。
+-- 3. 等价于LoudDog.new(LoudDog),转换一下就是
+-- Dog.new(LoudDog),这是因为LoudDog没有'new' key,
+-- 但是它的元表中有 __index = Dog。
+-- 结果: seymour的元表是LoudDog,并且
+-- LoudDog.__index = Dog。所以有seymour.key
+-- = seymour.key, LoudDog.key, Dog.key
+-- 从其中第一个有指定key的table获取。
+-- 4. 在LoudDog可以找到'makeSound'的key;
+-- 等价于LoudDog.makeSound(seymour)。
+
+-- 如果有必要,子类也可以有new(),与基类相似:
+function LoudDog:new()
+ newObj = {}
+ -- 初始化newObj
+ self.__index = self
+ return setmetatable(newObj, self)
+end
+
+----------------------------------------------------
+-- 4. 模块
+----------------------------------------------------
+
+
+--[[ 我把这部分给注释了,这样脚本剩下的部分可以运行
+
+-- 假设文件mod.lua的内容类似这样:
+local M = {}
+
+local function sayMyName()
+ print('Hrunkner')
+end
+
+function M.sayHello()
+ print('Why hello there')
+ sayMyName()
+end
+
+return M
+
+-- 另一个文件可以使用mod.lua的功能:
+local mod = require('mod') -- 运行文件mod.lua.
+
+-- require是包含模块的标准做法。
+-- require等价于: (针对没有被缓存的情况;参见后面的内容)
+local mod = (function ()
+ <contents of mod.lua>
+end)()
+-- mod.lua被包在一个函数体中,因此mod.lua的局部变量
+-- 对外不可见。
+
+-- 下面的代码可以工作,因为在这里mod = mod.lua 中的 M:
+mod.sayHello() -- Says hello to Hrunkner.
+
+-- 这是错误的;sayMyName只在mod.lua中存在:
+mod.sayMyName() -- 错误
+
+-- require返回的值会被缓存,所以一个文件只会被运行一次,
+-- 即使它被require了多次。
+
+-- 假设mod2.lua包含代码"print('Hi!')"。
+local a = require('mod2') -- 打印Hi!
+local b = require('mod2') -- 不再打印; a=b.
+
+-- dofile与require类似,但是不缓存:
+dofile('mod2') --> Hi!
+dofile('mod2') --> Hi! (再次运行,与require不同)
+
+-- loadfile加载一个lua文件,但是并不运行它。
+f = loadfile('mod2') -- Calling f() runs mod2.lua.
+
+-- loadstring是loadfile的字符串版本。
+g = loadstring('print(343)') --返回一个函数。
+g() -- 打印343; 在此之前什么也不打印。
+
+--]]
+```
+
+## 参考
+
+
+
+为什么?我非常兴奋地学习lua, 这样我就可以使用[Löve 2D游戏引擎](http://love2d.org/)来编游戏。
+
+怎么做?我从[BlackBulletIV的面向程序员的Lua指南](http://nova-fusion.com/2012/08/27/lua-for-programmers-part-1/)入门。接着我阅读了官方的[Lua编程](http://www.lua.org/pil/contents.html)一书。
+
+lua-users.org上的[Lua简明参考](http://lua-users.org/files/wiki_insecure/users/thomasl/luarefv51.pdf)应该值得一看。
+
+本文没有涉及标准库的内容:
+
+* <a href="http://lua-users.org/wiki/StringLibraryTutorial">string library</a>
+* <a href="http://lua-users.org/wiki/TableLibraryTutorial">table library</a>
+* <a href="http://lua-users.org/wiki/MathLibraryTutorial">math library</a>
+* <a href="http://lua-users.org/wiki/IoLibraryTutorial">io library</a>
+* <a href="http://lua-users.org/wiki/OsLibraryTutorial">os library</a>
+
+使用Lua,欢乐常在!
diff --git a/zh-cn/perl-cn.html.markdown b/zh-cn/perl-cn.html.markdown
new file mode 100644
index 00000000..5b0d6179
--- /dev/null
+++ b/zh-cn/perl-cn.html.markdown
@@ -0,0 +1,152 @@
+---
+name: perl
+category: language
+language: perl
+filename: learnperl-cn.pl
+contributors:
+ - ["Korjavin Ivan", "http://github.com/korjavin"]
+translators:
+ - ["Yadong Wen", "https://github.com/yadongwen"]
+lang: zh-cn
+---
+
+Perl 5是一个功能强大、特性齐全的编程语言,有25年的历史。
+
+Perl 5可以在包括便携式设备和大型机的超过100个平台上运行,既适用于快速原型构建,也适用于大型项目开发。
+
+```perl
+# 单行注释以#号开头
+
+
+#### Perl的变量类型
+
+# 变量以$号开头。
+# 合法变量名以英文字母或者下划线起始,
+# 后接任意数目的字母、数字或下划线。
+
+### Perl有三种主要的变量类型:标量、数组和哈希。
+
+## 标量
+# 标量类型代表单个值:
+my $animal = "camel";
+my $answer = 42;
+
+# 标量类型值可以是字符串、整型或浮点类型,Perl会根据需要自动进行类型转换。
+
+## 数组
+# 数组类型代表一列值:
+my @animals = ("camel", "llama", "owl");
+my @numbers = (23, 42, 69);
+my @mixed = ("camel", 42, 1.23);
+
+
+
+## 哈希
+# 哈希类型代表一个键/值对的集合:
+
+my %fruit_color = ("apple", "red", "banana", "yellow");
+
+# 可以使用空格和“=>”操作符更清晰的定义哈希:
+
+my %fruit_color = (
+ apple => "red",
+ banana => "yellow",
+ );
+# perldata中有标量、数组和哈希更详细的介绍。 (perldoc perldata).
+
+# 可以用引用构建更复杂的数据类型,比如嵌套的列表和哈希。
+
+#### 逻辑和循环结构
+
+# Perl有大多数常见的逻辑和循环控制结构
+
+if ( $var ) {
+ ...
+} elsif ( $var eq 'bar' ) {
+ ...
+} else {
+ ...
+}
+
+unless ( condition ) {
+ ...
+ }
+# 上面这个比"if (!condition)"更可读。
+
+# 有Perl特色的后置逻辑结构
+print "Yow!" if $zippy;
+print "We have no bananas" unless $bananas;
+
+# while
+ while ( condition ) {
+ ...
+ }
+
+
+# for和foreach
+for ($i = 0; $i <= $max; $i++) {
+ ...
+ }
+
+foreach (@array) {
+ print "This element is $_\n";
+ }
+
+
+#### 正则表达式
+
+# Perl对正则表达式有深入广泛的支持,perlrequick和perlretut等文档有详细介绍。简单来说:
+
+# 简单匹配
+if (/foo/) { ... } # 如果 $_ 包含"foo"逻辑为真
+if ($a =~ /foo/) { ... } # 如果 $a 包含"foo"逻辑为真
+
+# 简单替换
+
+$a =~ s/foo/bar/; # 将$a中的foo替换为bar
+$a =~ s/foo/bar/g; # 将$a中所有的foo替换为bar
+
+
+#### 文件和输入输出
+
+# 可以使用“open()”函数打开文件用于输入输出。
+
+open(my $in, "<", "input.txt") or die "Can't open input.txt: $!";
+open(my $out, ">", "output.txt") or die "Can't open output.txt: $!";
+open(my $log, ">>", "my.log") or die "Can't open my.log: $!";
+
+# 可以用"<>"操作符读取一个打开的文件句柄。 在标量语境下会读取一行,
+# 在列表环境下会将整个文件读入并将每一行赋给列表的一个元素:
+
+my $line = <$in>;
+my @lines = <$in>;
+
+#### 子程序
+
+# 写子程序很简单:
+
+sub logger {
+ my $logmessage = shift;
+ open my $logfile, ">>", "my.log" or die "Could not open my.log: $!";
+ print $logfile $logmessage;
+}
+
+# 现在可以像内置函数一样调用子程序:
+
+logger("We have a logger subroutine!");
+
+
+```
+
+#### 使用Perl模块
+
+Perl模块提供一系列特性来帮助你避免重新发明轮子,CPAN是下载模块的好地方( http://www.cpan.org/ )。Perl发行版本身也包含很多流行的模块。
+
+perlfaq有很多常见问题和相应回答,也经常有对优秀CPAN模块的推荐介绍。
+
+#### 深入阅读
+
+ - [perl-tutorial](http://perl-tutorial.org/)
+ - [www.perl.com的learn站点](http://www.perl.org/learn.html)
+ - [perldoc](http://perldoc.perl.org/)
+ - 以及 perl 内置的: `perldoc perlintro`
diff --git a/zh-cn/php-cn.html.markdown b/zh-cn/php-cn.html.markdown
index c6ebb515..24939681 100755..100644
--- a/zh-cn/php-cn.html.markdown
+++ b/zh-cn/php-cn.html.markdown
@@ -180,7 +180,7 @@ assert($c >= $d);
// 下面的比较只有在类型相同、值相同的情况下才为真
assert($c === $d);
assert($a !== $d);
-assert(1 == '1');
+assert(1 === '1');
assert(1 !== '1');
// 变量可以根据其使用来进行类型转换
@@ -243,7 +243,7 @@ if ($x === '0') {
-// 下面的语法常用语模板中:
+// 下面的语法常用于模板中:
?>
<?php if ($x): ?>
@@ -333,7 +333,7 @@ function my_function () {
echo my_function(); // => "Hello"
// 函数名需要以字母或者下划线开头,
-// 后面可以跟着任意的字幕、下划线、数字.
+// 后面可以跟着任意的字母、下划线、数字.
function add ($x, $y = 1) { // $y 是可选参数,默认值为 1
$result = $x + $y;
diff --git a/zh-cn/python-cn.html.markdown b/zh-cn/python-cn.html.markdown
index 764eed54..deb94cdc 100755..100644
--- a/zh-cn/python-cn.html.markdown
+++ b/zh-cn/python-cn.html.markdown
@@ -17,6 +17,7 @@ Python 由 Guido Van Rossum 在90年代初创建。 它现在是最流行的语
如果是Python 3,请在网络上寻找其他教程
```python
+
# 单行注释
""" 多行字符串可以用
三个引号包裹,不过这也可以被当做
@@ -28,84 +29,84 @@ Python 由 Guido Van Rossum 在90年代初创建。 它现在是最流行的语
####################################################
# 数字类型
-3 #=> 3
+3 # => 3
# 简单的算数
-1 + 1 #=> 2
-8 - 1 #=> 7
-10 * 2 #=> 20
-35 / 5 #=> 7
+1 + 1 # => 2
+8 - 1 # => 7
+10 * 2 # => 20
+35 / 5 # => 7
# 整数的除法会自动取整
-5 / 2 #=> 2
+5 / 2 # => 2
# 要做精确的除法,我们需要引入浮点数
2.0 # 浮点数
-11.0 / 4.0 #=> 2.75 好多了
+11.0 / 4.0 # => 2.75 精确多了
# 括号具有最高优先级
-(1 + 3) * 2 #=> 8
+(1 + 3) * 2 # => 8
-# 布尔值也是原始数据类型
+# 布尔值也是基本的数据类型
True
False
-# 用not来取非
-not True #=> False
-not False #=> True
+# 用 not 来取非
+not True # => False
+not False # => True
# 相等
-1 == 1 #=> True
-2 == 1 #=> False
+1 == 1 # => True
+2 == 1 # => False
# 不等
-1 != 1 #=> False
-2 != 1 #=> True
+1 != 1 # => False
+2 != 1 # => True
# 更多的比较操作符
-1 < 10 #=> True
-1 > 10 #=> False
-2 <= 2 #=> True
-2 >= 2 #=> True
+1 < 10 # => True
+1 > 10 # => False
+2 <= 2 # => True
+2 >= 2 # => True
# 比较运算可以连起来写!
-1 < 2 < 3 #=> True
-2 < 3 < 2 #=> False
+1 < 2 < 3 # => True
+2 < 3 < 2 # => False
-# 字符串通过"或'括起来
+# 字符串通过 " 或 ' 括起来
"This is a string."
'This is also a string.'
# 字符串通过加号拼接
-"Hello " + "world!" #=> "Hello world!"
+"Hello " + "world!" # => "Hello world!"
# 字符串可以被视为字符的列表
-"This is a string"[0] #=> 'T'
+"This is a string"[0] # => 'T'
# % 可以用来格式化字符串
"%s can be %s" % ("strings", "interpolated")
-# 也可以用format方法来格式化字符串
+# 也可以用 format 方法来格式化字符串
# 推荐使用这个方法
"{0} can be {1}".format("strings", "formatted")
# 也可以用变量名代替数字
"{name} wants to eat {food}".format(name="Bob", food="lasagna")
# None 是对象
-None #=> None
+None # => None
# 不要用相等 `==` 符号来和None进行比较
-# 要用 `is`
-"etc" is None #=> False
-None is None #=> True
+# 要用 `is`
+"etc" is None # => False
+None is None # => True
# 'is' 可以用来比较对象的相等性
# 这个操作符在比较原始数据时没多少用,但是比较对象时必不可少
-# None, 0, 和空字符串都被算作False
-# 其他的均为True
-0 == False #=> True
-"" == False #=> True
+# None, 0, 和空字符串都被算作 False
+# 其他的均为 True
+0 == False # => True
+"" == False # => True
####################################################
@@ -116,16 +117,16 @@ None is None #=> True
print "I'm Python. Nice to meet you!"
-# 给变量赋值前不需要事先生命
-some_var = 5 # 规范用小写字母和下划线来做为变量名
-some_var #=> 5
+# 给变量赋值前不需要事先声明
+some_var = 5 # 一般建议使用小写字母和下划线组合来做为变量名
+some_var # => 5
-# 访问之前为赋值的变量会抛出异常
-# 查看控制流程一节来了解异常处理
-some_other_var # 抛出命名异常
+# 访问未赋值的变量会抛出异常
+# 可以查看控制流程一节来了解如何异常处理
+some_other_var # 抛出 NameError
-# if语句可以作为表达式来使用
-"yahoo!" if 3 > 2 else 2 #=> "yahoo!"
+# if 语句可以作为表达式来使用
+"yahoo!" if 3 > 2 else 2 # => "yahoo!"
# 列表用来保存序列
li = []
@@ -133,64 +134,64 @@ li = []
other_li = [4, 5, 6]
# 在列表末尾添加元素
-li.append(1) #li 现在是 [1]
-li.append(2) #li 现在是 [1, 2]
-li.append(4) #li 现在是 [1, 2, 4]
-li.append(3) #li 现在是 [1, 2, 4, 3]
+li.append(1) # li 现在是 [1]
+li.append(2) # li 现在是 [1, 2]
+li.append(4) # li 现在是 [1, 2, 4]
+li.append(3) # li 现在是 [1, 2, 4, 3]
# 移除列表末尾元素
-li.pop() #=> 3 and li is now [1, 2, 4]
-# 放回来
+li.pop() # => 3 li 现在是 [1, 2, 4]
+# 重新加进去
li.append(3) # li is now [1, 2, 4, 3] again.
# 像其他语言访问数组一样访问列表
-li[0] #=> 1
+li[0] # => 1
# 访问最后一个元素
-li[-1] #=> 3
+li[-1] # => 3
# 越界会抛出异常
-li[4] # 抛出越界异常
+li[4] # 抛出越界异常
# 切片语法需要用到列表的索引访问
# 可以看做数学之中左闭右开区间
-li[1:3] #=> [2, 4]
+li[1:3] # => [2, 4]
# 省略开头的元素
-li[2:] #=> [4, 3]
+li[2:] # => [4, 3]
# 省略末尾的元素
-li[:3] #=> [1, 2, 4]
+li[:3] # => [1, 2, 4]
# 删除特定元素
-del li[2] # li 现在是 [1, 2, 3]
+del li[2] # li 现在是 [1, 2, 3]
# 合并列表
-li + other_li #=> [1, 2, 3, 4, 5, 6] - 不改变这两个列表
+li + other_li # => [1, 2, 3, 4, 5, 6] - 并不会不改变这两个列表
-# 通过拼接合并列表
-li.extend(other_li) # li 是 [1, 2, 3, 4, 5, 6]
+# 通过拼接来合并列表
+li.extend(other_li) # li 是 [1, 2, 3, 4, 5, 6]
-# 用in来返回元素是否在列表中
-1 in li #=> True
+# 用 in 来返回元素是否在列表中
+1 in li # => True
# 返回列表长度
-len(li) #=> 6
+len(li) # => 6
-# 元组类似于列表,但是他是不可改变的
+# 元组类似于列表,但它是不可改变的
tup = (1, 2, 3)
-tup[0] #=> 1
+tup[0] # => 1
tup[0] = 3 # 类型错误
# 对于大多数的列表操作,也适用于元组
-len(tup) #=> 3
-tup + (4, 5, 6) #=> (1, 2, 3, 4, 5, 6)
-tup[:2] #=> (1, 2)
-2 in tup #=> True
+len(tup) # => 3
+tup + (4, 5, 6) # => (1, 2, 3, 4, 5, 6)
+tup[:2] # => (1, 2)
+2 in tup # => True
# 你可以将元组解包赋给多个变量
-a, b, c = (1, 2, 3) # a是1,b是2,c是3
-# 如果不加括号,那么会自动视为元组
+a, b, c = (1, 2, 3) # a 是 1,b 是 2,c 是 3
+# 如果不加括号,将会被自动视为元组
d, e, f = 4, 5, 6
# 现在我们可以看看交换两个数字是多么容易的事
-e, d = d, e # d是5,e是4
+e, d = d, e # d 是 5,e 是 4
# 字典用来储存映射关系
@@ -199,59 +200,59 @@ empty_dict = {}
filled_dict = {"one": 1, "two": 2, "three": 3}
# 字典也用中括号访问元素
-filled_dict["one"] #=> 1
+filled_dict["one"] # => 1
# 把所有的键保存在列表中
-filled_dict.keys() #=> ["three", "two", "one"]
+filled_dict.keys() # => ["three", "two", "one"]
# 键的顺序并不是唯一的,得到的不一定是这个顺序
# 把所有的值保存在列表中
-filled_dict.values() #=> [3, 2, 1]
+filled_dict.values() # => [3, 2, 1]
# 和键的顺序相同
# 判断一个键是否存在
-"one" in filled_dict #=> True
-1 in filled_dict #=> False
+"one" in filled_dict # => True
+1 in filled_dict # => False
-# 查询一个不存在的键会抛出键异常
-filled_dict["four"] # 键异常
+# 查询一个不存在的键会抛出 KeyError
+filled_dict["four"] # KeyError
-# 用get方法来避免键异常
-filled_dict.get("one") #=> 1
-filled_dict.get("four") #=> None
-# get方法支持在不存在的时候返回一个默认值
-filled_dict.get("one", 4) #=> 1
-filled_dict.get("four", 4) #=> 4
+# 用 get 方法来避免 KeyError
+filled_dict.get("one") # => 1
+filled_dict.get("four") # => None
+# get 方法支持在不存在的时候返回一个默认值
+filled_dict.get("one", 4) # => 1
+filled_dict.get("four", 4) # => 4
-# Setdefault是一个更安全的添加字典元素的方法
-filled_dict.setdefault("five", 5) #filled_dict["five"] 的值为 5
-filled_dict.setdefault("five", 6) #filled_dict["five"] 的值仍然是 5
+# setdefault 是一个更安全的添加字典元素的方法
+filled_dict.setdefault("five", 5) # filled_dict["five"] 的值为 5
+filled_dict.setdefault("five", 6) # filled_dict["five"] 的值仍然是 5
# 集合储存无顺序的元素
empty_set = set()
-# 出事话一个集合
-some_set = set([1,2,2,3,4]) # filled_set 现在是 set([1, 2, 3, 4])
+# 初始化一个集合
+some_set = set([1, 2, 2, 3, 4]) # some_set 现在是 set([1, 2, 3, 4])
# Python 2.7 之后,大括号可以用来表示集合
-filled_set = {1, 2, 2, 3, 4} # => {1 2 3 4}
+filled_set = {1, 2, 2, 3, 4} # => {1 2 3 4}
-# 为集合添加元素
-filled_set.add(5) # filled_set 现在是 {1, 2, 3, 4, 5}
+# 向集合添加元素
+filled_set.add(5) # filled_set 现在是 {1, 2, 3, 4, 5}
-# 用&来实现集合的交
+# 用 & 来计算集合的交
other_set = {3, 4, 5, 6}
-filled_set & other_set #=> {3, 4, 5}
+filled_set & other_set # => {3, 4, 5}
-# 用|来实现集合的并
-filled_set | other_set #=> {1, 2, 3, 4, 5, 6}
+# 用 | 来计算集合的并
+filled_set | other_set # => {1, 2, 3, 4, 5, 6}
-# 用-来实现集合的差
-{1,2,3,4} - {2,3,5} #=> {1, 4}
+# 用 - 来计算集合的差
+{1, 2, 3, 4} - {2, 3, 5} # => {1, 4}
-# 用in来判断元素是否存在于集合中
-2 in filled_set #=> True
-10 in filled_set #=> False
+# 用 in 来判断元素是否存在于集合中
+2 in filled_set # => True
+10 in filled_set # => False
####################################################
@@ -261,13 +262,13 @@ filled_set | other_set #=> {1, 2, 3, 4, 5, 6}
# 新建一个变量
some_var = 5
-# 这是个if语句,在python中缩进是很重要的。
-# 会输出 "some var is smaller than 10"
+# 这是个 if 语句,在 python 中缩进是很重要的。
+# 下面的代码片段将会输出 "some var is smaller than 10"
if some_var > 10:
print "some_var is totally bigger than 10."
elif some_var < 10: # 这个 elif 语句是不必须的
print "some_var is smaller than 10."
-else: # 也不是必须的
+else: # 这个 else 也不是必须的
print "some_var is indeed 10."
@@ -281,7 +282,7 @@ else: # 也不是必须的
for animal in ["dog", "cat", "mouse"]:
# 你可以用 % 来格式化字符串
print "%s is a mammal" % animal
-
+
"""
`range(number)` 返回从0到给定数字的列表
输出:
@@ -294,7 +295,7 @@ for i in range(4):
print i
"""
-While循环
+while 循环
输出:
0
1
@@ -304,29 +305,29 @@ While循环
x = 0
while x < 4:
print x
- x += 1 # Shorthand for x = x + 1
+ x += 1 # x = x + 1 的简写
-# 用 try/except块来处理异常
+# 用 try/except 块来处理异常
# Python 2.6 及以上适用:
try:
- # 用raise来抛出异常
+ # 用 raise 来抛出异常
raise IndexError("This is an index error")
except IndexError as e:
- pass # Pass就是什么都不做,不过通常这里会做一些恢复工作
+ pass # pass 就是什么都不做,不过通常这里会做一些恢复工作
####################################################
## 4. 函数
####################################################
-# 用def来新建函数
+# 用 def 来新建函数
def add(x, y):
print "x is %s and y is %s" % (x, y)
- return x + y # Return values with a return statement
+ return x + y # 通过 return 来返回值
# 调用带参数的函数
-add(5, 6) #=> 输出 "x is 5 and y is 6" 返回 11
+add(5, 6) # => 输出 "x is 5 and y is 6" 返回 11
# 通过关键字赋值来调用函数
add(y=6, x=5) # 顺序是无所谓的
@@ -335,7 +336,7 @@ add(y=6, x=5) # 顺序是无所谓的
def varargs(*args):
return args
-varargs(1, 2, 3) #=> (1,2,3)
+varargs(1, 2, 3) # => (1,2,3)
# 我们也可以定义接受多个变量的函数,这些变量是按照关键字排列的
@@ -343,7 +344,7 @@ def keyword_args(**kwargs):
return kwargs
# 实际效果:
-keyword_args(big="foot", loch="ness") #=> {"big": "foot", "loch": "ness"}
+keyword_args(big="foot", loch="ness") # => {"big": "foot", "loch": "ness"}
# 你也可以同时将一个函数定义成两种形式
def all_the_args(*args, **kwargs):
@@ -355,38 +356,38 @@ all_the_args(1, 2, a=3, b=4) prints:
{"a": 3, "b": 4}
"""
-# 当调用函数的时候,我们也可以和之前所做的相反,把元组和字典展开为参数
+# 当调用函数的时候,我们也可以进行相反的操作,把元组和字典展开为参数
args = (1, 2, 3, 4)
kwargs = {"a": 3, "b": 4}
-all_the_args(*args) # equivalent to foo(1, 2, 3, 4)
-all_the_args(**kwargs) # equivalent to foo(a=3, b=4)
-all_the_args(*args, **kwargs) # equivalent to foo(1, 2, 3, 4, a=3, b=4)
+all_the_args(*args) # 等价于 foo(1, 2, 3, 4)
+all_the_args(**kwargs) # 等价于 foo(a=3, b=4)
+all_the_args(*args, **kwargs) # 等价于 foo(1, 2, 3, 4, a=3, b=4)
-# Python 有一等函数:
+# 函数在 python 中是一等公民
def create_adder(x):
def adder(y):
return x + y
return adder
add_10 = create_adder(10)
-add_10(3) #=> 13
+add_10(3) # => 13
# 匿名函数
-(lambda x: x > 2)(3) #=> True
+(lambda x: x > 2)(3) # => True
# 内置高阶函数
-map(add_10, [1,2,3]) #=> [11, 12, 13]
-filter(lambda x: x > 5, [3, 4, 5, 6, 7]) #=> [6, 7]
+map(add_10, [1, 2, 3]) # => [11, 12, 13]
+filter(lambda x: x > 5, [3, 4, 5, 6, 7]) # => [6, 7]
# 可以用列表方法来对高阶函数进行更巧妙的引用
-[add_10(i) for i in [1, 2, 3]] #=> [11, 12, 13]
-[x for x in [3, 4, 5, 6, 7] if x > 5] #=> [6, 7]
+[add_10(i) for i in [1, 2, 3]] # => [11, 12, 13]
+[x for x in [3, 4, 5, 6, 7] if x > 5] # => [6, 7]
####################################################
## 5. 类
####################################################
-# 我们新建的类是从object类中继承的
+# 我们新建的类是从 object 类中继承的
class Human(object):
# 类属性,由所有类的对象共享
@@ -397,9 +398,9 @@ class Human(object):
# 将参数赋给对象成员属性
self.name = name
- # 成员方法,参数要有self
+ # 成员方法,参数要有 self
def say(self, msg):
- return "%s: %s" % (self.name, msg)
+ return "%s: %s" % (self.name, msg)
# 类方法由所有类的对象共享
# 这类方法在调用时,会把类本身传给第一个参数
@@ -421,15 +422,15 @@ j = Human("Joel")
print j.say("hello") # 输出 "Joel: hello"
# 访问类的方法
-i.get_species() #=> "H. sapiens"
+i.get_species() # => "H. sapiens"
# 改变共享属性
Human.species = "H. neanderthalensis"
-i.get_species() #=> "H. neanderthalensis"
-j.get_species() #=> "H. neanderthalensis"
+i.get_species() # => "H. neanderthalensis"
+j.get_species() # => "H. neanderthalensis"
# 访问静态变量
-Human.grunt() #=> "*grunt*"
+Human.grunt() # => "*grunt*"
####################################################
@@ -438,12 +439,12 @@ Human.grunt() #=> "*grunt*"
# 我们可以导入其他模块
import math
-print math.sqrt(16) #=> 4
+print math.sqrt(16) # => 4
-# 我们也可以从一个模块中特定的函数
+# 我们也可以从一个模块中导入特定的函数
from math import ceil, floor
-print ceil(3.7) #=> 4.0
-print floor(3.7) #=> 3.0
+print ceil(3.7) # => 4.0
+print floor(3.7) # => 3.0
# 从模块中导入所有的函数
# 警告:不推荐使用
@@ -451,13 +452,13 @@ from math import *
# 简写模块名
import math as m
-math.sqrt(16) == m.sqrt(16) #=> True
+math.sqrt(16) == m.sqrt(16) # => True
# Python的模块其实只是普通的python文件
# 你也可以创建自己的模块,并且导入它们
# 模块的名字就和文件的名字相同
-# 以可以通过下面的信息找找要成为模块需要什么属性或方法
+# 也可以通过下面的方法查看模块中有什么属性和方法
import math
dir(math)
diff --git a/zh-cn/r-cn.html.markdown b/zh-cn/r-cn.html.markdown
new file mode 100644
index 00000000..19c5f25d
--- /dev/null
+++ b/zh-cn/r-cn.html.markdown
@@ -0,0 +1,541 @@
+---
+language: R
+contributors:
+ - ["e99n09", "http://github.com/e99n09"]
+ - ["isomorphismes", "http://twitter.com/isomorphisms"]
+translators:
+ - ["小柒", "http://weibo.com/u/2328126220"]
+ - ["alswl", "https://github.com/alswl"]
+filename: learnr-zh.r
+lang: zh-cn
+---
+
+R 是一门统计语言。它有很多数据分析和挖掘程序包。可以用来统计、分析和制图。
+你也可以在 LaTeX 文档中运行 `R` 命令。
+
+```python
+# 评论以 # 开始
+
+# R 语言原生不支持 多行注释
+# 但是你可以像这样来多行注释
+
+# 在窗口里按回车键可以执行一条命令
+
+
+###################################################################
+# 不用懂编程就可以开始动手了
+###################################################################
+
+data() # 浏览内建的数据集
+data(rivers) # 北美主要河流的长度(数据集)
+ls() # 在工作空间中查看「河流」是否出现
+head(rivers) # 撇一眼数据集
+# 735 320 325 392 524 450
+length(rivers) # 我们测量了多少条河流?
+# 141
+summary(rivers)
+# Min. 1st Qu. Median Mean 3rd Qu. Max.
+# 135.0 310.0 425.0 591.2 680.0 3710.0
+stem(rivers) # 茎叶图(一种类似于直方图的展现形式)
+#
+# The decimal point is 2 digit(s) to the right of the |
+#
+# 0 | 4
+# 2 | 011223334555566667778888899900001111223333344455555666688888999
+# 4 | 111222333445566779001233344567
+# 6 | 000112233578012234468
+# 8 | 045790018
+# 10 | 04507
+# 12 | 1471
+# 14 | 56
+# 16 | 7
+# 18 | 9
+# 20 |
+# 22 | 25
+# 24 | 3
+# 26 |
+# 28 |
+# 30 |
+# 32 |
+# 34 |
+# 36 | 1
+
+
+stem(log(rivers)) # 查看数据集的方式既不是标准形式,也不是取log后的结果! 看起来,是钟形曲线形式的基本数据集
+
+# The decimal point is 1 digit(s) to the left of the |
+#
+# 48 | 1
+# 50 |
+# 52 | 15578
+# 54 | 44571222466689
+# 56 | 023334677000124455789
+# 58 | 00122366666999933445777
+# 60 | 122445567800133459
+# 62 | 112666799035
+# 64 | 00011334581257889
+# 66 | 003683579
+# 68 | 0019156
+# 70 | 079357
+# 72 | 89
+# 74 | 84
+# 76 | 56
+# 78 | 4
+# 80 |
+# 82 | 2
+
+
+hist(rivers, col="#333333", border="white", breaks=25) # 试试用这些参数画画 (译者注:给 river 做统计频数直方图,包含了这些参数:数据源,颜色,边框,空格)
+hist(log(rivers), col="#333333", border="white", breaks=25) #你还可以做更多式样的绘图
+
+# 还有其他一些简单的数据集可以被用来加载。R 语言包括了大量这种 data()
+data(discoveries)
+plot(discoveries, col="#333333", lwd=3, xlab="Year", main="Number of important discoveries per year")
+# 译者注:参数为(数据源,颜色,线条宽度,X 轴名称,标题)
+plot(discoveries, col="#333333", lwd=3, type = "h", xlab="Year", main="Number of important discoveries per year")
+
+
+# 除了按照默认的年份排序,我们还可以排序来发现特征
+sort(discoveries)
+# [1] 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2
+# [26] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3
+# [51] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4
+# [76] 4 4 4 4 5 5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 8 9 10 12
+
+stem(discoveries, scale=2) # 译者注:茎叶图(数据,放大系数)
+#
+# The decimal point is at the |
+#
+# 0 | 000000000
+# 1 | 000000000000
+# 2 | 00000000000000000000000000
+# 3 | 00000000000000000000
+# 4 | 000000000000
+# 5 | 0000000
+# 6 | 000000
+# 7 | 0000
+# 8 | 0
+# 9 | 0
+# 10 | 0
+# 11 |
+# 12 | 0
+
+max(discoveries)
+# 12
+
+summary(discoveries)
+# Min. 1st Qu. Median Mean 3rd Qu. Max.
+# 0.0 2.0 3.0 3.1 4.0 12.0
+
+
+
+
+#基本的统计学操作也不需要任何编程知识
+
+#随机生成数据
+round(runif(7, min=.5, max=6.5))
+# 译者注:runif 产生随机数,round 四舍五入
+# 1 4 6 1 4 6 4
+
+# 你输出的结果会和我们给出的不同,除非我们设置了相同的随机种子 random.seed(31337)
+
+
+#从标准高斯函数中随机生成 9 次
+rnorm(9)
+# [1] 0.07528471 1.03499859 1.34809556 -0.82356087 0.61638975 -1.88757271
+# [7] -0.59975593 0.57629164 1.08455362
+
+
+
+
+
+
+
+
+
+#########################
+# 基础编程
+#########################
+
+# 数值
+
+#“数值”指的是双精度的浮点数
+5 # 5
+class(5) # "numeric"
+5e4 # 50000 # 用科学技术法方便的处理极大值、极小值或者可变的量级
+6.02e23 # 阿伏伽德罗常数#
+1.6e-35 # 布朗克长度
+
+# 长整数并用 L 结尾
+5L # 5
+#输出5L
+class(5L) # "integer"
+
+# 可以自己试一试?用 class() 函数获取更多信息
+# 事实上,你可以找一些文件查阅 `xyz` 以及xyz的差别
+# `xyz` 用来查看源码实现,?xyz 用来看帮助
+
+# 算法
+10 + 66 # 76
+53.2 - 4 # 49.2
+2 * 2.0 # 4
+3L / 4 # 0.75
+3 %% 2 # 1
+
+# 特殊数值类型
+class(NaN) # "numeric"
+class(Inf) # "numeric"
+class(-Inf) # "numeric" # 在以下场景中会用到 integrate( dnorm(x), 3, Inf ) -- 消除 Z 轴数据
+
+# 但要注意,NaN 并不是唯一的特殊数值类型……
+class(NA) # 看上面
+class(NULL) # NULL
+
+
+# 简单列表
+c(6, 8, 7, 5, 3, 0, 9) # 6 8 7 5 3 0 9
+c('alef', 'bet', 'gimmel', 'dalet', 'he')
+c('Z', 'o', 'r', 'o') == "Zoro" # FALSE FALSE FALSE FALSE
+
+# 一些优雅的内置功能
+5:15 # 5 6 7 8 9 10 11 12 13 14 15
+
+seq(from=0, to=31337, by=1337)
+# [1] 0 1337 2674 4011 5348 6685 8022 9359 10696 12033 13370 14707
+# [13] 16044 17381 18718 20055 21392 22729 24066 25403 26740 28077 29414 30751
+
+letters
+# [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
+# [20] "t" "u" "v" "w" "x" "y" "z"
+
+month.abb # "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
+
+
+# Access the n'th element of a list with list.name[n] or sometimes list.name[[n]]
+# 使用 list.name[n] 来访问第 n 个列表元素,有时候需要使用 list.name[[n]]
+letters[18] # "r"
+LETTERS[13] # "M"
+month.name[9] # "September"
+c(6, 8, 7, 5, 3, 0, 9)[3] # 7
+
+
+
+# 字符串
+
+# 字符串和字符在 R 语言中没有区别
+"Horatio" # "Horatio"
+class("Horatio") # "character"
+substr("Fortuna multis dat nimis, nulli satis.", 9, 15) # "multis "
+gsub('u', 'ø', "Fortuna multis dat nimis, nulli satis.") # "Fortøna møltis dat nimis, nølli satis."
+
+
+
+# 逻辑值
+
+# 布尔值
+class(TRUE) # "logical"
+class(FALSE) # "logical"
+# 和我们预想的一样
+TRUE == TRUE # TRUE
+TRUE == FALSE # FALSE
+FALSE != FALSE # FALSE
+FALSE != TRUE # TRUE
+# 缺失数据(NA)也是逻辑值
+class(NA) # "logical"
+#定义NA为逻辑型
+
+
+
+# 因子
+# 因子是为数据分类排序设计的(像是排序小朋友们的年级或性别)
+levels(factor(c("female", "male", "male", "female", "NA", "female"))) # "female" "male" "NA"
+
+factor(c("female", "female", "male", "NA", "female"))
+# female female male NA female
+# Levels: female male NA
+
+data(infert) # 自然以及引产导致的不育症
+levels(infert$education) # "0-5yrs" "6-11yrs" "12+ yrs"
+
+
+
+# 变量
+
+# 有许多种方式用来赋值
+x = 5 # 这样可以
+y <- "1" # 更推荐这样
+TRUE -> z # 这样可行,但是很怪
+
+#我们还可以使用强制转型
+as.numeric(y) # 1
+as.character(x) # "5"
+
+# 循环
+
+# for 循环语句
+for (i in 1:4) {
+ print(i)
+}
+
+# while 循环
+a <- 10
+while (a > 4) {
+ cat(a, "...", sep = "")
+ a <- a - 1
+}
+
+# 记住,在 R 语言中 for / while 循环都很慢
+# 建议使用 apply()(我们一会介绍)来错做一串数据(比如一列或者一行数据)
+
+# IF/ELSE
+
+# 再来看这些优雅的标准
+if (4 > 3) {
+ print("Huzzah! It worked!")
+} else {
+ print("Noooo! This is blatantly illogical!")
+}
+
+# =>
+# [1] "Huzzah! It worked!"
+
+# 函数
+
+# 定义如下
+jiggle <- function(x) {
+ x + rnorm(x, sd=.1) #add in a bit of (controlled) noise
+ return(x)
+}
+
+# 和其他 R 语言函数一样调用
+jiggle(5) # 5±ε. 使用 set.seed(2716057) 后, jiggle(5)==5.005043
+
+#########################
+# 数据容器:vectors, matrices, data frames, and arrays
+#########################
+
+# 单维度
+# 你可以将目前我们学习到的任何类型矢量化,只要它们拥有相同的类型
+vec <- c(8, 9, 10, 11)
+vec # 8 9 10 11
+# 矢量的类型是这一组数据元素的类型
+class(vec) # "numeric"
+# If you vectorize items of different classes, weird coercions happen
+#如果你强制的将不同类型数值矢量化,会出现特殊值
+c(TRUE, 4) # 1 4
+c("dog", TRUE, 4) # "dog" "TRUE" "4"
+
+#我们这样来取内部数据,(R 的下标索引顺序 1 开始)
+vec[1] # 8
+# 我们可以根据条件查找特定数据
+which(vec %% 2 == 0) # 1 3
+# 抓取矢量中第一个和最后一个字符
+head(vec, 1) # 8
+tail(vec, 1) # 11
+#如果下标溢出或不存会得到 NA
+vec[6] # NA
+# 你可以使用 length() 获取矢量的长度
+length(vec) # 4
+
+# 你可以直接操作矢量或者矢量的子集
+vec * 4 # 16 20 24 28
+vec[2:3] * 5 # 25 30
+# 这里有许多内置的函数,来表现向量
+mean(vec) # 9.5
+var(vec) # 1.666667
+sd(vec) # 1.290994
+max(vec) # 11
+min(vec) # 8
+sum(vec) # 38
+
+# 二维(相同元素类型)
+
+#你可以为同样类型的变量建立矩阵
+mat <- matrix(nrow = 3, ncol = 2, c(1,2,3,4,5,6))
+mat
+# =>
+# [,1] [,2]
+# [1,] 1 4
+# [2,] 2 5
+# [3,] 3 6
+# 和 vector 不一样的是,一个矩阵的类型真的是 「matrix」,而不是内部元素的类型
+class(mat) # => "matrix"
+# 访问第一行的字符
+mat[1,] # 1 4
+# 操作第一行数据
+3 * mat[,1] # 3 6 9
+# 访问一个特定数据
+mat[3,2] # 6
+# 转置整个矩阵(译者注:变成 2 行 3 列)
+t(mat)
+# =>
+# [,1] [,2] [,3]
+# [1,] 1 2 3
+# [2,] 4 5 6
+
+# 使用 cbind() 函数把两个矩阵按列合并,形成新的矩阵
+mat2 <- cbind(1:4, c("dog", "cat", "bird", "dog"))
+mat2
+# =>
+# [,1] [,2]
+# [1,] "1" "dog"
+# [2,] "2" "cat"
+# [3,] "3" "bird"
+# [4,] "4" "dog"
+class(mat2) # matrix
+# Again, note what happened!
+# 注意
+# 因为矩阵内部元素必须包含同样的类型
+# 所以现在每一个元素都转化成字符串
+c(class(mat2[,1]), class(mat2[,2]))
+
+# 按行合并两个向量,建立新的矩阵
+mat3 <- rbind(c(1,2,4,5), c(6,7,0,4))
+mat3
+# =>
+# [,1] [,2] [,3] [,4]
+# [1,] 1 2 4 5
+# [2,] 6 7 0 4
+# 哈哈,数据类型都一样的,没有发生强制转换,生活真美好
+
+# 二维(不同的元素类型)
+
+# 利用 data frame 可以将不同类型数据放在一起
+dat <- data.frame(c(5,2,1,4), c("dog", "cat", "bird", "dog"))
+names(dat) <- c("number", "species") # 给数据列命名
+class(dat) # "data.frame"
+dat
+# =>
+# number species
+# 1 5 dog
+# 2 2 cat
+# 3 1 bird
+# 4 4 dog
+class(dat$number) # "numeric"
+class(dat[,2]) # "factor"
+# data.frame() 会将字符向量转换为 factor 向量
+
+# 有很多精妙的方法来获取 data frame 的子数据集
+dat$number # 5 2 1 4
+dat[,1] # 5 2 1 4
+dat[,"number"] # 5 2 1 4
+
+# 多维(相同元素类型)
+
+# 使用 arry 创造一个 n 维的表格
+# You can make a two-dimensional table (sort of like a matrix)
+# 你可以建立一个 2 维表格(有点像矩阵)
+array(c(c(1,2,4,5),c(8,9,3,6)), dim=c(2,4))
+# =>
+# [,1] [,2] [,3] [,4]
+# [1,] 1 4 8 3
+# [2,] 2 5 9 6
+#你也可以利用数组建立一个三维的矩阵
+array(c(c(c(2,300,4),c(8,9,0)),c(c(5,60,0),c(66,7,847))), dim=c(3,2,2))
+# =>
+# , , 1
+#
+# [,1] [,2]
+# [1,] 2 8
+# [2,] 300 9
+# [3,] 4 0
+#
+# , , 2
+#
+# [,1] [,2]
+# [1,] 5 66
+# [2,] 60 7
+# [3,] 0 847
+
+#列表(多维的,不同类型的)
+
+# R语言有列表的形式
+list1 <- list(time = 1:40)
+list1$price = c(rnorm(40,.5*list1$time,4)) # 随机
+list1
+
+# You can get items in the list like so
+# 你可以这样获得列表的元素
+list1$time
+# You can subset list items like vectors
+# 你也可以和矢量一样获取他们的子集
+list1$price[4]
+
+#########################
+# apply()函数家族
+#########################
+
+# 还记得 mat 么?
+mat
+# =>
+# [,1] [,2]
+# [1,] 1 4
+# [2,] 2 5
+# [3,] 3 6
+# Use apply(X, MARGIN, FUN) to apply function FUN to a matrix X
+# 使用(X, MARGIN, FUN)将函数 FUN 应用到矩阵 X 的行 (MAR = 1) 或者 列 (MAR = 2)
+# That is, R does FUN to each row (or column) of X, much faster than a
+# R 在 X 的每一行/列使用 FUN,比循环要快很多
+apply(mat, MAR = 2, myFunc)
+# =>
+# [,1] [,2]
+# [1,] 3 15
+# [2,] 7 19
+# [3,] 11 23
+# 还有其他家族函数 ?lapply, ?sapply
+
+# 不要被吓到,虽然许多人在此都被搞混
+# plyr 程序包的作用是用来改进 apply() 函数家族
+
+install.packages("plyr")
+require(plyr)
+?plyr
+
+#########################
+# 载入数据
+#########################
+
+# "pets.csv" 是网上的一个文本
+pets <- read.csv("http://learnxinyminutes.com/docs/pets.csv")
+pets
+head(pets, 2) # 前两行
+tail(pets, 1) # 最后一行
+
+# 以 .csv 格式来保存数据集或者矩阵
+write.csv(pets, "pets2.csv") # 保存到新的文件 pets2.csv
+# set working directory with setwd(), look it up with getwd()
+# 使用 setwd() 改变工作目录,使用 getwd() 查看当前工作目录
+
+# 尝试使用 ?read.csv 和 ?write.csv 来查看更多信息
+
+#########################
+# 画图
+#########################
+
+# 散点图
+plot(list1$time, list1$price, main = "fake data") # 译者注:横轴 list1$time,纵轴 wlist1$price,标题 fake data
+# 回归图
+linearModel <- lm(price ~ time, data = list1) # 译者注:线性模型,数据集为list1,以价格对时间做相关分析模型
+linearModel # 拟合结果
+# 将拟合结果展示在图上,颜色设为红色
+abline(linearModel, col = "red")
+# 也可以获取各种各样漂亮的分析图
+plot(linearModel)
+
+# 直方图
+hist(rpois(n = 10000, lambda = 5), col = "thistle") # 译者注:统计频数直方图
+
+# 柱状图
+barplot(c(1,4,5,1,2), names.arg = c("red","blue","purple","green","yellow"))
+
+# 可以尝试着使用 ggplot2 程序包来美化图片
+install.packages("ggplot2")
+require(ggplot2)
+?ggplot2
+
+```
+
+## 获得 R
+
+* 从 [http://www.r-project.org/](http://www.r-project.org/) 获得安装包和图形化界面
+* [RStudio](http://www.rstudio.com/ide/) 是另一个图形化界面
diff --git a/zh-cn/racket-cn.html.markdown b/zh-cn/racket-cn.html.markdown
new file mode 100644
index 00000000..8ef3671f
--- /dev/null
+++ b/zh-cn/racket-cn.html.markdown
@@ -0,0 +1,608 @@
+---
+
+language: racket
+lang: zh-cn
+filename: learnracket-zh.rkt
+contributors:
+ - ["th3rac25", "https://github.com/voila"]
+ - ["Eli Barzilay", "https://github.com/elibarzilay"]
+ - ["Gustavo Schmidt", "https://github.com/gustavoschmidt"]
+translators:
+ - ["lyuehh", "https://github.com/lyuehh"]
+---
+
+Racket是Lisp/Scheme家族中的一个通用的,多范式的编程语言。
+非常期待您的反馈!你可以通过[@th3rac25](http://twitter.com/th3rac25)或以用户名为 th3rac25 的Google邮箱服务和我取得联系
+
+```racket
+#lang racket ; 声明我们使用的语言
+
+;;; 注释
+
+;; 单行注释以分号开始
+
+#| 块注释
+ 可以横跨很多行而且...
+ #|
+ 可以嵌套
+ |#
+|#
+
+;; S表达式注释忽略剩下的表达式
+;; 在调试的时候会非常有用
+#; (被忽略的表达式)
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 1. 原始数据类型和操作符
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;;; 数字
+9999999999999999999999 ; 整数
+#b111 ; 二进制数字 => 7
+#o111 ; 八进制数字 => 73
+#x111 ; 十六进制数字 => 273
+3.14 ; 实数
+6.02e+23
+1/2 ; 有理数
+1+2i ; 复数
+
+;; 函数调用写作(f x y z ...)
+;; 在这里 f 是一个函数, x, y, z, ... 是参数
+;; 如果你想创建一个列表数据的字面量, 使用 ' 来阻止它们
+;; 被求值
+'(+ 1 2) ; => (+ 1 2)
+;; 接下来,是一些数学运算
+(+ 1 1) ; => 2
+(- 8 1) ; => 7
+(* 10 2) ; => 20
+(expt 2 3) ; => 8
+(quotient 5 2) ; => 2
+(remainder 5 2) ; => 1
+(/ 35 5) ; => 7
+(/ 1 3) ; => 1/3
+(exact->inexact 1/3) ; => 0.3333333333333333
+(+ 1+2i 2-3i) ; => 3-1i
+
+;;; 布尔类型
+#t ; 为真
+#f ; 为假,#f 之外的任何值都是真
+(not #t) ; => #f
+(and 0 #f (error "doesn't get here")) ; => #f
+(or #f 0 (error "doesn't get here")) ; => 0
+
+;;; 字符
+#\A ; => #\A
+#\λ ; => #\λ
+#\u03BB ; => #\λ
+
+;;; 字符串是字符组成的定长数组
+"Hello, world!"
+"Benjamin \"Bugsy\" Siegel" ; \是转义字符
+"Foo\tbar\41\x21\u0021\a\r\n" ; 包含C语言的转义字符,和Unicode
+"λx:(μα.α→α).xx" ; 字符串可以包含Unicode字符
+
+;; 字符串可以相加
+(string-append "Hello " "world!") ; => "Hello world!"
+
+;; 一个字符串可以看做是一个包含字符的列表
+(string-ref "Apple" 0) ; => #\A
+
+;; format 可以用来格式化字符串
+(format "~a can be ~a" "strings" "formatted")
+
+;; 打印字符串非常简单
+(printf "I'm Racket. Nice to meet you!\n")
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 2. 变量
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 你可以使用 define 定义一个变量
+;; 变量的名字可以使用任何字符除了: ()[]{}",'`;#|\
+(define some-var 5)
+some-var ; => 5
+
+;; 你也可以使用Unicode字符
+(define ⊆ subset?)
+(⊆ (set 3 2) (set 1 2 3)) ; => #t
+
+;; 访问未赋值的变量会引发一个异常
+; x ; => x: undefined ...
+
+;; 本地绑定: `me' 被绑定到 "Bob",并且只在 let 中生效
+(let ([me "Bob"])
+ "Alice"
+ me) ; => "Bob"
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 3. 结构和集合
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;; 结构体
+(struct dog (name breed age))
+(define my-pet
+ (dog "lassie" "collie" 5))
+my-pet ; => #<dog>
+(dog? my-pet) ; => #t
+(dog-name my-pet) ; => "lassie"
+
+;;; 对 (不可变的)
+;; `cons' 返回对, `car' 和 `cdr' 从对中提取第1个
+;; 和第2个元素
+(cons 1 2) ; => '(1 . 2)
+(car (cons 1 2)) ; => 1
+(cdr (cons 1 2)) ; => 2
+
+;;; 列表
+
+;; 列表由链表构成, 由 `cons' 的结果
+;; 和一个 `null' (或者 '()) 构成,后者标记了这个列表的结束
+(cons 1 (cons 2 (cons 3 null))) ; => '(1 2 3)
+;; `list' 给列表提供了一个非常方便的可变参数的生成器
+(list 1 2 3) ; => '(1 2 3)
+;; 一个单引号也可以用来表示一个列表字面量
+'(1 2 3) ; => '(1 2 3)
+
+;; 仍然可以使用 `cons' 在列表的开始处添加一项
+(cons 4 '(1 2 3)) ; => '(4 1 2 3)
+
+;; `append' 函数可以将两个列表合并
+(append '(1 2) '(3 4)) ; => '(1 2 3 4)
+
+;; 列表是非常基础的类型,所以有*很多*操作列表的方法
+;; 下面是一些例子:
+(map add1 '(1 2 3)) ; => '(2 3 4)
+(map + '(1 2 3) '(10 20 30)) ; => '(11 22 33)
+(filter even? '(1 2 3 4)) ; => '(2 4)
+(count even? '(1 2 3 4)) ; => 2
+(take '(1 2 3 4) 2) ; => '(1 2)
+(drop '(1 2 3 4) 2) ; => '(3 4)
+
+;;; 向量
+
+;; 向量是定长的数组
+#(1 2 3) ; => '#(1 2 3)
+
+;; 使用 `vector-append' 方法将2个向量合并
+(vector-append #(1 2 3) #(4 5 6)) ; => #(1 2 3 4 5 6)
+
+;;; Set(翻译成集合也不太合适,所以不翻译了..)
+
+;; 从一个列表创建一个Set
+(list->set '(1 2 3 1 2 3 3 2 1 3 2 1)) ; => (set 1 2 3)
+
+;; 使用 `set-add' 增加一个成员
+;; (函数式特性: 这里会返回一个扩展后的Set,而不是修改输入的值)
+(set-add (set 1 2 3) 4) ; => (set 1 2 3 4)
+
+;; 使用 `set-remove' 移除一个成员
+(set-remove (set 1 2 3) 1) ; => (set 2 3)
+
+;; 使用 `set-member?' 测试成员是否存在
+(set-member? (set 1 2 3) 1) ; => #t
+(set-member? (set 1 2 3) 4) ; => #f
+
+;;; 散列表
+
+;; 创建一个不变的散列表 (可变散列表的例子在下面)
+(define m (hash 'a 1 'b 2 'c 3))
+
+;; 根据键取得值
+(hash-ref m 'a) ; => 1
+
+;; 获取一个不存在的键是一个异常
+; (hash-ref m 'd) => 没有找到元素
+
+;; 你可以给不存在的键提供一个默认值
+(hash-ref m 'd 0) ; => 0
+
+;; 使用 `hash-set' 来扩展一个不可变的散列表
+;; (返回的是扩展后的散列表而不是修改它)
+(define m2 (hash-set m 'd 4))
+m2 ; => '#hash((b . 2) (a . 1) (d . 4) (c . 3))
+
+;; 记住,使用 `hash` 创建的散列表是不可变的
+m ; => '#hash((b . 2) (a . 1) (c . 3)) <-- no `d'
+
+;; 使用 `hash-remove' 移除一个键值对 (函数式特性,m并不变)
+(hash-remove m 'a) ; => '#hash((b . 2) (c . 3))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 3. 函数
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;; 使用 `lambda' 创建函数
+;; 函数总是返回它最后一个表达式的值
+(lambda () "Hello World") ; => #<procedure>
+;; 也可以使用 Unicode 字符 `λ'
+(λ () "Hello World") ; => 同样的函数
+
+;; 使用括号调用一个函数,也可以直接调用一个 lambda 表达式
+((lambda () "Hello World")) ; => "Hello World"
+((λ () "Hello World")) ; => "Hello World"
+
+;; 将函数赋值为一个变量
+(define hello-world (lambda () "Hello World"))
+(hello-world) ; => "Hello World"
+
+;; 你可以使用函数定义的语法糖来简化代码
+(define (hello-world2) "Hello World")
+
+;; `()`是函数的参数列表
+(define hello
+ (lambda (name)
+ (string-append "Hello " name)))
+(hello "Steve") ; => "Hello Steve"
+;; 同样的,可以使用语法糖来定义:
+(define (hello2 name)
+ (string-append "Hello " name))
+
+;; 你也可以使用可变参数, `case-lambda'
+(define hello3
+ (case-lambda
+ [() "Hello World"]
+ [(name) (string-append "Hello " name)]))
+(hello3 "Jake") ; => "Hello Jake"
+(hello3) ; => "Hello World"
+;; ... 或者给参数指定一个可选的默认值
+(define (hello4 [name "World"])
+ (string-append "Hello " name))
+
+;; 函数可以将多余的参数放到一个列表里
+(define (count-args . args)
+ (format "You passed ~a args: ~a" (length args) args))
+(count-args 1 2 3) ; => "You passed 3 args: (1 2 3)"
+;; ... 也可以使用不带语法糖的 `lambda' 形式:
+(define count-args2
+ (lambda args
+ (format "You passed ~a args: ~a" (length args) args)))
+
+;; 你可以混用两种用法
+(define (hello-count name . args)
+ (format "Hello ~a, you passed ~a extra args" name (length args)))
+(hello-count "Finn" 1 2 3)
+; => "Hello Finn, you passed 3 extra args"
+;; ... 不带语法糖的形式:
+(define hello-count2
+ (lambda (name . args)
+ (format "Hello ~a, you passed ~a extra args" name (length args))))
+
+;; 使用关键字
+(define (hello-k #:name [name "World"] #:greeting [g "Hello"] . args)
+ (format "~a ~a, ~a extra args" g name (length args)))
+(hello-k) ; => "Hello World, 0 extra args"
+(hello-k 1 2 3) ; => "Hello World, 3 extra args"
+(hello-k #:greeting "Hi") ; => "Hi World, 0 extra args"
+(hello-k #:name "Finn" #:greeting "Hey") ; => "Hey Finn, 0 extra args"
+(hello-k 1 2 3 #:greeting "Hi" #:name "Finn" 4 5 6)
+ ; => "Hi Finn, 6 extra args"
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 4. 判断是否相等
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;; 判断数字使用 `='
+(= 3 3.0) ; => #t
+(= 2 1) ; => #f
+
+;; 判断对象使用 `eq?'
+(eq? 3 3) ; => #t
+(eq? 3 3.0) ; => #f
+(eq? (list 3) (list 3)) ; => #f
+
+;; 判断集合使用 `equal?'
+(equal? (list 'a 'b) (list 'a 'b)) ; => #t
+(equal? (list 'a 'b) (list 'b 'a)) ; => #f
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 5. 控制结构
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;;; 条件判断
+
+(if #t ; 测试表达式
+ "this is true" ; 为真的表达式
+ "this is false") ; 为假的表达式
+; => "this is true"
+
+;; 注意, 除 `#f` 之外的所有值都认为是真
+(member 'Groucho '(Harpo Groucho Zeppo)) ; => '(Groucho Zeppo)
+(if (member 'Groucho '(Harpo Groucho Zeppo))
+ 'yep
+ 'nope)
+; => 'yep
+
+;; `cond' 会进行一系列的判断来选择一个结果
+(cond [(> 2 2) (error "wrong!")]
+ [(< 2 2) (error "wrong again!")]
+ [else 'ok]) ; => 'ok
+
+;;; 模式匹配
+
+(define (fizzbuzz? n)
+ (match (list (remainder n 3) (remainder n 5))
+ [(list 0 0) 'fizzbuzz]
+ [(list 0 _) 'fizz]
+ [(list _ 0) 'buzz]
+ [_ #f]))
+
+(fizzbuzz? 15) ; => 'fizzbuzz
+(fizzbuzz? 37) ; => #f
+
+;;; 循环
+
+;; 循环可以使用递归(尾递归)
+(define (loop i)
+ (when (< i 10)
+ (printf "i=~a\n" i)
+ (loop (add1 i))))
+(loop 5) ; => i=5, i=6, ...
+
+;; 类似的,可以使用 `let` 定义
+(let loop ((i 0))
+ (when (< i 10)
+ (printf "i=~a\n" i)
+ (loop (add1 i)))) ; => i=0, i=1, ...
+
+;; 看上面的例子怎么增加一个新的 `loop' 形式, 但是 Racket 已经有了一个非常
+;; 灵活的 `for' 了:
+(for ([i 10])
+ (printf "i=~a\n" i)) ; => i=0, i=1, ...
+(for ([i (in-range 5 10)])
+ (printf "i=~a\n" i)) ; => i=5, i=6, ...
+
+;;; 其他形式的迭代
+;; `for' 允许在很多数据结构中迭代:
+;; 列表, 向量, 字符串, Set, 散列表, 等...
+
+(for ([i (in-list '(l i s t))])
+ (displayln i))
+
+(for ([i (in-vector #(v e c t o r))])
+ (displayln i))
+
+(for ([i (in-string "string")])
+ (displayln i))
+
+(for ([i (in-set (set 'x 'y 'z))])
+ (displayln i))
+
+(for ([(k v) (in-hash (hash 'a 1 'b 2 'c 3 ))])
+ (printf "key:~a value:~a\n" k v))
+
+;;; 更多复杂的迭代
+
+;; 并行扫描多个序列 (遇到长度小的就停止)
+(for ([i 10] [j '(x y z)]) (printf "~a:~a\n" i j))
+; => 0:x 1:y 2:z
+
+;; 嵌套循环
+(for* ([i 2] [j '(x y z)]) (printf "~a:~a\n" i j))
+; => 0:x, 0:y, 0:z, 1:x, 1:y, 1:z
+
+;; 带有条件判断的 `for`
+(for ([i 1000]
+ #:when (> i 5)
+ #:unless (odd? i)
+ #:break (> i 10))
+ (printf "i=~a\n" i))
+; => i=6, i=8, i=10
+
+;;; 更多的例子帮助你加深理解..
+;; 和 `for' 循环非常像 -- 收集结果
+
+(for/list ([i '(1 2 3)])
+ (add1 i)) ; => '(2 3 4)
+
+(for/list ([i '(1 2 3)] #:when (even? i))
+ i) ; => '(2)
+
+(for/list ([i 10] [j '(x y z)])
+ (list i j)) ; => '((0 x) (1 y) (2 z))
+
+(for/list ([i 1000] #:when (> i 5) #:unless (odd? i) #:break (> i 10))
+ i) ; => '(6 8 10)
+
+(for/hash ([i '(1 2 3)])
+ (values i (number->string i)))
+; => '#hash((1 . "1") (2 . "2") (3 . "3"))
+
+;; 也有很多其他的内置方法来收集循环中的值:
+(for/sum ([i 10]) (* i i)) ; => 285
+(for/product ([i (in-range 1 11)]) (* i i)) ; => 13168189440000
+(for/and ([i 10] [j (in-range 10 20)]) (< i j)) ; => #t
+(for/or ([i 10] [j (in-range 0 20 2)]) (= i j)) ; => #t
+;; 如果需要合并计算结果, 使用 `for/fold'
+(for/fold ([sum 0]) ([i '(1 2 3 4)]) (+ sum i)) ; => 10
+;; (这个函数可以在大部分情况下替代普通的命令式循环)
+
+;;; 异常
+
+;; 要捕获一个异常,使用 `with-handlers' 形式
+(with-handlers ([exn:fail? (lambda (exn) 999)])
+ (+ 1 "2")) ; => 999
+(with-handlers ([exn:break? (lambda (exn) "no time")])
+ (sleep 3)
+ "phew") ; => "phew", 如果你打断了它,那么结果 => "no time"
+
+;; 使用 `raise' 抛出一个异常后者其他任何值
+(with-handlers ([number? ; 捕获抛出的数字类型的值
+ identity]) ; 将它们作为普通值
+ (+ 1 (raise 2))) ; => 2
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 6. 可变的值
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;; 使用 `set!' 给一个已经存在的变量赋一个新值
+(define n 5)
+(set! n (add1 n))
+n ; => 6
+
+;; 给那些明确地需要变化的值使用 `boxes` (在其他语言里类似指针
+;; 或者引用)
+(define n* (box 5))
+(set-box! n* (add1 (unbox n*)))
+(unbox n*) ; => 6
+
+;; 很多 Racket 诗句类型是不可变的 (对,列表,等),有一些既是可变的
+;; 又是不可变的 (字符串,向量,散列表
+;; 等...)
+
+;; 使用 `vector' 或者 `make-vector' 创建一个可变的向量
+(define vec (vector 2 2 3 4))
+(define wall (make-vector 100 'bottle-of-beer))
+;; 使用 `vector-set!` 更新一项
+(vector-set! vec 0 1)
+(vector-set! wall 99 'down)
+vec ; => #(1 2 3 4)
+
+;; 创建一个空的可变散列表,然后操作它
+(define m3 (make-hash))
+(hash-set! m3 'a 1)
+(hash-set! m3 'b 2)
+(hash-set! m3 'c 3)
+(hash-ref m3 'a) ; => 1
+(hash-ref m3 'd 0) ; => 0
+(hash-remove! m3 'a)
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 7. 模块
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;; 模块让你将你的代码组织为多个文件,成为可重用的模块,
+;; 在这里,我们使用嵌套在本文的整个大模块
+;; 里的子模块(从 "#lang" 这一行开始)
+
+(module cake racket/base ; 基于 racket/base 定义一个 `cake` 模块
+
+ (provide print-cake) ; 这个模块导出的函数
+
+ (define (print-cake n)
+ (show " ~a " n #\.)
+ (show " .-~a-. " n #\|)
+ (show " | ~a | " n #\space)
+ (show "---~a---" n #\-))
+
+ (define (show fmt n ch) ; 内部函数
+ (printf fmt (make-string n ch))
+ (newline)))
+
+;; 使用 `require` 从模块中得到所有 `provide` 的函数
+(require 'cake) ; 这里的 `'`表示是本地的子模块
+(print-cake 3)
+; (show "~a" 1 #\A) ; => 报错, `show' 没有被导出,不存在
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 8. 类和对象
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;; 创建一个 fish% 类(%是给类绑定用的)
+(define fish%
+ (class object%
+ (init size) ; 初始化的参数
+ (super-new) ; 父类的初始化
+ ;; 域
+ (define current-size size)
+ ;; 公共方法
+ (define/public (get-size)
+ current-size)
+ (define/public (grow amt)
+ (set! current-size (+ amt current-size)))
+ (define/public (eat other-fish)
+ (grow (send other-fish get-size)))))
+
+;; 创建一个 fish% 类的示例
+(define charlie
+ (new fish% [size 10]))
+
+;; 使用 `send' 调用一个对象的方法
+(send charlie get-size) ; => 10
+(send charlie grow 6)
+(send charlie get-size) ; => 16
+
+;; `fish%' 是一个普通的值,我们可以用它来混入
+(define (add-color c%)
+ (class c%
+ (init color)
+ (super-new)
+ (define my-color color)
+ (define/public (get-color) my-color)))
+(define colored-fish% (add-color fish%))
+(define charlie2 (new colored-fish% [size 10] [color 'red]))
+(send charlie2 get-color)
+;; 或者,不带名字
+(send (new (add-color fish%) [size 10] [color 'red]) get-color)
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 9. 宏
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;; 宏让你扩展这门语言的语法
+
+;; 让我们定义一个while循环
+(define-syntax-rule (while condition body ...)
+ (let loop ()
+ (when condition
+ body ...
+ (loop))))
+
+(let ([i 0])
+ (while (< i 10)
+ (displayln i)
+ (set! i (add1 i))))
+
+;; 宏是安全的,你不能修改现有的变量
+(define-syntax-rule (swap! x y) ; !表示会修改
+ (let ([tmp x])
+ (set! x y)
+ (set! y tmp)))
+
+(define tmp 2)
+(define other 3)
+(swap! tmp other)
+(printf "tmp = ~a; other = ~a\n" tmp other)
+;; 变量 `tmp` 被重命名为 `tmp_1`
+;; 避免名字冲突
+;; (let ([tmp_1 tmp])
+;; (set! tmp other)
+;; (set! other tmp_1))
+
+;; 但它们仍然会导致错误代码,比如:
+(define-syntax-rule (bad-while condition body ...)
+ (when condition
+ body ...
+ (bad-while condition body ...)))
+;; 这个宏会挂掉,它产生了一个无限循环,如果你试图去使用它
+;; 编译器会进入死循环
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 10. 契约
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;; 契约限制变量从模块中导入
+
+(module bank-account racket
+ (provide (contract-out
+ [deposit (-> positive? any)] ; 数量一直是正值
+ [balance (-> positive?)]))
+
+ (define amount 0)
+ (define (deposit a) (set! amount (+ amount a)))
+ (define (balance) amount)
+ )
+
+(require 'bank-account)
+(deposit 5)
+
+(balance) ; => 5
+
+;; 客户端尝试存储一个负值时会出错
+;; (deposit -5) ; => deposit: contract violation
+;; expected: positive?
+;; given: -5
+;; more details....
+```
+
+## 进一步阅读
+
+想知道更多吗? 尝试 [Getting Started with Racket](http://docs.racket-lang.org/getting-started/)
diff --git a/zh-cn/ruby-cn.html.markdown b/zh-cn/ruby-cn.html.markdown
index 6530b520..3c47f3f9 100644
--- a/zh-cn/ruby-cn.html.markdown
+++ b/zh-cn/ruby-cn.html.markdown
@@ -6,6 +6,7 @@ contributors:
- ["David Underwood", "http://theflyingdeveloper.com"]
- ["Joel Walden", "http://joelwalden.net"]
- ["Luke Holder", "http://twitter.com/lukeholder"]
+ - ["lidashuang", "https://github.com/lidashuang"]
translators:
- ["Lin Xiangyu", "https://github.com/oa414"]
---
@@ -94,7 +95,7 @@ x = y = 10 #=> 10
x #=> 10
y #=> 10
-# 按照惯例,用snake_case 作为变量名
+# 按照惯例,用 snake_case 作为变量名
snake_case = true
# 使用具有描述性的运算符
@@ -102,7 +103,8 @@ path_to_project_root = '/good/name/'
path = '/bad/name/'
# 符号(Symbols,也是对象)
-# 符号是不可变的,内部用整数类型表示的可重用的值。通常用它代替字符串来有效地表达有意义的值
+# 符号是不可变的,内部用整数类型表示的可重用的值。
+# 通常用它代替字符串来有效地表示有意义的值。
:pending.class #=> Symbol
@@ -172,7 +174,7 @@ new_hash = { defcon: 3, action: true}
new_hash.keys #=> [:defcon, :action]
# 小贴士:数组和哈希表都是可枚举的
-# 它们可以共享一些有用的方法,比如each, map, count, 和more
+# 它们可以共享一些有用的方法,比如each, map, count 等等
# 控制流
diff --git a/zh-cn/scala-cn.html.markdown b/zh-cn/scala-cn.html.markdown
index 1ce41ac6..24f73bb5 100644
--- a/zh-cn/scala-cn.html.markdown
+++ b/zh-cn/scala-cn.html.markdown
@@ -6,7 +6,6 @@ contributors:
- ["Dominic Bou-Samra", "http://dbousamra.github.com"]
translators:
- ["Peiyong Lin", ""]
-filename: learn.scala
lang: zh-cn
---
diff --git a/zh-cn/visualbasic-cn.html.markdown b/zh-cn/visualbasic-cn.html.markdown
new file mode 100644
index 00000000..95f01ed6
--- /dev/null
+++ b/zh-cn/visualbasic-cn.html.markdown
@@ -0,0 +1,274 @@
+---
+language: Visual Basic
+contributors:
+ - ["Brian Martin", "http://brianmartin.biz"]
+translators:
+ - ["Abner Chou", "http://github.com/NoahDragon"]
+lang: zh-cn
+filename: learnvisualbasic.vb-cn
+---
+
+```vb
+Module Module1
+
+ Sub Main()
+ ' 让我们先从简单的终端程序学起。
+ ' 单引号用来生成注释(注意是半角单引号,非全角单引号’)
+ ' 为了方便运行此示例代码,我写了个目录索引。
+ ' 可能你还不了解以下代码的意义,但随着教程的深入,
+ ' 你会渐渐理解其用法。
+ Console.Title = ("Learn X in Y Minutes")
+ Console.WriteLine("NAVIGATION") ' 显示目录
+ Console.WriteLine("")
+ Console.ForegroundColor = ConsoleColor.Green
+ Console.WriteLine("1. Hello World Output") ' Hello world 输出示例
+ Console.WriteLine("2. Hello World Input") ' Hello world 输入示例
+ Console.WriteLine("3. Calculating Whole Numbers") ' 求整数之和
+ Console.WriteLine("4. Calculating Decimal Numbers") ' 求小数之和
+ Console.WriteLine("5. Working Calculator") ' 计算器
+ Console.WriteLine("6. Using Do While Loops") ' 使用 Do While 循环
+ Console.WriteLine("7. Using For While Loops") ' 使用 For While 循环
+ Console.WriteLine("8. Conditional Statements") ' 条件语句
+ Console.WriteLine("9. Select A Drink") ' 选饮料
+ Console.WriteLine("50. About") ' 关于
+ Console.WriteLine("Please Choose A Number From The Above List")
+ Dim selection As String = Console.ReadLine
+ Select Case selection
+ Case "1" ' Hello world 输出示例
+ Console.Clear() ' 清空屏幕
+ HelloWorldOutput() ' 调用程序块
+ Case "2" ' Hello world 输入示例
+ Console.Clear()
+ HelloWorldInput()
+ Case "3" ' 求整数之和
+ Console.Clear()
+ CalculatingWholeNumbers()
+ Case "4" ' 求小数之和
+ Console.Clear()
+ CalculatingDecimalNumbers()
+ Case "5" ' 计算器
+ Console.Clear()
+ WorkingCalculator()
+ Case "6" ' 使用 do while 循环
+ Console.Clear()
+ UsingDoWhileLoops()
+ Case "7" ' 使用 for while 循环
+ Console.Clear()
+ UsingForLoops()
+ Case "8" ' 条件语句
+ Console.Clear()
+ ConditionalStatement()
+ Case "9" ' If/Else 条件语句
+ Console.Clear()
+ IfElseStatement() ' 选饮料
+ Case "50" ' 关于本程序和作者
+ Console.Clear()
+ Console.Title = ("Learn X in Y Minutes :: About")
+ MsgBox("This tutorial is by Brian Martin (@BrianMartinn")
+ Console.Clear()
+ Main()
+ Console.ReadLine()
+
+ End Select
+ End Sub
+
+ ' 一、对应程序目录1,下同
+
+ ' 使用 private subs 声明函数。
+ Private Sub HelloWorldOutput()
+ ' 程序名
+ Console.Title = "Hello World Ouput | Learn X in Y Minutes"
+ ' 使用 Console.Write("") 或者 Console.WriteLine("") 来输出文本到屏幕上
+ ' 对应的 Console.Read() 或 Console.Readline() 用来读取键盘输入
+ Console.WriteLine("Hello World")
+ Console.ReadLine()
+ ' Console.WriteLine()后加Console.ReadLine()是为了防止屏幕输出信息一闪而过
+ ' 类似平时常见的“单击任意键继续”的意思。
+ End Sub
+
+ ' 二
+ Private Sub HelloWorldInput()
+ Console.Title = "Hello World YourName | Learn X in Y Minutes"
+ ' 变量
+ ' 用来存储用户输入的数据
+ ' 变量声明以 Dim 开始,结尾为 As VariableType (变量类型).
+
+ ' 此教程中,我们希望知道你的姓名,并让程序记录并输出。
+ Dim username As String
+ ' 我们定义username使用字符串类型(String)来记录用户姓名。
+ Console.WriteLine("Hello, What is your name? ") ' 询问用户输入姓名
+ username = Console.ReadLine() ' 存储用户名到变量 username
+ Console.WriteLine("Hello " + username) ' 输出将是 Hello + username
+ Console.ReadLine() ' 暂停屏幕并显示以上输出
+ ' 以上程序将询问你的姓名,并和你打招呼。
+ ' 其它变量如整型(Integer)我们用整型来处理整数。
+ End Sub
+
+ ' 三
+ Private Sub CalculatingWholeNumbers()
+ Console.Title = "Calculating Whole Numbers | Learn X in Y Minutes"
+ Console.Write("First number: ") ' 输入一个整数:1,2,50,104,等等
+ Dim a As Integer = Console.ReadLine()
+ Console.Write("Second number: ") ' 输入第二个整数
+ Dim b As Integer = Console.ReadLine()
+ Dim c As Integer = a + b
+ Console.WriteLine(c)
+ Console.ReadLine()
+ ' 以上程序将两个整数相加
+ End Sub
+
+ ' 四
+ Private Sub CalculatingDecimalNumbers()
+ Console.Title = "Calculating with Double | Learn X in Y Minutes"
+ ' 当然,我们还需要能够处理小数。
+ ' 只需要要将整型(Integer)改为小数(Double)类型即可。
+
+ ' 输入一个小数: 1.2, 2.4, 50.1, 104.9,等等
+ Console.Write("First number: ")
+ Dim a As Double = Console.ReadLine
+ Console.Write("Second number: ") ' 输入第二个数
+ Dim b As Double = Console.ReadLine
+ Dim c As Double = a + b
+ Console.WriteLine(c)
+ Console.ReadLine()
+ ' 以上代码能实现两个小数相加
+ End Sub
+
+ ' 五
+ Private Sub WorkingCalculator()
+ Console.Title = "The Working Calculator| Learn X in Y Minutes"
+ ' 但是如果你希望有个能够处理加减乘除的计算器呢?
+ ' 只需将上面代码复制粘帖即可。
+ Console.Write("First number: ") ' 输入第一个数
+ Dim a As Double = Console.ReadLine
+ Console.Write("Second number: ") ' 输入第二个数
+ Dim b As Integer = Console.ReadLine
+ Dim c As Integer = a + b
+ Dim d As Integer = a * b
+ Dim e As Integer = a - b
+ Dim f As Integer = a / b
+
+ ' 通过以下代码我们可以将以上所算的加减乘除结果输出到屏幕上。
+ Console.Write(a.ToString() + " + " + b.ToString())
+ ' 我们希望答案开头能有3个空格,可以使用String.PadLeft(3)方法。
+ Console.WriteLine(" = " + c.ToString.PadLeft(3))
+ Console.Write(a.ToString() + " * " + b.ToString())
+ Console.WriteLine(" = " + d.ToString.PadLeft(3))
+ Console.Write(a.ToString() + " - " + b.ToString())
+ Console.WriteLine(" = " + e.ToString.PadLeft(3))
+ Console.Write(a.ToString() + " / " + b.ToString())
+ Console.WriteLine(" = " + e.ToString.PadLeft(3))
+ Console.ReadLine()
+
+ End Sub
+
+ ' 六
+ Private Sub UsingDoWhileLoops()
+ ' 如同以上的代码一样
+ ' 这次我们将询问用户是否继续 (Yes or No?)
+ ' 我们将使用Do While循环,因为我们不知到用户是否需要使用一次以上。
+ Console.Title = "UsingDoWhileLoops | Learn X in Y Minutes"
+ Dim answer As String ' 我们使用字符串变量来存储answer(答案)
+ Do ' 循环开始
+ Console.Write("First number: ")
+ Dim a As Double = Console.ReadLine
+ Console.Write("Second number: ")
+ Dim b As Integer = Console.ReadLine
+ Dim c As Integer = a + b
+ Dim d As Integer = a * b
+ Dim e As Integer = a - b
+ Dim f As Integer = a / b
+
+ Console.Write(a.ToString() + " + " + b.ToString())
+ Console.WriteLine(" = " + c.ToString.PadLeft(3))
+ Console.Write(a.ToString() + " * " + b.ToString())
+ Console.WriteLine(" = " + d.ToString.PadLeft(3))
+ Console.Write(a.ToString() + " - " + b.ToString())
+ Console.WriteLine(" = " + e.ToString.PadLeft(3))
+ Console.Write(a.ToString() + " / " + b.ToString())
+ Console.WriteLine(" = " + e.ToString.PadLeft(3))
+ Console.ReadLine()
+ ' 询问用户是否继续,注意大小写。
+ Console.Write("Would you like to continue? (yes / no)")
+ ' 程序读入用户输入
+ answer = Console.ReadLine() ' added a bracket here
+ ' 当用户输入"yes"时,程序将跳转到Do,并再次执行
+ Loop While answer = "yes"
+
+ End Sub
+
+ ' 七
+ Private Sub UsingForLoops()
+ ' 有一些程序只需要运行一次。
+ ' 这个程序我们将实现从10倒数计数.
+
+ Console.Title = "Using For Loops | Learn X in Y Minutes"
+ ' 声明变量和Step (步长,即递减的速度,如-1,-2,-3等)。
+ For i As Integer = 10 To 0 Step -1
+ Console.WriteLine(i.ToString) ' 将计数结果输出的屏幕
+ Next i ' 计算新的i值
+ Console.WriteLine("Start")
+ Console.ReadLine()
+ End Sub
+
+ ' 八
+ Private Sub ConditionalStatement()
+ Console.Title = "Conditional Statements | Learn X in Y Minutes"
+ Dim userName As String = Console.ReadLine
+ Console.WriteLine("Hello, What is your name? ") ' 询问用户姓名
+ userName = Console.ReadLine() ' 存储用户姓名
+ If userName = "Adam" Then
+ Console.WriteLine("Hello Adam")
+ Console.WriteLine("Thanks for creating this useful site")
+ Console.ReadLine()
+ Else
+ Console.WriteLine("Hello " + userName)
+ Console.WriteLine("Have you checked out www.learnxinyminutes.com")
+ Console.ReadLine() ' 程序停止,并输出以上文本
+ End If
+ End Sub
+
+ ' 九
+ Private Sub IfElseStatement()
+ Console.Title = "If / Else Statement | Learn X in Y Minutes"
+ ' 有时候我们需要考虑多于两种情况。
+ ' 这时我们就需要使用If/ElesIf条件语句。
+ ' If语句就好似个自动售货机,当用户输入A1,A2,A3,等去选择物品时,
+ ' 所有的选择可以合并到一个If语句中
+
+ Dim selection As String = Console.ReadLine() ' 读入用户选择
+ Console.WriteLine("A1. for 7Up") ' A1 七喜
+ Console.WriteLine("A2. for Fanta") ' A2 芬达
+ Console.WriteLine("A3. for Dr. Pepper") ' A3 胡椒医生
+ Console.WriteLine("A4. for Diet Coke") ' A4 无糖可乐
+ Console.ReadLine()
+ If selection = "A1" Then
+ Console.WriteLine("7up")
+ Console.ReadLine()
+ ElseIf selection = "A2" Then
+ Console.WriteLine("fanta")
+ Console.ReadLine()
+ ElseIf selection = "A3" Then
+ Console.WriteLine("dr. pepper")
+ Console.ReadLine()
+ ElseIf selection = "A4" Then
+ Console.WriteLine("diet coke")
+ Console.ReadLine()
+ Else
+ Console.WriteLine("Please select a product") ' 请选择你需要的产品
+ Console.ReadLine()
+ End If
+
+ End Sub
+
+End Module
+
+```
+
+## 参考
+
+我(译注:原作者)在命令行下学习的VB。命令行编程使我能够更好的了解程序编译运行机制,并使学习其它语言变得容易。
+
+如果希望进一步学习VB,这里还有更深层次的 <a href="http://www.vbbootcamp.co.uk/" Title="VB教学">VB教学(英文)</a>。
+
+所有代码均通过测试。只需复制粘帖到Visual Basic中,并按F5运行即可。