summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--clojure.html.markdown2
-rw-r--r--common-lisp.html.markdown605
-rw-r--r--csharp.html.markdown549
-rw-r--r--es-es/coffeescript-es.html.markdown56
-rw-r--r--go.html.markdown301
-rw-r--r--haskell.html.markdown2
-rw-r--r--ko-kr/lua-kr.html.markdown3
-rw-r--r--objective-c.html.markdown310
-rw-r--r--php.html.markdown5
-rw-r--r--pt-br/elisp-pt.html.markdown359
-rw-r--r--pt-br/python-pt.html.markdown2
-rw-r--r--ruby-ecosystem.html.markdown6
-rw-r--r--ruby.html.markdown38
-rwxr-xr-xzh-cn/python-cn.html.markdown277
-rw-r--r--zh-cn/racket-cn.html.markdown608
-rw-r--r--zh-cn/ruby-cn.html.markdown5
16 files changed, 2969 insertions, 159 deletions
diff --git a/clojure.html.markdown b/clojure.html.markdown
index 6baae0ce..a502a95c 100644
--- a/clojure.html.markdown
+++ b/clojure.html.markdown
@@ -365,7 +365,7 @@ my-atom ;=> Atom<#...> (Returns the Atom object)
### Further Reading
-This is far from exhaustive, but hopefully it's enought o get you on your feet.
+This is far from exhaustive, but hopefully it's enough to get you on your feet.
Clojure.org has lots of articles:
[http://clojure.org/](http://clojure.org/)
diff --git a/common-lisp.html.markdown b/common-lisp.html.markdown
new file mode 100644
index 00000000..a917304c
--- /dev/null
+++ b/common-lisp.html.markdown
@@ -0,0 +1,605 @@
+---
+
+language: "Common Lisp"
+filename: commonlisp.lisp
+contributors:
+ - ["Paul Nathan", "https://github.com/pnathan"]
+---
+
+ANSI Common Lisp is a general purpose, multi-paradigm programming
+language suited for a wide variety of industry applications. It is
+frequently referred to a programmable programming language.
+
+The classic starting point is [Practical Common Lisp and freely available.](http://www.gigamonkeys.com/book/)
+
+Another popular and recent book is
+[Land of Lisp](http://landoflisp.com/).
+
+
+
+```scheme
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;; 0. Syntax
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;;; General form.
+
+;; Lisp has two fundamental pieces of syntax: the ATOM and the
+;; S-expression. Typically, grouped S-expressions are called `forms`.
+
+10 ; an atom; it evaluates to itself
+
+:THING ;Another atom; evaluating to the symbol :thing.
+
+t ; another atom, denoting true.
+
+(+ 1 2 3 4) ; an s-expression
+
+'(4 :foo t) ;another one
+
+
+;;; Comments
+
+;; Single line comments start with a semicolon; use two for normal
+;; comments, three for section comments, and four for file-level
+;; comments.
+
+#| Block comments
+ can span multiple lines and...
+ #|
+ they can be nested!
+ |#
+|#
+
+;;; Environment.
+
+;; A variety of implementations exist; most are
+;; standard-conformant. CLISP is a good starting one.
+
+;; Libraries are managed through Quicklisp.org's Quicklisp system.
+
+;; Common Lisp is usually developed with a text editor and a REPL
+;; (Read Evaluate Print Loop) running at the same time. The REPL
+;; allows for interactive exploration of the program as it is "live"
+;; in the system.
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;; 1. Primitive Datatypes and Operators
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;;; Symbols
+
+'foo ; => FOO Notice that the symbol is upper-cased automatically.
+
+;; Intern manually creates a symbol from a string.
+
+(intern "AAAA") ; => AAAA
+
+(intern "aaa") ; => |aaa|
+
+;;; Numbers
+9999999999999999999999 ; integers
+#b111 ; binary => 7
+#o111 ; octal => 73
+#x111 ; hexadecimal => 273
+3.14159s0 ; single
+3.14159d0 ; double
+1/2 ; ratios
+#C(1 2) ; complex numbers
+
+
+;; Function application is written (f x y z ...)
+;; where f is a function and x, y, z, ... are operands
+;; If you want to create a literal list of data, use ' to stop it from
+;; being evaluated - literally, "quote" the data.
+'(+ 1 2) ; => (+ 1 2)
+;; You can also call a function manually:
+(funcall #'+ 1 2 3) ; => 6
+;; Some arithmetic operations
+(+ 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)
+
+ ;;; Booleans
+t ; for true (any not-nil value is true)
+nil ; for false - and the empty list
+(not nil) ; => t
+(and 0 t) ; => t
+(or 0 nil) ; => 0
+
+ ;;; Characters
+#\A ; => #\A
+#\λ ; => #\GREEK_SMALL_LETTER_LAMDA
+#\u03BB ; => #\GREEK_SMALL_LETTER_LAMDA
+
+;;; Strings are fixed-length arrays of characters.
+"Hello, world!"
+"Benjamin \"Bugsy\" Siegel" ; backslash is an escaping character
+
+;; Strings can be concatenated too!
+(concatenate 'string "Hello " "world!") ; => "Hello world!"
+
+;; A string can be treated like a sequence of characters
+(elt "Apple" 0) ; => #\A
+
+;; format can be used to format strings:
+(format nil "~a can be ~a" "strings" "formatted")
+
+;; Printing is pretty easy; ~% is the format specifier for newline.
+(format t "Common Lisp is groovy. Dude.~%")
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 2. Variables
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; You can create a global (dynamically scoped) using defparameter
+;; a variable name can use any character except: ()[]{}",'`;#|\
+
+;; Dynamically scoped variables should have earmuffs in their name!
+
+(defparameter *some-var* 5)
+*some-var* ; => 5
+
+;; You can also use unicode characters.
+(defparameter *AΛB* nil)
+
+
+;; Accessing a previously unbound variable is an
+;; undefined behavior (but possible). Don't do it.
+
+
+;; Local binding: `me` is bound to "dance with you" only within the
+;; (let ...). Let always returns the value of the last `form` in the
+;; let form.
+
+(let ((me "dance with you"))
+ me)
+;; => "dance with you"
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 3. Structs and Collections
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;; Structs
+(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, and dog-name are all created by defstruct!
+
+;;; Pairs
+;; `cons' constructs pairs, `car' and `cdr' extract the first
+;; and second elements
+(cons 'SUBJECT 'VERB) ; => '(SUBJECT . VERB)
+(car (cons 'SUBJECT 'VERB)) ; => SUBJECT
+(cdr (cons 'SUBJECT 'VERB)) ; => VERB
+
+;;; Lists
+
+;; Lists are linked-list data structures, made of `cons' pairs and end
+;; with a `nil' (or '()) to mark the end of the list
+(cons 1 (cons 2 (cons 3 nil))) ; => '(1 2 3)
+;; `list' is a convenience variadic constructor for lists
+(list 1 2 3) ; => '(1 2 3)
+;; and a quote can also be used for a literal list value
+'(1 2 3) ; => '(1 2 3)
+
+;; Can still use `cons' to add an item to the beginning of a list
+(cons 4 '(1 2 3)) ; => '(4 1 2 3)
+
+;; Use `append' to - surprisingly - append lists together
+(append '(1 2) '(3 4)) ; => '(1 2 3 4)
+
+;; Or use concatenate -
+
+(concatenate
+
+;; Lists are a very central type, so there is a wide variety of functionality for
+;; them, a few examples:
+(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)
+
+
+;;; Vectors
+
+;; Vectors are fixed-length arrays
+#(1 2 3) ; => #(1 2 3)
+
+;; Use concatenate to add vectors together
+(concatenate 'vector #(1 2 3) #(4 5 6)) ; => #(1 2 3 4 5 6)
+
+;;; Arrays
+
+;; Both vectors and strings are special-cases of arrays.
+
+;; 2D arrays
+
+(make-array (list 2 2))
+
+;; (make-array '(2 2)) works as well.
+
+; => #2A((0 0) (0 0))
+
+(make-array (list 2 2 2))
+
+; => #3A(((0 0) (0 0)) ((0 0) (0 0)))
+
+;; Caution- the default initial values are
+;; implementation-defined. Here's how to define them:
+
+(make-array '(2) :initial-element 'unset)
+
+; => #(UNSET UNSET)
+
+;; And, to access the element at 1,1,1 -
+(aref (make-array (list 2 2 2)) 1 1 1)
+
+; => 0
+
+;;; Naively, sets are just lists:
+
+(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)
+
+;; But you'll want to use a better data structure than a linked list
+;; for performant work!
+
+;;; Dictionaries are implemented as hash tables.
+
+;; Create a hash table
+(defparameter *m* (make-hash-table))
+
+;; set a value
+(setf (gethash 'a *m*) 1)
+
+;; Retrieve a value
+(gethash 'a *m*) ; => 1, t
+
+;; Detail - Common Lisp has multiple return values possible. gethash
+;; returns t in the second value if anything was found, and nil if
+;; not.
+
+;; Retrieving a non-present value returns nil
+ (gethash *m* 'd) ;=> nil, nil
+
+;; You can provide a default value for missing keys
+(gethash *m* 'd :not-found) ; => :NOT-FOUND
+
+;; Let's handle the multiple return values here in code.
+
+(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. Functions
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;; Use `lambda' to create anonymous functions.
+;; A function always returns the value of its last expression.
+;; The exact printable representation of a function will vary...
+
+(lambda () "Hello World") ; => #<FUNCTION (LAMBDA ()) {1004E7818B}>
+
+;; Use funcall to call lambda functions
+(funcall (lambda () "Hello World")) ; => "Hello World"
+
+;; Or Apply
+(apply (lambda () "Hello World") nil) ; => "Hello World"
+
+;; De-anonymize the function
+(defun hello-world ()
+ "Hello World")
+(hello-world) ; => "Hello World"
+
+;; The () in the above is the list of arguments for the function
+(defun hello (name)
+ (format nil "Hello, ~a " name))
+
+(hello "Steve") ; => "Hello, Steve"
+
+;; Functions can have optional arguments; they default to 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
+
+;; And the defaults can be set...
+(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
+
+
+;; And of course, keywords are allowed as well... usually more
+;; flexible than &optional.
+
+(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. Equality
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;; Common Lisp has a sophisticated equality system. A couple are covered yere.
+
+;; for numbers use `='
+(= 3 3.0) ; => t
+(= 2 1) ; => nil
+
+;; for object identity (approximately) use `eql`
+(eql 3 3) ; => t
+(eql 3 3.0) ; => nil
+(eql (list 3) (list 3)) ; => nil
+
+;; for lists, strings, and bit-vectors use `equal'
+(equal (list 'a 'b) (list 'a 'b)) ; => t
+(equal (list 'a 'b) (list 'b 'a)) ; => nil
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 5. Control Flow
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;;; Conditionals
+
+(if t ; test expression
+ "this is true" ; then expression
+ "this is false") ; else expression
+; => "this is true"
+
+;; In conditionals, all non-nil values are treated as true
+(member 'Groucho '(Harpo Groucho Zeppo)) ; => '(GROUCHO ZEPPO)
+(if (member 'Groucho '(Harpo Groucho Zeppo))
+ 'yep
+ 'nope)
+; => 'YEP
+
+;; `cond' chains a series of tests to select a result
+(cond ((> 2 2) (error "wrong!"))
+ ((< 2 2) (error "wrong again!"))
+ (t 'ok)) ; => 'OK
+
+;; Typecase switches on the type of the value
+(typecase 1
+ (string :string)
+ (integer :int))
+
+; => :int
+
+;;; Iteration
+
+;; Of course recursion is supported:
+
+(defun walker (n)
+ (if (zerop n)
+ :walked
+ (walker (1- n))))
+
+(walker) ; => :walked
+
+;; Most of the time, we use DOLIST or 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. Mutation
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;; Use `setf' to assign a new value to an existing variable. This was
+;; demonstrated earlier in the hash table example.
+
+(let ((variable 10))
+ (setf variable 2))
+ ; => 2
+
+
+;; Good Lisp style is to minimize destructive functions and to avoid
+;; mutation when reasonable.
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 7. Classes and Objects
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;; No more Animal classes, let's have Human-Powered Mechanical
+;; Conveyances.
+
+(defclass human-powered-conveyance ()
+ ((velocity
+ :accessor velocity
+ :initarg :velocity)
+ (average-efficiency
+ :accessor average-efficiency)
+ :initarg :average-efficiency)
+ (:documentation "A human powered conveyance"))
+
+;; defclass, followed by name, followed by the superclass list,
+;; followed by slot list, followed by optional qualities such as
+;; :documentation.
+
+;; When no superclass list is set, the empty list defaults to the
+;; standard-object class. This *can* be changed, but not until you
+;; know what you're doing. Look up the Art of the Metaobject Protocol
+;; for more information.
+
+(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)))
+
+
+;; Calling DESCRIBE on the human-powered-conveyance class in the REPL gives:
+
+(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.
+(defparameter *foo#\u03BBooo* nil) ; Direct slots:
+; VELOCITY
+; Readers: VELOCITY
+; Writers: (SETF VELOCITY)
+; AVERAGE-EFFICIENCY
+; Readers: AVERAGE-EFFICIENCY
+; Writers: (SETF AVERAGE-EFFICIENCY)
+
+;; Note the reflective behavior available to you! Common Lisp is
+;; designed to be an interactive system
+
+;; To define a method, let's find out what our circumference of the
+;; bike wheel turns out to be using the equation: C = d * pi
+
+(defmethod circumference ((object bicycle))
+ (* pi (wheel-size object)))
+
+;; pi is defined in Lisp already for us!
+
+;; Let's suppose we find out that the efficiency value of the number
+;; of rowers in a canoe is roughly logarithmic. This should probably be set
+;; in the constructor/initializer.
+
+;; Here's how to initialize your instance after Common Lisp gets done
+;; constructing it:
+
+(defmethod initialize-instance :after ((object canoe) &rest args)
+ (setf (average-efficiency object) (log (1+ (number-of-rowers object)))))
+
+;; Then to construct an instance and check the average efficiency...
+
+(average-efficiency (make-instance 'canoe :number-of-rowers 15))
+; => 2.7725887
+
+
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 8. Macros
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;; Macros let you extend the syntax of the language
+
+;; Common Lisp doesn't come with a WHILE loop- let's add one.
+;; If we obey our assembler instincts, we wind up with:
+
+(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)))
+
+;; Let's look at the high-level version of this:
+
+
+(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)))
+
+;; However, with a modern compiler, this is not required; the LOOP
+;; form compiles equally well and is easier to read.
+
+;; Note that ``` is used, as well as `,` and `@`. ``` is a quote-type operator
+;; known as quasiquote; it allows the use of `,` . `,` allows "unquoting"
+;; variables. @ interpolates lists.
+
+;; Gensym creates a unique symbol guaranteed to not exist elsewhere in
+;; the system. This is because macros are expanded at compile time and
+;; variables declared in the macro can collide with variables used in
+;; regular code.
+
+;; See Practical Common Lisp for more information on macros.
+```
+
+
+## Further Reading
+
+[Keep moving on to the Practical Common Lisp book.](http://www.gigamonkeys.com/book/)
+
+
+## Credits.
+
+Lots of thanks to the Scheme people for rolling up a great starting
+point which could be easily moved to Common Lisp.
+
+- [Paul Khoung](https://github.com/pkhuong) for some great reviewing.
diff --git a/csharp.html.markdown b/csharp.html.markdown
new file mode 100644
index 00000000..c254b5a9
--- /dev/null
+++ b/csharp.html.markdown
@@ -0,0 +1,549 @@
+---
+
+language: c#
+contributors:
+ - ["Irfan Charania", "https://github.com/irfancharania"]
+filename: LearnCSharp.cs
+
+---
+
+C# is an elegant and type-safe object-oriented language that enables developers to build a variety of secure and robust applications that run on the .NET Framework.
+
+[Read more here.](http://msdn.microsoft.com/en-us/library/vstudio/z1zx9t92.aspx)
+
+```c#
+// Single-line comments start with //
+/*
+Multi-line comments look like this
+*/
+/// <summary>
+/// This is an XML documentation comment
+/// </summary>
+
+// Specify namespaces application will be using
+using System;
+using System.Collections.Generic;
+
+
+// defines scope to organize code into "packages"
+namespace Learning
+{
+ // Each .cs file should at least contain a class with the same name as the file
+ // you're allowed to do otherwise, but shouldn't for sanity.
+ public class LearnCSharp
+ {
+ // A console application must have a main method as an entry point
+ public static void Main(string[] args)
+ {
+ // Use Console.WriteLine to print lines
+ Console.WriteLine("Hello World");
+ Console.WriteLine(
+ "Integer: " + 10 +
+ " Double: " + 3.14 +
+ " Boolean: " + true);
+
+ // To print without a new line, use Console.Write
+ Console.Write("Hello ");
+ Console.Write("World");
+
+
+ ///////////////////////////////////////////////////
+ // Types & Variables
+ //
+ // Declare a variable using <type> <name>
+ ///////////////////////////////////////////////////
+
+ // Sbyte - Signed 8-bit integer
+ // (-128 <= sbyte <= 127)
+ sbyte fooSbyte = 100;
+
+ // Byte - Unsigned 8-bit integer
+ // (0 <= byte <= 255)
+ byte fooByte = 100;
+
+ // Short - Signed 16-bit integer
+ // (-32,768 <= short <= 32,767)
+ short fooShort = 10000;
+
+ // Ushort - Unsigned 16-bit integer
+ // (0 <= ushort <= 65,535)
+ ushort fooUshort = 10000;
+
+ // Integer - Signed 32-bit integer
+ // (-2,147,483,648 <= int <= 2,147,483,647)
+ int fooInt = 1;
+
+ // Uinteger - Unsigned 32-bit integer
+ // (0 <= uint <= 4,294,967,295)
+ uint fooUint = 1;
+
+ // Long - Signed 64-bit integer
+ // (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807)
+ long fooLong = 100000L;
+ // L is used to denote that this variable value is of type long or ulong
+ // anything without is treated as int or uint depending on size.
+
+ // Ulong - Unsigned 64-bit integer
+ // (0 <= ulong <= 18,446,744,073,709,551,615)
+ ulong fooUlong = 100000L;
+
+ // Float - Single-precision 32-bit IEEE 754 Floating Point
+ // Precision: 7 digits
+ float fooFloat = 234.5f;
+ // f is used to denote that this variable value is of type float;
+ // otherwise it is treated as double.
+
+ // Double - Double-precision 64-bit IEEE 754 Floating Point
+ // Precision: 15-16 digits
+ double fooDouble = 123.4;
+
+ // Bool - true & false
+ bool fooBoolean = true;
+ bool barBoolean = false;
+
+ // Char - A single 16-bit Unicode character
+ char fooChar = 'A';
+
+ // Strings
+ string fooString = "My string is here!";
+ Console.WriteLine(fooString);
+
+ // formatting
+ string fooFs = string.Format("Check Check, {0} {1}, {0} {1:0.0}", 1, 2);
+ Console.WriteLine(fooFormattedString);
+
+ // formatting dates
+ DateTime fooDate = DateTime.Now;
+ Console.WriteLine(fooDate.ToString("hh:mm, dd MMM yyyy"));
+
+ // \n is an escaped character that starts a new line
+ string barString = "Printing on a new line?\nNo Problem!";
+ Console.WriteLine(barString);
+
+ // it can be written prettier by using the @ symbol
+ string bazString = @"Here's some stuff
+ on a new line!";
+ Console.WriteLine(bazString);
+
+ // quotes need to be escaped
+ // use \" normally
+ string quotedString = "some \"quoted\" stuff";
+ Console.WriteLine(quotedString);
+
+ // use "" when strings start with @
+ string quotedString2 = @"some MORE ""quoted"" stuff";
+ Console.WriteLine(quotedString2);
+
+ // Use const or read-only to make a variable immutable
+ // const values are calculated at compile time
+ const int HOURS_I_WORK_PER_WEEK = 9001;
+
+ // Nullable types
+ // any type can be made nullable by suffixing a ?
+ // <type>? <var name> = <value>
+ int? nullable = null;
+ Console.WriteLine("Nullable variable: " + nullable);
+
+ // ?? is syntactic sugar for specifying default value
+ // in case variable is null
+ int notNullable = nullable ?? 0;
+ Console.WriteLine("Not nullable variable: " + notNullable);
+
+ // Var - compiler will choose the most appropriate type based on value
+ var fooImplicit = true;
+
+ ///////////////////////////////////////////////////
+ // Data Structures
+ ///////////////////////////////////////////////////
+ Console.WriteLine("\n->Data Structures");
+
+ // Arrays
+ // The array size must be decided upon declaration
+ // The format for declaring an array is follows:
+ // <datatype>[] <var name> = new <datatype>[<array size>];
+ int[] intArray = new int[10];
+ string[] stringArray = new string[1];
+ bool[] boolArray = new bool[100];
+
+ // Another way to declare & initialize an array
+ int[] y = { 9000, 1000, 1337 };
+
+ // Indexing an array - Accessing an element
+ Console.WriteLine("intArray @ 0: " + intArray[0]);
+
+ // Arrays are zero-indexed and mutable.
+ intArray[1] = 1;
+ Console.WriteLine("intArray @ 1: " + intArray[1]); // => 1
+
+ // Lists
+ // Lists are used more frequently than arrays as they are more flexible
+ // The format for declaring a list is follows:
+ // List<datatype> <var name> = new List<datatype>();
+ List<int> intList = new List<int>();
+ List<string> stringList = new List<string>();
+
+ // Another way to declare & initialize a list
+ List<int> z = new List<int> { 9000, 1000, 1337 };
+
+ // Indexing a list - Accessing an element
+ // Lists are zero-indexed and mutable.
+ Console.WriteLine("z @ 0: " + z[2]);
+
+ // Lists don't default to a value;
+ // A value must be added before accessing the index
+ intList.Add(1);
+ Console.WriteLine("intList @ 0: " + intList[0]);
+
+
+ // Others data structures to check out:
+ //
+ // Stack/Queue
+ // Dictionary
+ // Read-only Collections
+ // Tuple (.Net 4+)
+
+
+ ///////////////////////////////////////
+ // Operators
+ ///////////////////////////////////////
+ Console.WriteLine("\n->Operators");
+
+ int i1 = 1, i2 = 2; // Shorthand for multiple declarations
+
+ // Arithmetic is straightforward
+ Console.WriteLine("1+2 = " + (i1 + i2)); // => 3
+ Console.WriteLine("2-1 = " + (i2 - i1)); // => 1
+ Console.WriteLine("2*1 = " + (i2 * i1)); // => 2
+ Console.WriteLine("1/2 = " + (i1 / i2)); // => 0 (0.5 truncated down)
+
+ // Modulo
+ Console.WriteLine("11%3 = " + (11 % 3)); // => 2
+
+ // Comparison operators
+ 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
+
+ // Bitwise operators!
+ /*
+ ~ Unary bitwise complement
+ << Signed left shift
+ >> Signed right shift
+ >>> Unsigned right shift
+ & Bitwise AND
+ ^ Bitwise exclusive OR
+ | Bitwise inclusive OR
+ */
+
+ // Incrementations
+ int i = 0;
+ Console.WriteLine("\n->Inc/Dec-rementation");
+ Console.WriteLine(i++); //i = 1. Post-Incrementation
+ Console.WriteLine(++i); //i = 2. Pre-Incrementation
+ Console.WriteLine(i--); //i = 1. Post-Decrementation
+ Console.WriteLine(--i); //i = 0. Pre-Decrementation
+
+
+ ///////////////////////////////////////
+ // Control Structures
+ ///////////////////////////////////////
+ Console.WriteLine("\n->Control Structures");
+
+ // If statements are c-like
+ 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");
+ }
+
+ // Ternary operators
+ // A simple if/else can be written as follows
+ // <condition> ? <true> : <false>
+ string isTrue = (true) ? "True" : "False";
+ Console.WriteLine("Ternary demo: " + isTrue);
+
+
+ // While loop
+ int fooWhile = 0;
+ while (fooWhile < 100)
+ {
+ //Console.WriteLine(fooWhile);
+ //Increment the counter
+ //Iterated 99 times, fooWhile 0->99
+ fooWhile++;
+ }
+ Console.WriteLine("fooWhile Value: " + fooWhile);
+
+ // Do While Loop
+ int fooDoWhile = 0;
+ do
+ {
+ //Console.WriteLine(fooDoWhile);
+ //Increment the counter
+ //Iterated 99 times, fooDoWhile 0->99
+ fooDoWhile++;
+ } while (fooDoWhile < 100);
+ Console.WriteLine("fooDoWhile Value: " + fooDoWhile);
+
+ // For Loop
+ int fooFor;
+ //for loop structure => for(<start_statement>; <conditional>; <step>)
+ for (fooFor = 0; fooFor < 10; fooFor++)
+ {
+ //Console.WriteLine(fooFor);
+ //Iterated 10 times, fooFor 0->9
+ }
+ Console.WriteLine("fooFor Value: " + fooFor);
+
+ // Switch Case
+ // A switch works with the byte, short, char, and int data types.
+ // It also works with enumerated types (discussed in Enum Types),
+ // the String class, and a few special classes that wrap
+ // primitive types: Character, Byte, Short, and Integer.
+ int month = 3;
+ string monthString;
+ switch (month)
+ {
+ case 1:
+ monthString = "January";
+ break;
+ case 2:
+ monthString = "February";
+ break;
+ case 3:
+ monthString = "March";
+ break;
+ default:
+ monthString = "Some other month";
+ break;
+ }
+ Console.WriteLine("Switch Case Result: " + monthString);
+
+
+ ///////////////////////////////////////
+ // Converting Data Types And Typcasting
+ ///////////////////////////////////////
+
+ // Converting data
+
+ // Convert String To Integer
+ // this will throw an Exception on failure
+ int.Parse("123");//returns an integer version of "123"
+
+ // try parse will default to type default on failure
+ // in this case: 0
+ int tryInt;
+ int.TryParse("123", out tryInt);
+
+ // Convert Integer To String
+ // Convert class has a number of methods to facilitate conversions
+ Convert.ToString(123);
+
+ ///////////////////////////////////////
+ // Classes And Functions
+ ///////////////////////////////////////
+
+ Console.WriteLine("\n->Classes & Functions");
+
+ // (definition of the Bicycle class follows)
+
+ // Use new to instantiate a class
+ Bicycle trek = new Bicycle();
+
+ // Call object methods
+ trek.speedUp(3); // You should always use setter and getter methods
+ trek.setCadence(100);
+
+ // ToString is a convention to display the value of this Object.
+ Console.WriteLine("trek info: " + trek.ToString());
+
+ // Instantiate another new Bicycle
+ Bicycle octo = new Bicycle(5, 10);
+ Console.WriteLine("octo info: " + octo.ToString());
+
+ // Instantiate a new Penny Farthing
+ PennyFarthing funbike = new PennyFarthing(1, 10);
+ Console.WriteLine("funbike info: " + funbike.ToString());
+
+ Console.Read();
+ } // End main method
+
+
+ } // End LearnCSharp class
+
+ // You can include other classes in a .cs file
+
+
+ // Class Declaration Syntax:
+ // <public/private/protected> class <class name>{
+ // //data fields, constructors, functions all inside.
+ // //functions are called as methods in Java.
+ // }
+
+ public class Bicycle
+ {
+ // Bicycle's Fields/Variables
+ public int cadence; // Public: Can be accessed from anywhere
+ private int _speed; // Private: Only accessible from within the class
+ protected int gear; // Protected: Accessible from the class and subclasses
+ internal int wheels; // Internal: Accessible from within the assembly
+ string name; // default: Only accessible from within this class
+
+ // readonly values are set at run time
+ // they can only be assigned upon declaration or in a constructor
+ readonly bool hasCardsInSpokes = false; // read-only private
+
+ // Constructors are a way of creating classes
+ // This is a default constructor
+ public Bicycle()
+ {
+ gear = 1;
+ cadence = 50;
+ _speed = 5;
+ name = "Bontrager";
+ }
+
+ // This is a specified constructor (it contains arguments)
+ public Bicycle(int startCadence, int startSpeed, int startGear,
+ string name, bool hasCardsInSpokes)
+ {
+ this.gear = startGear;
+ this.cadence = startCadence;
+ this._speed = startSpeed;
+ this.name = name;
+ this.hasCardsInSpokes = hasCardsInSpokes;
+ }
+
+ // Constructors can be chained
+ public Bicycle(int startCadence, int startSpeed) :
+ this(startCadence, startSpeed, 0, "big wheels", true)
+ {
+ }
+
+ // Function Syntax:
+ // <public/private/protected> <return type> <function name>(<args>)
+
+ // classes can implement getters and setters for their fields
+ // or they can implement properties
+
+ // Method declaration syntax:
+ // <scope> <return type> <method name>(<args>)
+ public int getCadence()
+ {
+ return cadence;
+ }
+
+ // void methods require no return statement
+ public void setCadence(int newValue)
+ {
+ cadence = newValue;
+ }
+
+ // virtual keyword indicates this method can be overridden
+ public virtual void setGear(int newValue)
+ {
+ gear = newValue;
+ }
+
+ public void speedUp(int increment)
+ {
+ _speed += increment;
+ }
+
+ public void slowDown(int decrement)
+ {
+ _speed -= decrement;
+ }
+
+ // properties get/set values
+ // when only data needs to be accessed, consider using properties.
+ // properties may have either get or set, or both
+ private bool _hasTassles; // private variable
+ public bool hasTassles // public accessor
+ {
+ get { return _hasTassles; }
+ set { _hasTassles = value; }
+ }
+
+ private int _frameSize;
+ public int FrameSize
+ {
+ get { return _frameSize; }
+ // you are able to specify access modifiers for either get or set
+ // this means only Bicycle class can call set on Framesize
+ private set { _frameSize = value; }
+ }
+
+ //Method to display the attribute values of this Object.
+ public override string ToString()
+ {
+ return "gear: " + gear +
+ " cadence: " + cadence +
+ " speed: " + _speed +
+ " name: " + name +
+ " cards in spokes: " + (hasCardsInSpokes ? "yes" : "no") +
+ "\n------------------------------\n"
+ ;
+ }
+ } // end class Bicycle
+
+ // PennyFarthing is a subclass of Bicycle
+ class PennyFarthing : Bicycle
+ {
+ // (Penny Farthings are those bicycles with the big front wheel.
+ // They have no gears.)
+
+ // calling parent constructor
+ public PennyFarthing(int startCadence, int startSpeed) :
+ base(startCadence, startSpeed, 0, "PennyFarthing", true)
+ {
+ }
+
+ public override void setGear(int gear)
+ {
+ gear = 0;
+ }
+ }
+} // End Namespace
+
+```
+
+## Topics Not Covered
+
+ * Enums, Flags
+ * Attributes
+ * Generics (T), Delegates, Func, Actions, lambda expressions
+ * Exceptions, Interfaces, Abstraction
+ * LINQ
+ * ASP.NET (Web Forms/MVC/WebMatrix)
+ * Winforms
+ * Windows Presentation Foundation (WPF)
+
+
+
+## Further Reading
+
+ * [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/es-es/coffeescript-es.html.markdown b/es-es/coffeescript-es.html.markdown
new file mode 100644
index 00000000..a58c0d07
--- /dev/null
+++ b/es-es/coffeescript-es.html.markdown
@@ -0,0 +1,56 @@
+---
+language: coffeescript
+lang: es-es
+contributors:
+ - ["Tenor Biel", "http://github.com/L8D"]
+translators:
+ - ["Pablo Elices", "http://github.com/pabloelices"]
+filename: coffeescript-es.coffee
+---
+
+``` coffeescript
+# CoffeeScript es un lenguaje hipster.
+# Tiene convenciones de muchos lenguajes modernos.
+# Los comentarios son como en Ruby y Python, usan almohadillas.
+
+###
+Los comentarios en bloque son como estos, y se traducen directamente a '/*' y '*/' para el código JavaScript resultante.
+
+Deberías entender la mayor parte de la semántica de JavaScript antes de continuar.
+###
+
+# Asignación:
+number = 42 #=> var number = 42;
+opposite = true #=> var opposite = true;
+
+# Condiciones:
+number = -42 if opposite #=> if(opposite) { number = -42; }
+
+# Funciones:
+square = (x) -> x * x #=> var square = function(x) { return x * x; }
+
+# Rangos:
+list = [1..5] #=> var list = [1, 2, 3, 4, 5];
+
+# Objetos:
+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); }
+#}
+
+# Símbolos:
+race = (winner, runners...) ->
+ print winner, runners
+
+# Existencia:
+alert "I knew it!" if elvis?
+#=> if(typeof elvis !== "undefined" && elvis !== null) { alert("I knew it!"); }
+
+# Colecciones por comprensión:
+cubes = (math.cube num for num in list) #=> ...
+```
diff --git a/go.html.markdown b/go.html.markdown
new file mode 100644
index 00000000..e7b35926
--- /dev/null
+++ b/go.html.markdown
@@ -0,0 +1,301 @@
+---
+name: Go
+category: language
+language: Go
+filename: learngo.go
+contributors:
+ - ["Sonia Keys", "https://github.com/soniakeys"]
+---
+
+Go was created out of the need to get work done. It's not the latest trend
+in computer science, but it is the newest fastest way to solve real-world
+problems.
+
+It has familiar concepts of imperative languages with static typing.
+It's fast to compile and fast to execute, it adds easy-to-understand
+concurrency to leverage today's multi-core CPUs, and has features to
+help with large-scale programming.
+
+Go comes with a great standard library and an enthusiastic community.
+
+```Go
+// Single line comment
+/* Multi-
+ line comment */
+
+// A package clause starts every source file.
+// Main is a special name declaring an executable rather than a library.
+package main
+
+// Import declaration declares library packages referenced in this file.
+import (
+ "fmt" // A package in the Go standard library
+ "net/http" // Yes, a web server!
+ "strconv" // String conversions
+)
+
+// A function definition. Main is special. It is the entry point for the
+// executable program. Love it or hate it, Go uses brace brackets.
+func main() {
+ // Println outputs a line to stdout.
+ // Qualify it with the package name, fmt.
+ fmt.Println("Hello world!")
+
+ // Call another function within this package.
+ beyondHello()
+}
+
+// Functions have parameters in parentheses.
+// If there are no parameters, empty parens are still required.
+func beyondHello() {
+ var x int // Variable declaration. Variables must be declared before use.
+ x = 3 // Variable assignment.
+ // "Short" declarations use := to infer the type, declare, and assign.
+ y := 4
+ sum, prod := learnMultiple(x, y) // function returns two values
+ fmt.Println("sum:", sum, "prod:", prod) // simple output
+ learnTypes() // < y minutes, learn more!
+}
+
+// Functions can have parameters and (multiple!) return values.
+func learnMultiple(x, y int) (sum, prod int) {
+ return x + y, x * y // return two values
+}
+
+// Some built-in types and literals.
+func learnTypes() {
+ // Short declaration usually gives you what you want.
+ s := "Learn Go!" // string type
+
+ s2 := `A "raw" string literal
+can include line breaks.` // same string type
+
+ // non-ASCII literal. Go source is UTF-8.
+ g := 'Σ' // rune type, an alias for uint32, holds a UTF-8 code point
+
+ f := 3.14195 // float64, an IEEE-754 64-bit floating point number
+ c := 3 + 4i // complex128, represented internally with two float64s
+
+ // Var syntax with an initializers.
+ var u uint = 7 // unsigned, but implementation dependent size as with int
+ var pi float32 = 22. / 7
+
+ // Conversion syntax with a short declaration.
+ n := byte('\n') // byte is an alias for uint8
+
+ // Arrays have size fixed at compile time.
+ var a4 [4]int // an array of 4 ints, initialized to all 0
+ a3 := [...]int{3, 1, 5} // an array of 3 ints, initialized as shown
+
+ // Slices have dynamic size. Arrays and slices each have advantages
+ // but use cases for slices are much more common.
+ s3 := []int{4, 5, 9} // compare to a3. no ellipsis here
+ s4 := make([]int, 4) // allocates slice of 4 ints, initialized to all 0
+ var d2 [][]float64 // declaration only, nothing allocated here
+ bs := []byte("a slice") // type conversion syntax
+
+ p, q := learnMemory() // declares p, q to be type pointer to int.
+ fmt.Println(*p, *q) // * follows a pointer. This prints two ints.
+
+ // Maps are a dynamically growable associative array type, like the
+ // hash or dictionary types of some other languages.
+ m := map[string]int{"three": 3, "four": 4}
+ m["one"] = 1
+
+ // Unused variables are an error in Go.
+ // The underbar lets you "use" a variable but discard its value.
+ _, _, _, _, _, _, _, _, _ = s2, g, f, u, pi, n, a3, s4, bs
+ // Output of course counts as using a variable.
+ fmt.Println(s, c, a4, s3, d2, m)
+
+ learnFlowControl() // back in the flow
+}
+
+// Go is fully garbage collected. It has pointers but no pointer arithmetic.
+// You can make a mistake with a nil pointer, but not by incrementing a pointer.
+func learnMemory() (p, q *int) {
+ // Named return values p and q have type pointer to int.
+ p = new(int) // built-in function new allocates memory.
+ // The allocated int is initialized to 0, p is no longer nil.
+ s := make([]int, 20) // allocate 20 ints as a single block of memory
+ s[3] = 7 // assign one of them
+ r := -2 // declare another local variable
+ return &s[3], &r // & takes the address of an object.
+}
+
+func expensiveComputation() int {
+ return 1e6
+}
+
+func learnFlowControl() {
+ // If statements require brace brackets, and do not require parens.
+ if true {
+ fmt.Println("told ya")
+ }
+ // Formatting is standardized by the command line command "go fmt."
+ if false {
+ // pout
+ } else {
+ // gloat
+ }
+ // Use switch in preference to chained if statements.
+ x := 1
+ switch x {
+ case 0:
+ case 1:
+ // cases don't "fall through"
+ case 2:
+ // unreached
+ }
+ // Like if, for doesn't use parens either.
+ for x := 0; x < 3; x++ { // ++ is a statement
+ fmt.Println("iteration", x)
+ }
+ // x == 1 here.
+
+ // For is the only loop statement in Go, but it has alternate forms.
+ for { // infinite loop
+ break // just kidding
+ continue // unreached
+ }
+ // As with for, := in an if statement means to declare and assign y first,
+ // then test y > x.
+ if y := expensiveComputation(); y > x {
+ x = y
+ }
+ // Function literals are closures.
+ xBig := func() bool {
+ return x > 100 // references x declared above switch statement.
+ }
+ fmt.Println("xBig:", xBig()) // true (we last assigned 1e6 to x)
+ x /= 1e5 // this makes it == 10
+ fmt.Println("xBig:", xBig()) // false now
+
+ // When you need it, you'll love it.
+ goto love
+love:
+
+ learnInterfaces() // Good stuff coming up!
+}
+
+// Define Stringer as an interface type with one method, String.
+type Stringer interface {
+ String() string
+}
+
+// Define pair as a struct with two fields, ints named x and y.
+type pair struct {
+ x, y int
+}
+
+// Define a method on type pair. Pair now implements Stringer.
+func (p pair) String() string { // p is called the "receiver"
+ // Sprintf is another public function in package fmt.
+ // Dot syntax references fields of p.
+ return fmt.Sprintf("(%d, %d)", p.x, p.y)
+}
+
+func learnInterfaces() {
+ // Brace syntax is a "struct literal." It evaluates to an initialized
+ // struct. The := syntax declares and initializes p to this struct.
+ p := pair{3, 4}
+ fmt.Println(p.String()) // call String method of p, of type pair.
+ var i Stringer // declare i of interface type Stringer.
+ i = p // valid because pair implements Stringer
+ // Call String method of i, of type Stringer. Output same as above.
+ fmt.Println(i.String())
+
+ // Functions in the fmt package call the String method to ask an object
+ // for a printable representation of itself.
+ fmt.Println(p) // output same as above. Println calls String method.
+ fmt.Println(i) // output same as above
+
+ learnErrorHandling()
+}
+
+func learnErrorHandling() {
+ // ", ok" idiom used to tell if something worked or not.
+ m := map[int]string{3: "three", 4: "four"}
+ if x, ok := m[1]; !ok { // ok will be false because 1 is not in the map.
+ fmt.Println("no one there")
+ } else {
+ fmt.Print(x) // x would be the value, if it were in the map.
+ }
+ // An error value communicates not just "ok" but more about the problem.
+ if _, err := strconv.Atoi("non-int"); err != nil { // _ discards value
+ // prints "strconv.ParseInt: parsing "non-int": invalid syntax"
+ fmt.Println(err)
+ }
+ // We'll revisit interfaces a little later. Meanwhile,
+ learnConcurrency()
+}
+
+// c is a channel, a concurrency-safe communication object.
+func inc(i int, c chan int) {
+ c <- i + 1 // <- is the "send" operator when a channel appears on the left.
+}
+
+// We'll use inc to increment some numbers concurrently.
+func learnConcurrency() {
+ // Same make function used earlier to make a slice. Make allocates and
+ // initializes slices, maps, and channels.
+ c := make(chan int)
+ // Start three concurrent goroutines. Numbers will be incremented
+ // concurrently, perhaps in parallel if the machine is capable and
+ // properly configured. All three send to the same channel.
+ go inc(0, c) // go is a statement that starts a new goroutine.
+ go inc(10, c)
+ go inc(-805, c)
+ // Read three results from the channel and print them out.
+ // There is no telling in what order the results will arrive!
+ fmt.Println(<-c, <-c, <-c) // channel on right, <- is "receive" operator.
+
+ cs := make(chan string) // another channel, this one handles strings.
+ cc := make(chan chan string) // a channel of channels.
+ go func() { c <- 84 }() // start a new goroutine just to send a value
+ go func() { cs <- "wordy" }() // again, for cs this time
+ // Select has syntax like a switch statement but each case involves
+ // a channel operation. It selects a case at random out of the cases
+ // that are ready to communicate.
+ select {
+ case i := <-c: // the value received can be assigned to a variable
+ fmt.Println("it's a", i)
+ case <-cs: // or the value received can be discarded
+ fmt.Println("it's a string")
+ case <-cc: // empty channel, not ready for communication.
+ fmt.Println("didn't happen.")
+ }
+ // At this point a value was taken from either c or cs. One of the two
+ // goroutines started above has completed, the other will remain blocked.
+
+ learnWebProgramming() // Go does it. You want to do it too.
+}
+
+// A single function from package http starts a web server.
+func learnWebProgramming() {
+ // ListenAndServe first parameter is TCP address to listen at.
+ // Second parameter is an interface, specifically http.Handler.
+ err := http.ListenAndServe(":8080", pair{})
+ fmt.Println(err) // don't ignore errors
+}
+
+// Make pair an http.Handler by implementing its only method, ServeHTTP.
+func (p pair) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ // Serve data with a method of http.ResponseWriter
+ w.Write([]byte("You learned Go in Y minutes!"))
+}
+```
+
+## Further Reading
+
+The root of all things Go is the [official Go web site](http://golang.org/).
+There you can follow the tutorial, play interactively, and read lots.
+
+The language definition itself is highly recommended. It's easy to read
+and amazingly short (as language definitions go these days.)
+
+On the reading list for students of Go is the source code to the standard
+library. Comprehensively documented, it demonstrates the best of readable
+and understandable Go, Go style, and Go idioms. Click on a function name
+in the documentation and the source code comes up!
+
diff --git a/haskell.html.markdown b/haskell.html.markdown
index 9847ef2a..e3ec3f38 100644
--- a/haskell.html.markdown
+++ b/haskell.html.markdown
@@ -84,7 +84,7 @@ not False -- True
-- rest of the elements of this "infinite" list don't exist yet! Haskell won't
-- actually evaluate them until it needs to.
-- joining two lists
+-- joining two lists
[1..5] ++ [6..10]
-- adding to the head of a list
diff --git a/ko-kr/lua-kr.html.markdown b/ko-kr/lua-kr.html.markdown
index 7e8c2bc0..04d119c4 100644
--- a/ko-kr/lua-kr.html.markdown
+++ b/ko-kr/lua-kr.html.markdown
@@ -1,4 +1,4 @@
----
+---
language: lua
category: language
contributors:
@@ -395,6 +395,7 @@ g() -- 343이 출력됩니다. 그전까지는 아무것도 출력되지 않습
--]]
```
+
## 참고자료
루아를 배우는 일이 흥미진진했던 이유는 <a href="http://love2d.org/">Love 2D 게임 엔진</a>을 이용해
diff --git a/objective-c.html.markdown b/objective-c.html.markdown
new file mode 100644
index 00000000..2b1b3c67
--- /dev/null
+++ b/objective-c.html.markdown
@@ -0,0 +1,310 @@
+---
+
+language: Objective-C
+contributors:
+ - ["Eugene Yagrushkin", "www.about.me/yagrushkin"]
+ - ["Yannick Loriot", "https://github.com/YannickL"]
+filename: LearnObjectiveC.m
+
+---
+
+Objective-C is the main programming language used by Apple for the OS X and iOS operating systems and their respective frameworks, Cocoa and Cocoa Touch.
+It is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language.
+
+```cpp
+// Single-line comments start with //
+
+/*
+Multi-line comments look like this.
+*/
+
+// Imports the Foundation headers with #import
+#import <Foundation/Foundation.h>
+#import "MyClass.h"
+
+// Your program's entry point is a function called
+// main with an integer return type.
+int main (int argc, const char * argv[])
+{
+ // Create an autorelease pool to manage the memory into the program
+ NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
+
+ // Use NSLog to print lines to the console
+ NSLog(@"Hello World!"); // Print the string "Hello World!"
+
+ ///////////////////////////////////////
+ // Types & Variables
+ ///////////////////////////////////////
+
+ // Primitive declarations
+ int myPrimitive1 = 1;
+ long myPrimitive2 = 234554664565;
+
+ // Object declarations
+ // Put the * in front of the variable names for strongly-typed object declarations
+ MyClass *myObject1 = nil; // Strong typing
+ id myObject2 = nil; // Weak typing
+ // %@ is an object
+ // 'description' is a convention to display the value of the Objects
+ NSLog(@"%@ and %@", myObject1, [myObject2 description]); // Print "(null) and (null)"
+
+ // String
+ NSString *worldString = @"World";
+ NSLog(@"Hello %@!", worldString); // Print "Hello World!"
+
+ // Character literals
+ NSNumber *theLetterZNumber = @'Z';
+ char theLetterZ = [theLetterZNumber charValue];
+ NSLog(@"%c", theLetterZ);
+
+ // Integral literals
+ NSNumber *fortyTwoNumber = @42;
+ int fortyTwo = [fortyTwoNumber intValue];
+ NSLog(@"%i", fortyTwo);
+
+ NSNumber *fortyTwoUnsignedNumber = @42U;
+ unsigned int fortyTwoUnsigned = [fortyTwoUnsignedNumber unsignedIntValue];
+ NSLog(@"%u", fortyTwoUnsigned);
+
+ NSNumber *fortyTwoShortNumber = [NSNumber numberWithShort:42];
+ short fortyTwoShort = [fortyTwoShortNumber shortValue];
+ NSLog(@"%hi", fortyTwoShort);
+
+ NSNumber *fortyTwoLongNumber = @42L;
+ long fortyTwoLong = [fortyTwoLongNumber longValue];
+ NSLog(@"%li", fortyTwoLong);
+
+ // Floating point literals
+ NSNumber *piFloatNumber = @3.141592654F;
+ float piFloat = [piFloatNumber floatValue];
+ NSLog(@"%f", piFloat);
+
+ NSNumber *piDoubleNumber = @3.1415926535;
+ piDouble = [piDoubleNumber doubleValue];
+ NSLog(@"%f", piDouble);
+
+ // BOOL literals
+ NSNumber *yesNumber = @YES;
+ NSNumber *noNumber = @NO;
+
+ // Array object
+ NSArray *anArray = @[@1, @2, @3, @4];
+ NSNumber *thirdNumber = anArray[2];
+ NSLog(@"Third number = %@", thirdObject); // Print "Third number = 3"
+
+ // Dictionary object
+ NSDictionary *aDictionary = @{ @"key1" : @"value1", @"key2" : @"value2" };
+ NSObject *valueObject = aDictionary[@"A Key"];
+ NSLog(@"Object = %@", valueObject); // Print "Object = (null)"
+
+ ///////////////////////////////////////
+ // Operators
+ ///////////////////////////////////////
+
+ // The operators works like in the C language
+ // For example:
+ 2 + 5; // => 7
+ 4.2f + 5.1f; // => 9.3f
+ 3 == 2; // => 0 (NO)
+ 3 != 2; // => 1 (YES)
+ 1 && 1; // => 1 (Logical and)
+ 0 || 1; // => 1 (Logical or)
+ ~0x0F; // => 0xF0 (bitwise negation)
+ 0x0F & 0xF0; // => 0x00 (bitwise AND)
+ 0x01 << 1; // => 0x02 (bitwise left shift (by 1))
+
+ ///////////////////////////////////////
+ // Control Structures
+ ///////////////////////////////////////
+
+ // If-Else statement
+ if (NO)
+ {
+ NSLog(@"I am never run");
+ } else if (0)
+ {
+ NSLog(@"I am also never run");
+ } else
+ {
+ NSLog(@"I print");
+ }
+
+ // Switch statement
+ switch (2)
+ {
+ case 0:
+ {
+ NSLog(@"I am never run");
+ } break;
+ case 1:
+ {
+ NSLog(@"I am also never run");
+ } break;
+ default:
+ {
+ NSLog(@"I print");
+ } break;
+ }
+
+ // While loops statements
+ int ii = 0;
+ while (ii < 4)
+ {
+ NSLog(@"%d,", ii++); // ii++ increments ii in-place, after using its value.
+ } // => prints "0,"
+ // "1,"
+ // "2,"
+ // "3,"
+
+ // For loops statements
+ int jj;
+ for (jj=0; jj < 4; jj++)
+ {
+ NSLog(@"%d,", ii++);
+ } // => prints "0,"
+ // "1,"
+ // "2,"
+ // "3,"
+
+ // Foreach statements
+ NSArray *values = @[@0, @1, @2, @3];
+ for (NSNumber *value in values)
+ {
+ NSLog(@"%@,", value);
+ } // => prints "0,"
+ // "1,"
+ // "2,"
+ // "3,"
+
+ // Try-Catch-Finally statements
+ @try
+ {
+ // Your statements here
+ @throw [NSException exceptionWithName:@"FileNotFoundException"
+ reason:@"File Not Found on System" userInfo:nil];
+ } @catch (NSException * e)
+ {
+ NSLog(@"Exception: %@", e);
+ } @finally
+ {
+ NSLog(@"Finally");
+ } // => prints "Exception: File Not Found on System"
+ // "Finally"
+
+ ///////////////////////////////////////
+ // Objects
+ ///////////////////////////////////////
+
+ // Create an object instance by allocating memory and initializing it.
+ // An object is not fully functional until both steps have been completed.
+ MyClass *myObject = [[MyClass alloc] init];
+
+ // The Objective-C model of object-oriented programming is based on message
+ // passing to object instances.
+ // In Objective-C one does not simply call a method; one sends a message.
+ [myObject instanceMethodWithParameter:@"Steve Jobs"];
+
+ // Clean up the memory you used into your program
+ [pool drain];
+
+ // End the program
+ return 0;
+}
+
+///////////////////////////////////////
+// Classes And Functions
+///////////////////////////////////////
+
+// Declare your class in a header(MyClass.h) file:
+// Class Declaration Syntax:
+// @interface ClassName : ParentClassName <ImplementedProtocols>
+// {
+// Member variable declarations;
+// }
+// -/+ (type) Method declarations;
+// @end
+@interface MyClass : NSObject <MyCustomProtocol>
+{
+ int count;
+ id data;
+ NSString *name;
+}
+// Convenience notation to auto generate public getter and setter
+@property int count;
+@property (copy) NSString *name; // Copy the object during assignment.
+@property (readonly) id data; // Declare only a getter method.
+
+// Methods
++/- (return type)methodSignature:(Parameter Type *)parameterName;
+
+// + for class method
++ (NSString *)classMethod;
+
+// - for instance method
+- (NSString *)instanceMethodWithParmeter:(NSString *)string;
+- (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number;
+
+@end
+
+// Implement the methods in an implementation (MyClass.m) file:
+
+@implementation UserObject
+
+// Call when the object is releasing
+- (void)dealloc
+{
+}
+
+// Constructors are a way of creating classes
+// This is a default constructor which is call when the object is creating
+- (id)init
+{
+ if ((self = [super init]))
+ {
+ self.count = 1;
+ }
+ return self;
+}
+
++ (NSString *)classMethod
+{
+ return [[self alloc] init];
+}
+
+- (NSString *)instanceMethodWithParmeter:(NSString *)string
+{
+ return @"New string";
+}
+
+- (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number
+{
+ return @42;
+}
+
+// Methods declared into MyProtocol
+- (void)myProtocolMethod
+{
+ // statements
+}
+
+@end
+
+/*
+ * A protocol declares methods that can be implemented by any class.
+ * Protocols are not classes themselves. They simply define an interface
+ * that other objects are responsible for implementing.
+ */
+@protocol MyProtocol
+ - (void)myProtocolMethod;
+@end
+
+
+
+```
+## Further Reading
+
+[Wikipedia Objective-C](http://en.wikipedia.org/wiki/Objective-C)
+
+[Learning Objective-C](http://developer.apple.com/library/ios/referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/)
+
+[iOS For High School Students: Getting Started](http://www.raywenderlich.com/5600/ios-for-high-school-students-getting-started)
diff --git a/php.html.markdown b/php.html.markdown
index ce228870..083574ee 100644
--- a/php.html.markdown
+++ b/php.html.markdown
@@ -440,6 +440,11 @@ class MyClass
static $staticVar = 'static';
+ // Static variables and their visibility
+ public static $publicStaticVar = 'publicStatic';
+ private static $privateStaticVar = 'privateStatic'; // Accessible within the class only
+ protected static $protectedStaticVar = 'protectedStatic'; // Accessible from the class and subclasses
+
// Properties must declare their visibility
public $property = 'public';
public $instanceProp;
diff --git a/pt-br/elisp-pt.html.markdown b/pt-br/elisp-pt.html.markdown
new file mode 100644
index 00000000..9031cad9
--- /dev/null
+++ b/pt-br/elisp-pt.html.markdown
@@ -0,0 +1,359 @@
+---
+language: elisp
+contributors:
+ - ["Bastien Guerry", "http://bzg.fr"]
+translators:
+ - ["Lucas Tadeu Teixeira", "http://ltt.me"]
+lang: pt-br
+filename: learn-emacs-lisp-pt.el
+---
+
+```scheme
+;; Introdução ao Emacs Lisp em 15 minutos (v0.2d)
+;;
+;; Autor: Bastien / @bzg2 / http://bzg.fr
+;;
+;; Antes de começar, leia este texto escrito Peter Norvig:
+;; http://norvig.com/21-days.html
+;;
+;; Agora instale GNU Emacs 24.3:
+;;
+;; Debian: apt-get install emacs (ou veja as instruções da sua distribuição)
+;; OSX: http://emacsformacosx.com/emacs-builds/Emacs-24.3-universal-10.6.8.dmg
+;; Windows: http://ftp.gnu.org/gnu/windows/emacs/emacs-24.3-bin-i386.zip
+;;
+;; Informações mais gerais podem ser encontradas em:
+;; http://www.gnu.org/software/emacs/#Obtaining
+
+;; Aviso importante:
+;;
+;; Realizar este tutorial não danificará seu computador, a menos
+;; que você fique tão irritado a ponto de jogá-lo no chão. Neste caso,
+;; me abstenho de qualquer responsabilidade. Divirta-se!
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;
+;; Abra o Emacs.
+;;
+;; Aperte a tecla `q' para ocultar a mensagem de boas vindas.
+;;
+;; Agora olhe para a linha cinza na parte inferior da janela:
+;;
+;; "*scratch*" é o nome do espaço de edição em que você se encontra.
+;; Este espaço de edição é chamado "buffer".
+;;
+;; O buffer de rascunho (i.e., "scratch") é o buffer padrão quando
+;; o Emacs é aberto. Você nunca está editando arquivos: você está
+;; editando buffers que você pode salvar em um arquivo.
+;;
+;; "Lisp interaction" refere-se a um conjunto de comandos disponíveis aqui.
+;;
+;; O Emacs possui um conjunto de comandos embutidos (disponíveis em
+;; qualquer buffer) e vários subconjuntos de comandos disponíveis
+;; quando você ativa um modo específico. Aqui nós utilizamos
+;; `lisp-interaction-mode', que possui comandos para interpretar e navegar
+;; em código Elisp.
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;
+;; Pontos e vírgulas iniciam comentários em qualquer parte de uma linha.
+;;
+;; Programas codificados em Elisp são compostos por expressões simbólicas
+;; (conhecidas também por "sexps"):
+(+ 2 2)
+
+;; Esta expressão simbólica significa "Some 2 e 2".
+
+;; "Sexps" são envoltas em parêntese, possivelmente aninhados:
+(+ 2 (+ 1 1))
+
+;; Uma expressão simbólica contém átomos ou outras expressões
+;; simbólicas. Nos exemplos acima, 1 e 2 são átomos;
+;; (+ 2 (+ 1 1)) e (+ 1 1) são expressões simbólicas.
+
+;; No modo `lisp-interaction-mode' você pode interpretar "sexps".
+;; Posicione o cursor logo após o parêntese de fechamento e,
+;; então, segure apertado Ctrl e aperte a tecla j ("C-j", em resumo).
+
+(+ 3 (+ 1 2))
+;; ^ posicione o cursor aqui
+;; `C-j' => 6
+
+;; `C-j' insere o resultado da interpretação da expressão no buffer.
+
+;; `C-xC-e' exibe o mesmo resultado na linha inferior do Emacs,
+;; chamada de "mini-buffer". Nós geralmente utilizaremos `C-xC-e',
+;; já que não queremos poluir o buffer com texto desnecessário.
+
+;; `setq' armazena um valor em uma variável:
+(setq my-name "Bastien")
+;; `C-xC-e' => "Bastien" (texto exibido no mini-buffer)
+
+;; `insert' insere "Hello!" na posição em que se encontra seu cursor:
+(insert "Hello!")
+;; `C-xC-e' => "Hello!"
+
+;; Nós executamos `insert' com apenas um argumento ("Hello!"), mas
+;; mais argumentos podem ser passados -- aqui utilizamos dois:
+
+(insert "Hello" " world!")
+;; `C-xC-e' => "Hello world!"
+
+;; Você pode utilizar variávies no lugar de strings:
+(insert "Hello, I am " my-name)
+;; `C-xC-e' => "Hello, I am Bastien"
+
+;; Você pode combinar "sexps" em funções:
+(defun hello () (insert "Hello, I am " my-name))
+;; `C-xC-e' => hello
+
+;; Você pode interpretar chamadas de funções:
+(hello)
+;; `C-xC-e' => Hello, I am Bastien
+
+;; Os parêntesis vazios na definição da função significam que ela
+;; não aceita argumentos. Mas sempre utilizar `my-name' é um tédio!
+;; Vamos dizer à função para aceitar um argumento (o argumento é
+;; chamado "name"):
+
+(defun hello (name) (insert "Hello " name))
+;; `C-xC-e' => hello
+
+;; Agora vamos executar a função com a string "you" como o valor
+;; para seu único parâmetro:
+(hello "you")
+;; `C-xC-e' => "Hello you"
+
+;; Aí sim!
+
+;; Respire um pouco.
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;
+;; Agora mude para um novo buffer chamado "*test*":
+
+(switch-to-buffer-other-window "*test*")
+;; `C-xC-e'
+;; => [a tela exibirá duas janelas e o cursor estará no buffer *test*]
+
+;; Posicione o mouse sobre a janela superior e clique com o botão
+;; esquerdo para voltar. Ou você pode utilizar `C-xo' (i.e. segure
+;; ctrl-x e aperte o) para voltar para a outra janela, de forma interativa.
+
+;; Você pode combinar várias "sexps" com `progn':
+(progn
+ (switch-to-buffer-other-window "*test*")
+ (hello "you"))
+;; `C-xC-e'
+;; => [A tela exibirá duas janelas e o cursor estará no buffer *test*]
+
+;; Agora, se você não se importar, pararei de pedir que você aperte
+;; `C-xC-e': faça isso para cada "sexp" que escrevermos.
+
+;; Sempre volte para o buffer *scratch* com o mouse ou `C-xo'.
+
+;; Frequentemente, é útil apagar o conteúdo do buffer:
+(progn
+ (switch-to-buffer-other-window "*test*")
+ (erase-buffer)
+ (hello "there"))
+
+;; Ou voltar para a outra janela:
+(progn
+ (switch-to-buffer-other-window "*test*")
+ (erase-buffer)
+ (hello "you")
+ (other-window 1))
+
+;; Você pode armazenar um valor em uma variável local utilizando `let':
+(let ((local-name "you"))
+ (switch-to-buffer-other-window "*test*")
+ (erase-buffer)
+ (hello local-name)
+ (other-window 1))
+
+;; Neste caso, não é necessário utilizar `progn' já que `let' combina
+;; várias "sexps".
+
+;; Vamos formatar uma string:
+(format "Hello %s!\n" "visitor")
+
+;; %s é um espaço reservado para uma string, substituído por "visitor".
+;; \n é um caractere de nova linha.
+
+;; Vamos refinar nossa função utilizando `format':
+(defun hello (name)
+ (insert (format "Hello %s!\n" name)))
+
+(hello "you")
+
+;; Vamos criar outra função que utilize `let':
+(defun greeting (name)
+ (let ((your-name "Bastien"))
+ (insert (format "Hello %s!\n\nI am %s."
+ name ; the argument of the function
+ your-name ; the let-bound variable "Bastien"
+ ))))
+
+;; E executá-la:
+(greeting "you")
+
+;; Algumas funções são interativas:
+(read-from-minibuffer "Enter your name: ")
+
+;; Ao ser interpretada, esta função retorna o que você digitou no prompt.
+
+;; Vamos fazer nossa função `greeting' pedir pelo seu nome:
+(defun greeting (from-name)
+ (let ((your-name (read-from-minibuffer "Enter your name: ")))
+ (insert (format "Hello!\n\nI am %s and you are %s."
+ from-name ; the argument of the function
+ your-name ; the let-bound var, entered at prompt
+ ))))
+
+(greeting "Bastien")
+
+;; Vamos finalizá-la fazendo-a exibir os resultados em outra janela:
+(defun greeting (from-name)
+ (let ((your-name (read-from-minibuffer "Enter your name: ")))
+ (switch-to-buffer-other-window "*test*")
+ (erase-buffer)
+ (insert (format "Hello %s!\n\nI am %s." your-name from-name))
+ (other-window 1)))
+
+;; Agora teste-a:
+(greeting "Bastien")
+
+;; Respire um pouco.
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;
+;; Vamos armazenar uma lista de nomes:
+(setq list-of-names '("Sarah" "Chloe" "Mathilde"))
+
+;; Pegue o primeiro elemento desta lista utilizando `car':
+(car list-of-names)
+
+;; Pegue uma lista de todos os elementos, exceto o primeiro, utilizando
+;; `cdr':
+(cdr list-of-names)
+
+;; Adicione um elemento ao início da lista com `push':
+(push "Stephanie" list-of-names)
+
+;; NOTA: `car' e `cdr' não modificam a lista, `push' sim.
+;; Esta é uma diferença importante: algumas funções não têm qualquer
+;; efeito colateral (como `car'), enquanto outras sim (como `push').
+
+;; Vamos executar `hello' para cada elemento em `list-of-names':
+(mapcar 'hello list-of-names)
+
+;; Refine `greeting' para saudar todos os nomes em `list-of-names':
+(defun greeting ()
+ (switch-to-buffer-other-window "*test*")
+ (erase-buffer)
+ (mapcar 'hello list-of-names)
+ (other-window 1))
+
+(greeting)
+
+;; Você se lembra da função `hello' que nós definimos lá em cima? Ela
+;; recebe um argumento, um nome. `mapcar' executa `hello', sucessivamente,
+;; utilizando cada elemento de `list-of-names' como argumento para `hello'.
+
+;; Agora vamos arrumar, um pouco, o que nós temos escrito no buffer:
+
+(defun replace-hello-by-bonjour ()
+ (switch-to-buffer-other-window "*test*")
+ (goto-char (point-min))
+ (while (search-forward "Hello")
+ (replace-match "Bonjour"))
+ (other-window 1))
+
+;; (goto-char (point-min)) vai para o início do buffer.
+;; (search-forward "Hello") busca pela string "Hello".
+;; (while x y) interpreta a(s) sexp(s) y enquanto x retornar algo.
+;; Se x retornar `nil' (nada), nós saímos do laço.
+
+(replace-hello-by-bonjour)
+
+;; Você deveria ver todas as ocorrências de "Hello" no buffer *test*
+;; substituídas por "Bonjour".
+
+;; Você deveria, também, receber um erro: "Search failed: Hello".
+;;
+;; Para evitar este erro, você precisa dizer ao `search-forward' se ele
+;; deveria parar de buscar em algum ponto no buffer, e se ele deveria
+;; falhar de forma silenciosa quando nada fosse encontrado:
+
+;; (search-forward "Hello" nil t) dá conta do recado:
+
+;; O argumento `nil' diz: a busca não está limitada a uma posição.
+;; O argumento `t' diz: falhe silenciosamente quando nada for encontrado.
+
+;; Nós utilizamos esta "sexp" na função abaixo, que não gera um erro:
+
+(defun hello-to-bonjour ()
+ (switch-to-buffer-other-window "*test*")
+ (erase-buffer)
+ ;; Say hello to names in `list-of-names'
+ (mapcar 'hello list-of-names)
+ (goto-char (point-min))
+ ;; Replace "Hello" by "Bonjour"
+ (while (search-forward "Hello" nil t)
+ (replace-match "Bonjour"))
+ (other-window 1))
+
+(hello-to-bonjour)
+
+;; Vamos colorir os nomes:
+
+(defun boldify-names ()
+ (switch-to-buffer-other-window "*test*")
+ (goto-char (point-min))
+ (while (re-search-forward "Bonjour \\(.+\\)!" nil t)
+ (add-text-properties (match-beginning 1)
+ (match-end 1)
+ (list 'face 'bold)))
+ (other-window 1))
+
+;; Esta função introduz `re-search-forward': ao invés de buscar
+;; pela string "Bonjour", você busca por um padrão utilizando uma
+;; "expressão regular" (abreviada pelo prefixo "re-").
+
+;; A expressão regular é "Bonjour \\(.+\\)!" e lê-se:
+;; a string "Bonjour ", e
+;; um grupo de | que é o \\( ... \\)
+;; quaisquer caracteres | que é o .
+;; possivelmente repetidos | que é o +
+;; e a string "!".
+
+;; Preparado? Teste!
+
+(boldify-names)
+
+;; `add-text-properties' adiciona... propriedades de texto, como uma fonte.
+
+;; OK, terminamos por aqui. Feliz Hacking!
+
+;; Se você quiser saber mais sobre uma variável ou função:
+;;
+;; C-h v uma-variável RET
+;; C-h f uma-função RET
+;;
+;; Para ler o manual de Emacs Lisp que vem com o Emacs:
+;;
+;; C-h i m elisp RET
+;;
+;; Para ler uma introdução online ao Emacs Lisp:
+;; https://www.gnu.org/software/emacs/manual/html_node/eintr/index.html
+
+;; Agradecimentos a estas pessoas por seu feedback e sugestões:
+;; - Wes Hardaker
+;; - notbob
+;; - Kevin Montuori
+;; - Arne Babenhauserheide
+;; - Alan Schmitt
+;; - LinXitoW
+;; - Aaron Meurer
+```
diff --git a/pt-br/python-pt.html.markdown b/pt-br/python-pt.html.markdown
index e08bb5a8..5afd46d0 100644
--- a/pt-br/python-pt.html.markdown
+++ b/pt-br/python-pt.html.markdown
@@ -4,7 +4,7 @@ contributors:
- ["Louie Dinh", "http://ldinh.ca"]
translators:
- ["Vilson Vieira", "http://automata.cc"]
-lang: pt-bf
+lang: pt-br
filename: learnpython-pt.py
---
diff --git a/ruby-ecosystem.html.markdown b/ruby-ecosystem.html.markdown
index a31f552d..cae55cd3 100644
--- a/ruby-ecosystem.html.markdown
+++ b/ruby-ecosystem.html.markdown
@@ -93,13 +93,13 @@ which MRI version to target.
## RubySpec
-Most ruby implementations rely heavily on (RubySpec)[http://rubyspec.org/]. Ruby
+Most ruby implementations rely heavily on [RubySpec](http://rubyspec.org/). Ruby
has no official specification, so the community has written executable specs in
ruby to test their implementations' compatability with MRI.
## RubyGems
-(RubyGems)[http://rubygems.org/] is a community-run package manager for ruby.
+[RubyGems](http://rubygems.org/) is a community-run package manager for ruby.
RubyGems ships with ruby, so there is no need to download it separately.
Ruby packages are called "gems," and they can be hosted by the community at
@@ -108,7 +108,7 @@ things like version, dependencies, author(s), and license(s).
## Bundler
-(Bundler)[http://bundler.io/] is a gem dependency resolver. It uses a project's
+[Bundler](http://bundler.io/) is a gem dependency resolver. It uses a project's
Gemfile to find dependencies, and then fetches those dependencies' dependencies
recursively. It does this until all dependencies are resolved and downloaded, or
it will stop if a conflict has been found.
diff --git a/ruby.html.markdown b/ruby.html.markdown
index 861a94ad..19f2ec86 100644
--- a/ruby.html.markdown
+++ b/ruby.html.markdown
@@ -5,6 +5,8 @@ contributors:
- ["David Underwood", "http://theflyingdeveloper.com"]
- ["Joel Walden", "http://joelwalden.net"]
- ["Luke Holder", "http://twitter.com/lukeholder"]
+ - ["Tristan Hume", "http://thume.ca/"]
+ - ["Nick LaMuro", "https://github.com/NickLaMuro"]
---
```ruby
@@ -116,11 +118,11 @@ status == :approved #=> false
# Arrays
# This is an array
-[1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5]
+array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5]
# Arrays can contain different types of items
-array = [1, "hello", false] #=> => [1, "hello", false]
+[1, "hello", false] #=> [1, "hello", false]
# Arrays can be indexed
# From the front
@@ -158,11 +160,6 @@ hash['number'] #=> 5
# Asking a hash for a key that doesn't exist returns nil:
hash['nothing here'] #=> nil
-# Iterate over hashes with the #each method:
-hash.each do |k, v|
- puts "#{k} is #{v}"
-end
-
# Since Ruby 1.9, there's a special syntax when using symbols as keys:
new_hash = { defcon: 3, action: true}
@@ -177,9 +174,9 @@ new_hash.keys #=> [:defcon, :action]
if true
"if statement"
elsif false
- "else if, optional"
+ "else if, optional"
else
- "else, also optional"
+ "else, also optional"
end
for counter in 1..5
@@ -191,9 +188,15 @@ end
#=> iteration 4
#=> iteration 5
-# HOWEVER
-# No-one uses for loops
-# Use `each` instead, like this:
+# HOWEVER, No-one uses for loops.
+# Instead you should use the "each" method and pass it a block.
+# A block is a bunch of code that you can pass to a method like "each".
+# It is analogous to lambdas, anonymous functions or closures in other
+# programming languages.
+#
+# The "each" method of a range runs the block once for each element of the range.
+# The block is passed a counter as a parameter.
+# Calling the "each" method with a block looks like this:
(1..5).each do |counter|
puts "iteration #{counter}"
@@ -204,6 +207,17 @@ end
#=> iteration 4
#=> iteration 5
+# You can also surround blocks in curly brackets:
+(1..5).each {|counter| puts "iteration #{counter}"}
+
+# The contents of data structures can also be iterated using each.
+array.each do |element|
+ puts "#{element} is part of the array"
+end
+hash.each do |key, value|
+ puts "#{key} is #{value}"
+end
+
counter = 1
while counter <= 5 do
puts "iteration #{counter}"
diff --git a/zh-cn/python-cn.html.markdown b/zh-cn/python-cn.html.markdown
index 764eed54..51efaac3 100755
--- 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]) # 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 6530b520..619e6e92 100644
--- a/zh-cn/ruby-cn.html.markdown
+++ b/zh-cn/ruby-cn.html.markdown
@@ -94,7 +94,7 @@ x = y = 10 #=> 10
x #=> 10
y #=> 10
-# 按照惯例,用snake_case 作为变量名
+# 按照惯例,用 snake_case 作为变量名
snake_case = true
# 使用具有描述性的运算符
@@ -102,7 +102,8 @@ path_to_project_root = '/good/name/'
path = '/bad/name/'
# 符号(Symbols,也是对象)
-# 符号是不可变的,内部用整数类型表示的可重用的值。通常用它代替字符串来有效地表达有意义的值
+# 符号是不可变的,内部用整数类型表示的可重用的值。
+# 通常用它代替字符串来有效地表示有意义的值。
:pending.class #=> Symbol