summaryrefslogtreecommitdiffhomepage
path: root/zh-cn
diff options
context:
space:
mode:
Diffstat (limited to 'zh-cn')
-rwxr-xr-xzh-cn/c-cn.html.markdown2
-rw-r--r--zh-cn/dart-cn.html.markdown499
-rwxr-xr-xzh-cn/elisp-cn.html.markdown2
-rwxr-xr-xzh-cn/git-cn.html.markdown1
-rwxr-xr-xzh-cn/haskell-cn.html.markdown4
-rwxr-xr-xzh-cn/java-cn.html.markdown2
-rwxr-xr-xzh-cn/javascript-cn.html.markdown1
-rwxr-xr-xzh-cn/php-cn.html.markdown2
-rwxr-xr-xzh-cn/python-cn.html.markdown279
-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.markdown413
12 files changed, 1672 insertions, 149 deletions
diff --git a/zh-cn/c-cn.html.markdown b/zh-cn/c-cn.html.markdown
index f8a8e0bd..b4bff8fc 100755
--- a/zh-cn/c-cn.html.markdown
+++ b/zh-cn/c-cn.html.markdown
@@ -1,6 +1,6 @@
---
language: c
-filename: learnc.c
+filename: learnc-cn.c
contributors:
- ["Adam Bard", "http://adambard.com/"]
translators:
diff --git a/zh-cn/dart-cn.html.markdown b/zh-cn/dart-cn.html.markdown
new file mode 100644
index 00000000..6a6562bc
--- /dev/null
+++ b/zh-cn/dart-cn.html.markdown
@@ -0,0 +1,499 @@
+---
+language: dart
+lang: zh-cn
+filename: learndart-cn.dart
+contributors:
+ - ["Joao Pedrosa", "https://github.com/jpedrosa/"]
+translators:
+ - ["Guokai Han", "https://github.com/hanguokai/"]
+---
+
+Dart 是编程语言王国的新人。
+它借鉴了许多其他主流语言,并且不会偏离它的兄弟语言 JavaScript 太多。
+就像 JavaScript 一样,Dart 的目标是提供良好的浏览器集成。
+
+Dart 最有争议的特性必然是它的可选类型。
+
+```javascript
+import "dart:collection";
+import "dart:math" as DM;
+
+// 欢迎进入15分钟的 Dart 学习。 http://www.dartlang.org/
+// 这是一个可实际执行的向导。你可以用 Dart 运行它
+// 或者在线执行! 可以把代码复制/粘贴到这个网站。 http://try.dartlang.org/
+
+// 函数声明和方法声明看起来一样。
+// 函数声明可以嵌套。声明使用这种 name() {} 的形式,
+// 或者 name() => 单行表达式; 的形式。
+// 右箭头的声明形式会隐式地返回表达式的结果。
+example1() {
+ example1nested1() {
+ example1nested2() => print("Example1 nested 1 nested 2");
+ example1nested2();
+ }
+ example1nested1();
+}
+
+// 匿名函数没有函数名。
+example2() {
+ example2nested1(fn) {
+ fn();
+ }
+ example2nested1(() => print("Example2 nested 1"));
+}
+
+// 当声明函数类型的参数的时候,声明中可以包含
+// 函数参数需要的参数,指定所需的参数名即可。
+example3() {
+ example3nested1(fn(informSomething)) {
+ fn("Example3 nested 1");
+ }
+ example3planB(fn) { // 或者不声明函数参数的参数
+ fn("Example3 plan B");
+ }
+ example3nested1((s) => print(s));
+ example3planB((s) => print(s));
+}
+
+// 函数有可以访问到外层变量的闭包。
+var example4Something = "Example4 nested 1";
+example4() {
+ example4nested1(fn(informSomething)) {
+ fn(example4Something);
+ }
+ example4nested1((s) => print(s));
+}
+
+// 下面这个包含 sayIt 方法的类声明,同样有一个可以访问外层变量的闭包,
+// 就像前面的函数一样。
+var example5method = "Example5 sayIt";
+class Example5Class {
+ sayIt() {
+ print(example5method);
+ }
+}
+example5() {
+ // 创建一个 Example5Class 类的匿名实例,
+ // 并调用它的 sayIt 方法。
+ new Example5Class().sayIt();
+}
+
+// 类的声明使用这种形式 class name { [classBody] }.
+// classBody 中可以包含实例方法和变量,
+// 还可以包含类方法和变量。
+class Example6Class {
+ var example6InstanceVariable = "Example6 instance variable";
+ sayIt() {
+ print(example6InstanceVariable);
+ }
+}
+example6() {
+ new Example6Class().sayIt();
+}
+
+// 类方法和变量使用 static 关键词声明。
+class Example7Class {
+ static var example7ClassVariable = "Example7 class variable";
+ static sayItFromClass() {
+ print(example7ClassVariable);
+ }
+ sayItFromInstance() {
+ print(example7ClassVariable);
+ }
+}
+example7() {
+ Example7Class.sayItFromClass();
+ new Example7Class().sayItFromInstance();
+}
+
+// 字面量非常方便,但是对于在函数/方法的外层的字面量有一个限制,
+// 类的外层或外面的字面量必需是常量。
+// 字符串和数字默认是常量。
+// 但是 array 和 map 不是。他们需要用 "const" 声明为常量。
+var example8A = const ["Example8 const array"],
+ example8M = const {"someKey": "Example8 const map"};
+example8() {
+ print(example8A[0]);
+ print(example8M["someKey"]);
+}
+
+// Dart 中的循环使用标准的 for () {} 或 while () {} 的形式,
+// 以及更加现代的 for (.. in ..) {} 的形式, 或者
+// 以 forEach 开头并具有许多特性支持的函数回调的形式。
+var example9A = const ["a", "b"];
+example9() {
+ for (var i = 0; i < example9A.length; i++) {
+ print("Example9 for loop '${example9A[i]}'");
+ }
+ var i = 0;
+ while (i < example9A.length) {
+ print("Example9 while loop '${example9A[i]}'");
+ i++;
+ }
+ for (var e in example9A) {
+ print("Example9 for-in loop '${e}'");
+ }
+ example9A.forEach((e) => print("Example9 forEach loop '${e}'"));
+}
+
+// 遍历字符串中的每个字符或者提取其子串。
+var example10S = "ab";
+example10() {
+ for (var i = 0; i < example10S.length; i++) {
+ print("Example10 String character loop '${example10S[i]}'");
+ }
+ for (var i = 0; i < example10S.length; i++) {
+ print("Example10 substring loop '${example10S.substring(i, i + 1)}'");
+ }
+}
+
+// 支持两种数字格式 int 和 double 。
+example11() {
+ var i = 1 + 320, d = 3.2 + 0.01;
+ print("Example11 int ${i}");
+ print("Example11 double ${d}");
+}
+
+// DateTime 提供了日期/时间的算法。
+example12() {
+ var now = new DateTime.now();
+ print("Example12 now '${now}'");
+ now = now.add(new Duration(days: 1));
+ print("Example12 tomorrow '${now}'");
+}
+
+// 支持正则表达式。
+example13() {
+ var s1 = "some string", s2 = "some", re = new RegExp("^s.+?g\$");
+ match(s) {
+ if (re.hasMatch(s)) {
+ print("Example13 regexp matches '${s}'");
+ } else {
+ print("Example13 regexp doesn't match '${s}'");
+ }
+ }
+ match(s1);
+ match(s2);
+}
+
+// 布尔表达式必需被解析为 true 或 false,
+// 因为不支持隐式转换。
+example14() {
+ var v = true;
+ if (v) {
+ print("Example14 value is true");
+ }
+ v = null;
+ try {
+ if (v) {
+ // 不会执行
+ } else {
+ // 不会执行
+ }
+ } catch (e) {
+ print("Example14 null value causes an exception: '${e}'");
+ }
+}
+
+// try/catch/finally 和 throw 语句用于异常处理。
+// throw 语句可以使用任何对象作为参数。
+example15() {
+ try {
+ try {
+ throw "Some unexpected error.";
+ } catch (e) {
+ print("Example15 an exception: '${e}'");
+ throw e; // Re-throw
+ }
+ } catch (e) {
+ print("Example15 catch exception being re-thrown: '${e}'");
+ } finally {
+ print("Example15 Still run finally");
+ }
+}
+
+// 要想有效地动态创建长字符串,
+// 应该使用 StringBuffer。 或者 join 一个字符串的数组。
+example16() {
+ var sb = new StringBuffer(), a = ["a", "b", "c", "d"], e;
+ for (e in a) { sb.write(e); }
+ print("Example16 dynamic string created with "
+ "StringBuffer '${sb.toString()}'");
+ print("Example16 join string array '${a.join()}'");
+}
+
+// 字符串连接只需让相邻的字符串字面量挨着,
+// 不需要额外的操作符。
+example17() {
+ print("Example17 "
+ "concatenate "
+ "strings "
+ "just like that");
+}
+
+// 字符串使用单引号或双引号做分隔符,二者并没有实际的差异。
+// 这种灵活性可以很好地避免内容中需要转义分隔符的情况。
+// 例如,字符串内容里的 HTML 属性使用了双引号。
+example18() {
+ print('Example18 <a href="etc">'
+ "Don't can't I'm Etc"
+ '</a>');
+}
+
+// 用三个单引号或三个双引号表示的字符串
+// 可以跨越多行,并且包含行分隔符。
+example19() {
+ print('''Example19 <a href="etc">
+Example19 Don't can't I'm Etc
+Example19 </a>''');
+}
+
+// 字符串可以使用 $ 字符插入内容。
+// 使用 $ { [expression] } 的形式,表达式的值会被插入到字符串中。
+// $ 跟着一个变量名会插入变量的值。
+// 如果要在字符串中插入 $ ,可以使用 \$ 的转义形式代替。
+example20() {
+ var s1 = "'\${s}'", s2 = "'\$s'";
+ print("Example20 \$ interpolation ${s1} or $s2 works.");
+}
+
+// 可选类型允许作为 API 的标注,并且可以辅助 IDE,
+// 这样 IDE 可以更好地提供重构、自动完成和错误检测功能。
+// 目前为止我们还没有声明任何类型,并且程序运行地很好。
+// 事实上,类型在运行时会被忽略。
+// 类型甚至可以是错的,并且程序依然可以执行,
+// 好像和类型完全无关一样。
+// 有一个运行时参数可以让程序进入检查模式,它会在运行时检查类型错误。
+// 这在开发时很有用,但是由于增加了额外的检查会使程序变慢,
+// 因此应该避免在部署时使用。
+class Example21 {
+ List<String> _names;
+ Example21() {
+ _names = ["a", "b"];
+ }
+ List<String> get names => _names;
+ set names(List<String> list) {
+ _names = list;
+ }
+ int get length => _names.length;
+ void add(String name) {
+ _names.add(name);
+ }
+}
+void example21() {
+ Example21 o = new Example21();
+ o.add("c");
+ print("Example21 names '${o.names}' and length '${o.length}'");
+ o.names = ["d", "e"];
+ print("Example21 names '${o.names}' and length '${o.length}'");
+}
+
+// 类的继承形式是 class name extends AnotherClassName {} 。
+class Example22A {
+ var _name = "Some Name!";
+ get name => _name;
+}
+class Example22B extends Example22A {}
+example22() {
+ var o = new Example22B();
+ print("Example22 class inheritance '${o.name}'");
+}
+
+// 类也可以使用 mixin 的形式 :
+// class name extends SomeClass with AnotherClassName {}.
+// 必需继承某个类才能 mixin 另一个类。
+// 当前 mixin 的模板类不能有构造函数。
+// Mixin 主要是用来和辅助的类共享方法的,
+// 这样单一继承就不会影响代码复用。
+// Mixin 声明在类定义的 "with" 关键词后面。
+class Example23A {}
+class Example23Utils {
+ addTwo(n1, n2) {
+ return n1 + n2;
+ }
+}
+class Example23B extends Example23A with Example23Utils {
+ addThree(n1, n2, n3) {
+ return addTwo(n1, n2) + n3;
+ }
+}
+example23() {
+ var o = new Example23B(), r1 = o.addThree(1, 2, 3),
+ r2 = o.addTwo(1, 2);
+ print("Example23 addThree(1, 2, 3) results in '${r1}'");
+ print("Example23 addTwo(1, 2) results in '${r2}'");
+}
+
+// 类的构造函数名和类名相同,形式为
+// SomeClass() : super() {}, 其中 ": super()" 的部分是可选的,
+// 它用来传递参数给父类的构造函数。
+class Example24A {
+ var _value;
+ Example24A({value: "someValue"}) {
+ _value = value;
+ }
+ get value => _value;
+}
+class Example24B extends Example24A {
+ Example24B({value: "someOtherValue"}) : super(value: value);
+}
+example24() {
+ var o1 = new Example24B(),
+ o2 = new Example24B(value: "evenMore");
+ print("Example24 calling super during constructor '${o1.value}'");
+ print("Example24 calling super during constructor '${o2.value}'");
+}
+
+// 对于简单的类,有一种设置构造函数参数的快捷方式。
+// 只需要使用 this.parameterName 的前缀,
+// 它就会把参数设置为同名的实例变量。
+class Example25 {
+ var value, anotherValue;
+ Example25({this.value, this.anotherValue});
+}
+example25() {
+ var o = new Example25(value: "a", anotherValue: "b");
+ print("Example25 shortcut for constructor '${o.value}' and "
+ "'${o.anotherValue}'");
+}
+
+// 可以在大括号 {} 中声明命名参数。
+// 大括号 {} 中声明的参数的顺序是随意的。
+// 在中括号 [] 中声明的参数也是可选的。
+example26() {
+ var _name, _surname, _email;
+ setConfig1({name, surname}) {
+ _name = name;
+ _surname = surname;
+ }
+ setConfig2(name, [surname, email]) {
+ _name = name;
+ _surname = surname;
+ _email = email;
+ }
+ setConfig1(surname: "Doe", name: "John");
+ print("Example26 name '${_name}', surname '${_surname}', "
+ "email '${_email}'");
+ setConfig2("Mary", "Jane");
+ print("Example26 name '${_name}', surname '${_surname}', "
+ "email '${_email}'");
+}
+
+// 使用 final 声明的变量只能被设置一次。
+// 在类里面,final 实例变量可以通过常量的构造函数参数设置。
+class Example27 {
+ final color1, color2;
+ // 更灵活一点的方法是在冒号 : 后面设置 final 实例变量。
+ Example27({this.color1, color2}) : color2 = color2;
+}
+example27() {
+ final color = "orange", o = new Example27(color1: "lilac", color2: "white");
+ print("Example27 color is '${color}'");
+ print("Example27 color is '${o.color1}' and '${o.color2}'");
+}
+
+// 要导入一个库,使用 import "libraryPath" 的形式,或者如果要导入的是
+// 核心库使用 import "dart:libraryName" 。还有一个称为 "pub" 的包管理工具,
+// 它使用 import "package:packageName" 的约定形式。
+// 看下这个文件顶部的 import "dart:collection"; 语句。
+// 导入语句必需在其它代码声明之前出现。IterableBase 来自于 dart:collection 。
+class Example28 extends IterableBase {
+ var names;
+ Example28() {
+ names = ["a", "b"];
+ }
+ get iterator => names.iterator;
+}
+example28() {
+ var o = new Example28();
+ o.forEach((name) => print("Example28 '${name}'"));
+}
+
+// 对于控制流语句,我们有:
+// * 必需带 break 的标准 switch 语句
+// * if-else 和三元操作符 ..?..:..
+// * 闭包和匿名函数
+// * break, continue 和 return 语句
+example29() {
+ var v = true ? 30 : 60;
+ switch (v) {
+ case 30:
+ print("Example29 switch statement");
+ break;
+ }
+ if (v < 30) {
+ } else if (v > 30) {
+ } else {
+ print("Example29 if-else statement");
+ }
+ callItForMe(fn()) {
+ return fn();
+ }
+ rand() {
+ v = new DM.Random().nextInt(50);
+ return v;
+ }
+ while (true) {
+ print("Example29 callItForMe(rand) '${callItForMe(rand)}'");
+ if (v != 30) {
+ break;
+ } else {
+ continue;
+ }
+ // 不会到这里。
+ }
+}
+
+// 解析 int,把 double 转成 int,或者使用 ~/ 操作符在除法计算时仅保留整数位。
+// 让我们也来场猜数游戏吧。
+example30() {
+ var gn, tooHigh = false,
+ n, n2 = (2.0).toInt(), top = int.parse("123") ~/ n2, bottom = 0;
+ top = top ~/ 6;
+ gn = new DM.Random().nextInt(top + 1); // +1 because nextInt top is exclusive
+ print("Example30 Guess a number between 0 and ${top}");
+ guessNumber(i) {
+ if (n == gn) {
+ print("Example30 Guessed right! The number is ${gn}");
+ } else {
+ tooHigh = n > gn;
+ print("Example30 Number ${n} is too "
+ "${tooHigh ? 'high' : 'low'}. Try again");
+ }
+ return n == gn;
+ }
+ n = (top - bottom) ~/ 2;
+ while (!guessNumber(n)) {
+ if (tooHigh) {
+ top = n - 1;
+ } else {
+ bottom = n + 1;
+ }
+ n = bottom + ((top - bottom) ~/ 2);
+ }
+}
+
+// 程序的唯一入口点是 main 函数。
+// 在程序开始执行 main 函数之前,不期望执行任何外层代码。
+// 这样可以帮助程序更快地加载,甚至仅惰性加载程序启动时需要的部分。
+main() {
+ print("Learn Dart in 15 minutes!");
+ [example1, example2, example3, example4, example5, example6, example7,
+ example8, example9, example10, example11, example12, example13, example14,
+ example15, example16, example17, example18, example19, example20,
+ example21, example22, example23, example24, example25, example26,
+ example27, example28, example29, example30
+ ].forEach((ef) => ef());
+}
+
+```
+
+## 延伸阅读
+
+Dart 有一个综合性网站。它涵盖了 API 参考、入门向导、文章以及更多,
+还包括一个有用的在线试用 Dart 页面。
+http://www.dartlang.org/
+http://try.dartlang.org/
+
+
+
diff --git a/zh-cn/elisp-cn.html.markdown b/zh-cn/elisp-cn.html.markdown
index c3a2f927..d303c2e8 100755
--- a/zh-cn/elisp-cn.html.markdown
+++ b/zh-cn/elisp-cn.html.markdown
@@ -4,7 +4,7 @@ contributors:
- ["Bastien Guerry", "http://bzg.fr"]
translators:
- ["Chenbo Li", "http://binarythink.net"]
-filename: learn-emacs-lisp.el
+filename: learn-emacs-lisp-zh.el
lang: zh-cn
---
diff --git a/zh-cn/git-cn.html.markdown b/zh-cn/git-cn.html.markdown
index 8c24f0b8..86952eba 100755
--- a/zh-cn/git-cn.html.markdown
+++ b/zh-cn/git-cn.html.markdown
@@ -5,7 +5,6 @@ contributors:
- ["Jake Prather", "http://github.com/JakeHP"]
translators:
- ["Chenbo Li", "http://binarythink.net"]
-filename: LearnGit.txt
lang: zh-cn
---
diff --git a/zh-cn/haskell-cn.html.markdown b/zh-cn/haskell-cn.html.markdown
index cb0de467..8d51f144 100755
--- a/zh-cn/haskell-cn.html.markdown
+++ b/zh-cn/haskell-cn.html.markdown
@@ -1,6 +1,6 @@
---
language: haskell
-filename: learn-haskell.hs
+filename: learn-haskell-zh.hs
contributors:
- ["Adit Bhargava", "http://adit.io"]
translators:
@@ -404,4 +404,4 @@ qsort (p:xs) = qsort lesser ++ [p] ++ qsort greater
你可以从优秀的
[Learn you a Haskell](http://learnyouahaskell.com/) 或者
[Real World Haskell](http://book.realworldhaskell.org/)
-找到优雅不少的入门介绍。 \ No newline at end of file
+找到优雅不少的入门介绍。
diff --git a/zh-cn/java-cn.html.markdown b/zh-cn/java-cn.html.markdown
index b9ccf61a..9422ac2f 100755
--- a/zh-cn/java-cn.html.markdown
+++ b/zh-cn/java-cn.html.markdown
@@ -3,7 +3,7 @@ name: java
category: language
language: java
lang: zh-cn
-filename: LearnJava.java
+filename: LearnJava-zh.java
contributors:
- ["Jake Prather", "http://github.com/JakeHP"]
translators:
diff --git a/zh-cn/javascript-cn.html.markdown b/zh-cn/javascript-cn.html.markdown
index 3b5cfa94..89fc256e 100755
--- a/zh-cn/javascript-cn.html.markdown
+++ b/zh-cn/javascript-cn.html.markdown
@@ -2,6 +2,7 @@
language: javascript
category: language
name: javascript
+filename: javascript-zh.js
contributors:
- ["Adam Brenecki", "http://adam.brenecki.id.au"]
translators:
diff --git a/zh-cn/php-cn.html.markdown b/zh-cn/php-cn.html.markdown
index 3b242ce1..c6ebb515 100755
--- a/zh-cn/php-cn.html.markdown
+++ b/zh-cn/php-cn.html.markdown
@@ -5,7 +5,7 @@ contributors:
- ["Trismegiste", "https://github.com/Trismegiste"]
translators:
- ["Chenbo Li", "http://binarythink.net"]
-filename: learnphp.php
+filename: learnphp-zh.php
lang: zh-cn
---
diff --git a/zh-cn/python-cn.html.markdown b/zh-cn/python-cn.html.markdown
index 259e4ed8..51efaac3 100755
--- a/zh-cn/python-cn.html.markdown
+++ b/zh-cn/python-cn.html.markdown
@@ -4,7 +4,7 @@ contributors:
- ["Louie Dinh", "http://ldinh.ca"]
translators:
- ["Chenbo Li", "http://binarythink.net"]
-filename: learnpython.py
+filename: learnpython-zh.py
lang: zh-cn
---
@@ -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]) # filled_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/racket-cn.html.markdown b/zh-cn/racket-cn.html.markdown
new file mode 100644
index 00000000..d43511ea
--- /dev/null
+++ b/zh-cn/racket-cn.html.markdown
@@ -0,0 +1,608 @@
+---
+
+language: racket
+lang: zh-cn
+filename: learnracket.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 f66d1a03..619e6e92 100644
--- a/zh-cn/ruby-cn.html.markdown
+++ b/zh-cn/ruby-cn.html.markdown
@@ -1,6 +1,7 @@
---
language: ruby
-filename: learnruby.rb
+filename: learnruby-zh.rb
+lang: zh-cn
contributors:
- ["David Underwood", "http://theflyingdeveloper.com"]
- ["Joel Walden", "http://joelwalden.net"]
@@ -93,7 +94,7 @@ x = y = 10 #=> 10
x #=> 10
y #=> 10
-# 按照惯例,用snake_case 作为变量名
+# 按照惯例,用 snake_case 作为变量名
snake_case = true
# 使用具有描述性的运算符
@@ -101,7 +102,8 @@ path_to_project_root = '/good/name/'
path = '/bad/name/'
# 符号(Symbols,也是对象)
-# 符号是不可变的,内部用整数类型表示的可重用的值。通常用它代替字符串来有效地表达有意义的值
+# 符号是不可变的,内部用整数类型表示的可重用的值。
+# 通常用它代替字符串来有效地表示有意义的值。
:pending.class #=> Symbol
diff --git a/zh-cn/scala-cn.html.markdown b/zh-cn/scala-cn.html.markdown
new file mode 100644
index 00000000..1ce41ac6
--- /dev/null
+++ b/zh-cn/scala-cn.html.markdown
@@ -0,0 +1,413 @@
+---
+language: Scala
+filename: learnscala-zh.scala
+contributors:
+ - ["George Petrov", "http://github.com/petrovg"]
+ - ["Dominic Bou-Samra", "http://dbousamra.github.com"]
+translators:
+ - ["Peiyong Lin", ""]
+filename: learn.scala
+lang: zh-cn
+---
+
+Scala - 一门可拓展性的语言
+
+```cpp
+
+/*
+ 自行设置:
+
+ 1) 下载 Scala - http://www.scala-lang.org/downloads
+ 2) unzip/untar 到你喜欢的地方,放在路径中的 bin 目录下
+ 3) 在终端输入 scala,开启 Scala 的 REPL,你会看到提示符:
+
+ scala>
+
+ 这就是所谓的 REPL,你现在可以在其中运行命令,让我们做到这一点:
+*/
+
+println(10) // 打印整数 10
+
+println("Boo!") // 打印字符串 "BOO!"
+
+
+// 一些基础
+
+// 打印并强制换行
+println("Hello world!")
+// 没有强制换行的打印
+print("Hello world")
+
+// 通过 var 或者 val 来声明变量
+// val 声明是不可变的,var 声明是可修改的。不可变性是好事。
+val x = 10 // x 现在是 10
+x = 20 // 错误: 对 val 声明的变量重新赋值
+var x = 10
+x = 20 // x 现在是 20
+
+// 单行注释开始于两个斜杠
+/*
+多行注释看起来像这样。
+*/
+
+// 布尔值
+true
+false
+
+// 布尔操作
+!true // false
+!false // true
+true == false // false
+10 > 5 // true
+
+// 数学运算像平常一样
+1 + 1 // 2
+2 - 1 // 1
+5 * 3 // 15
+6 / 2 // 3
+
+
+// 在 REPL 计算一个命令会返回给你结果的类型和值
+
+1 + 7
+
+/* 上行的结果是:
+
+ scala> 1 + 7
+ res29: Int = 8
+
+ 这意味着计算 1 + 7 的结果是一个 Int 类型的对象,其值为 8
+
+ 1+7 的结果是一样的
+*/
+
+
+// 包括函数在内,每一个事物都是对象。在 REPL 中输入:
+
+7 // 结果 res30: Int = 7 (res30 是一个生成的结果的 var 命名)
+
+// 下一行给你一个接收一个 Int 类型并返回该数的平方的函数
+(x:Int) => x * x
+
+// 你可以分配给函数一个标识符,像这样:
+val sq = (x:Int) => x * x
+
+/* 上面的例子说明
+
+ sq: Int => Int = <function1>
+
+ 意味着这次我们给予了 sq 这样一个显式的名字给一个接受一个 Int 类型值并返回 一个 Int 类型值的函数
+
+ sq 可以像下面那样被执行:
+*/
+
+sq(10) // 返回给你:res33: Int = 100.
+
+// Scala 允许方法和函数返回或者接受其它的函数或者方法作为参数。
+
+val add10: Int => Int = _ + 10 // 一个接受一个 Int 类型参数并返回一个 Int 类型值的函数
+List(1, 2, 3) map add10 // List(11, 12, 13) - add10 被应用到每一个元素
+
+// 匿名函数可以被使用来代替有命名的函数:
+List(1, 2, 3) map (x => x + 10)
+
+// 下划线标志,如果匿名函数只有一个参数可以被使用来表示该参数变量
+List(1, 2, 3) map (_ + 10)
+
+// 如果你所应用的匿名块和匿名函数都接受一个参数,那么你甚至可以省略下划线
+List("Dom", "Bob", "Natalia") foreach println
+
+
+
+// 数据结构
+
+val a = Array(1, 2, 3, 5, 8, 13)
+a(0)
+a(3)
+a(21) // 这会抛出一个异常
+
+val m = Map("fork" -> "tenedor", "spoon" -> "cuchara", "knife" -> "cuchillo")
+m("fork")
+m("spoon")
+m("bottle") // 这会抛出一个异常
+
+val safeM = m.withDefaultValue("no lo se")
+safeM("bottle")
+
+val s = Set(1, 3, 7)
+s(0)
+s(1)
+
+/* 查看 map 的文档
+ * 点击[这里](http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.Map)
+ * 确保你可以读它
+ */
+
+
+// 元组
+
+(1, 2)
+
+(4, 3, 2)
+
+(1, 2, "three")
+
+(a, 2, "three")
+
+// 为什么有这个?
+
+val divideInts = (x:Int, y:Int) => (x / y, x % y)
+
+divideInts(10,3) // 函数 divideInts 返回你结果和余数
+
+// 要读取元组的元素,使用 _._n,n是从1开始的元素索引
+
+val d = divideInts(10,3)
+
+d._1
+
+d._2
+
+
+
+// 选择器
+
+s.map(sq)
+
+val sSquared = s. map(sq)
+
+sSquared.filter(_ < 10)
+
+sSquared.reduce (_+_)
+
+// filter 函数接受一个预测(一个函数,形式为 A -> Boolean) 并选择出所有的元素满足这个预测
+
+List(1, 2, 3) filter (_ > 2) // List(3)
+List(
+ Person(name = "Dom", age = 23),
+ Person(name = "Bob", age = 30)
+).filter(_.age > 25) // List(Person("Bob", 30))
+
+
+// Scala 的 foreach 方法定义在特定的接受一个类型的集合上
+// 返回 Unit(一个 void 方法)
+aListOfNumbers foreach (x => println(x))
+aListOfNumbers foreach println
+
+
+
+
+// For 包含
+
+for { n <- s } yield sq(n)
+
+val nSquared2 = for { n <- s } yield sq(n)
+
+for { n <- nSquared2 if n < 10 } yield n
+
+for { n <- s; nSquared = n * n if nSquared < 10} yield nSquared
+
+/* 注意:这些不是 for 循环. 一个 for 循环的语义是 '重复'('repeat'),
+ 然而,一个 for-包含 定义了一个两个数据结合间的关系 */
+
+
+
+// 循环和迭代
+
+1 to 5
+val r = 1 to 5
+r.foreach( println )
+
+r foreach println
+// 注意:Scala 是相当宽容的当它遇到点和括号 - 分别地学习这些规则。
+// 这帮助你编写读起来像英语的 DSLs 和 APIs
+
+(5 to 1 by -1) foreach ( println )
+
+// while 循环
+var i = 0
+while (i < 10) { println("i " + i); i+=1 }
+
+while (i < 10) { println("i " + i); i+=1 } // 发生了什么?为什么?
+
+i // 展示 i 的值。注意到 while 是一个传统意义上的循环
+ // 它顺序地执行并且改变循环变量的值。while 非常快,比 Java // 循环快,
+ // 但是在其上使用选择器和包含更容易理解和并行。
+
+// do while 循环
+do {
+ println("x is still less then 10");
+ x += 1
+} while (x < 10)
+
+// 在 Scala中,尾递归是一种惯用的执行循环的方式。
+// 递归函数需要显示的返回类型,编译器不能推断出类型。
+// 这里它是 Unit。
+def showNumbersInRange(a:Int, b:Int):Unit = {
+ print(a)
+ if (a < b)
+ showNumbersInRange(a + 1, b)
+}
+
+
+
+// 条件语句
+
+val x = 10
+
+if (x == 1) println("yeah")
+if (x == 10) println("yeah")
+if (x == 11) println("yeah")
+if (x == 11) println ("yeah") else println("nay")
+
+println(if (x == 10) "yeah" else "nope")
+val text = if (x == 10) "yeah" else "nope"
+
+var i = 0
+while (i < 10) { println("i " + i); i+=1 }
+
+
+
+// 面向对象特性
+
+// 类名是 Dog
+class Dog {
+ //bark 方法,返回字符串
+ def bark: String = {
+ // the body of the method
+ "Woof, woof!"
+ }
+}
+
+// 类可以包含几乎其它的构造,包括其它的类,
+// 函数,方法,对象,case 类,特性等等。
+
+
+
+// Case 类
+
+case class Person(name:String, phoneNumber:String)
+
+Person("George", "1234") == Person("Kate", "1236")
+
+
+
+
+// 模式匹配
+
+val me = Person("George", "1234")
+
+me match { case Person(name, number) => {
+ "We matched someone : " + name + ", phone : " + number }}
+
+me match { case Person(name, number) => "Match : " + name; case _ => "Hm..." }
+
+me match { case Person("George", number) => "Match"; case _ => "Hm..." }
+
+me match { case Person("Kate", number) => "Match"; case _ => "Hm..." }
+
+me match { case Person("Kate", _) => "Girl"; case Person("George", _) => "Boy" }
+
+val kate = Person("Kate", "1234")
+
+kate match { case Person("Kate", _) => "Girl"; case Person("George", _) => "Boy" }
+
+
+
+// 正则表达式
+
+val email = "(.*)@(.*)".r // 在字符串上调用 r 会使它变成一个正则表达式
+
+val email(user, domain) = "henry@zkpr.com"
+
+"mrbean@pyahoo.com" match {
+ case email(name, domain) => "I know your name, " + name
+}
+
+
+
+// 字符串
+
+"Scala 字符串被双引号包围" //
+'a' // Scala 字符
+'单引号的字符串不存在' // 错误
+"字符串拥有通常的 Java 方法定义在其上".length
+"字符串也有额外的 Scala 方法".reverse
+
+// 参见: scala.collection.immutable.StringOps
+
+println("ABCDEF".length)
+println("ABCDEF".substring(2, 6))
+println("ABCDEF".replace("C", "3"))
+
+val n = 45
+println(s"We have $n apples")
+
+val a = Array(11, 9, 6)
+println(s"My second daughter is ${a(2-1)} years old")
+
+// 一些字符需要被转义,举例来说,字符串中的双引号:
+val a = "They stood outside the \"Rose and Crown\""
+
+// 三个双引号使得字符串可以跨行并且可以包含引号(无需转义)
+
+val html = """<form id="daform">
+ <p>Press belo', Joe</p>
+ | <input type="submit">
+ </form>"""
+
+
+
+// 应用结果和组织
+
+// import
+import scala.collection.immutable.List
+
+// Import 所有的子包
+import scala.collection.immutable._
+
+// 在一条语句中 Import 多个类
+import scala.collection.immutable.{List, Map}
+
+// 使用 '=>' 来重命名一个 import
+import scala.collection.immutable{ List => ImmutableList }
+
+// import 除了一些类的其它所有的类。下面的例子除去了 Map 类和 Set 类:
+import scala.collection.immutable.{Map => _, Set => _, _}
+
+// 在 scala 源文件中,你的程序入口点使用一个拥有单一方法 main 的对象来定义:
+
+object Application {
+ def main(args: Array[String]): Unit = {
+ // stuff goes here.
+ }
+}
+
+// 文件可以包含多个类和对象。由 scalac 来编译
+
+
+
+
+// 输入和输出
+
+// 一行一行读取文件
+import scala.io.Source
+for(line <- Source.fromPath("myfile.txt").getLines())
+ println(line)
+
+// 使用 Java 的 PrintWriter 来写文件
+
+
+```
+
+## 更多的资源
+
+[为没耐心的人准备的 Scala](http://horstmann.com/scala/)
+
+[Twitter Scala school](http://twitter.github.io/scala_school/)
+
+[The Scala documentation](http://www.scala-lang.org/documentation/)
+
+[在浏览器尝试 Scala](http://scalatutorials.com/tour/)
+
+加入 [Scala 用户组](https://groups.google.com/forum/#!forum/scala-user)