diff options
-rw-r--r-- | common-lisp.html.markdown | 604 | ||||
-rw-r--r-- | es-es/c-es.html.markdown | 2 | ||||
-rw-r--r-- | es-es/elisp-es.html.markdown | 2 | ||||
-rw-r--r-- | es-es/java-es.html.markdown | 2 | ||||
-rw-r--r-- | es-es/python-es.html.markdown | 2 | ||||
-rw-r--r-- | es-es/ruby-es.html.markdown | 2 | ||||
-rw-r--r-- | pt-br/python-pt.html.markdown | 2 | ||||
-rw-r--r-- | ruby-ecosystem.html.markdown | 6 | ||||
-rwxr-xr-x | zh-cn/c-cn.html.markdown | 2 | ||||
-rw-r--r-- | zh-cn/dart-cn.html.markdown | 2 | ||||
-rwxr-xr-x | zh-cn/elisp-cn.html.markdown | 2 | ||||
-rwxr-xr-x | zh-cn/git-cn.html.markdown | 1 | ||||
-rwxr-xr-x | zh-cn/haskell-cn.html.markdown | 4 | ||||
-rwxr-xr-x | zh-cn/java-cn.html.markdown | 2 | ||||
-rwxr-xr-x | zh-cn/javascript-cn.html.markdown | 1 | ||||
-rwxr-xr-x | zh-cn/php-cn.html.markdown | 2 | ||||
-rwxr-xr-x | zh-cn/python-cn.html.markdown | 2 | ||||
-rw-r--r-- | zh-cn/racket-cn.html.markdown | 607 | ||||
-rw-r--r-- | zh-cn/ruby-cn.html.markdown | 7 | ||||
-rw-r--r-- | zh-cn/scala-cn.html.markdown | 2 |
20 files changed, 1234 insertions, 22 deletions
diff --git a/common-lisp.html.markdown b/common-lisp.html.markdown new file mode 100644 index 00000000..24b6947f --- /dev/null +++ b/common-lisp.html.markdown @@ -0,0 +1,604 @@ +--- + +language: commonlisp +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/). + + + +```commonlisp + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; 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/es-es/c-es.html.markdown b/es-es/c-es.html.markdown index 0624f4be..b109f761 100644 --- a/es-es/c-es.html.markdown +++ b/es-es/c-es.html.markdown @@ -1,6 +1,6 @@ --- language: c -filename: learnc.c +filename: learnc-es.c contributors: - ["Adam Bard", "http://adambard.com/"] translators: diff --git a/es-es/elisp-es.html.markdown b/es-es/elisp-es.html.markdown index 431ea794..a6cd3934 100644 --- a/es-es/elisp-es.html.markdown +++ b/es-es/elisp-es.html.markdown @@ -5,7 +5,7 @@ contributors: translators: - ["Guillermo Vayá", "http://willyfrog.es"] lang: es-es -filename: learn-emacs-lisp.el +filename: learn-emacs-lisp-es.el --- ```scheme diff --git a/es-es/java-es.html.markdown b/es-es/java-es.html.markdown index 90a43935..b34dca8d 100644 --- a/es-es/java-es.html.markdown +++ b/es-es/java-es.html.markdown @@ -5,7 +5,7 @@ contributors: translators: - ["Camilo Garrido", "http://www.twitter.com/hirohope"] lang: es-es -filename: LearnJava.java +filename: LearnJava-es.java --- Java es un lenguage de programación de propósito general, concurrente, basado en clases y diff --git a/es-es/python-es.html.markdown b/es-es/python-es.html.markdown index 1ec8d7e4..f92f5cde 100644 --- a/es-es/python-es.html.markdown +++ b/es-es/python-es.html.markdown @@ -5,7 +5,7 @@ contributors: translators: - ["Camilo Garrido", "http://www.twitter.com/hirohope"] lang: es-es -filename: learnpython.py +filename: learnpython-es.py --- Python fue creado por Guido Van Rossum en el principio de los 90'. Ahora es uno diff --git a/es-es/ruby-es.html.markdown b/es-es/ruby-es.html.markdown index fa039676..66a5d0fe 100644 --- a/es-es/ruby-es.html.markdown +++ b/es-es/ruby-es.html.markdown @@ -1,6 +1,6 @@ --- language: ruby -filename: learnruby.rb +filename: learnruby-es.rb contributors: - ["David Underwood", "http://theflyingdeveloper.com"] - ["Joel Walden", "http://joelwalden.net"] diff --git a/pt-br/python-pt.html.markdown b/pt-br/python-pt.html.markdown index c365ba96..e08bb5a8 100644 --- a/pt-br/python-pt.html.markdown +++ b/pt-br/python-pt.html.markdown @@ -5,7 +5,7 @@ contributors: translators: - ["Vilson Vieira", "http://automata.cc"] lang: pt-bf -filename: learnpython.py +filename: learnpython-pt.py --- Python foi criado por Guido Van Rossum no começo dos anos 90. Atualmente é uma 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/zh-cn/c-cn.html.markdown b/zh-cn/c-cn.html.markdown index f8a8e0bd..b4bff8fc 100755 --- a/zh-cn/c-cn.html.markdown +++ b/zh-cn/c-cn.html.markdown @@ -1,6 +1,6 @@ --- language: c -filename: learnc.c +filename: learnc-cn.c contributors: - ["Adam Bard", "http://adambard.com/"] translators: diff --git a/zh-cn/dart-cn.html.markdown b/zh-cn/dart-cn.html.markdown index 1b0cceb9..6a6562bc 100644 --- a/zh-cn/dart-cn.html.markdown +++ b/zh-cn/dart-cn.html.markdown @@ -1,7 +1,7 @@ --- language: dart lang: zh-cn -filename: learndart.dart +filename: learndart-cn.dart contributors: - ["Joao Pedrosa", "https://github.com/jpedrosa/"] translators: diff --git a/zh-cn/elisp-cn.html.markdown b/zh-cn/elisp-cn.html.markdown index c3a2f927..d303c2e8 100755 --- a/zh-cn/elisp-cn.html.markdown +++ b/zh-cn/elisp-cn.html.markdown @@ -4,7 +4,7 @@ contributors: - ["Bastien Guerry", "http://bzg.fr"]
translators:
- ["Chenbo Li", "http://binarythink.net"]
-filename: learn-emacs-lisp.el
+filename: learn-emacs-lisp-zh.el
lang: zh-cn
---
diff --git a/zh-cn/git-cn.html.markdown b/zh-cn/git-cn.html.markdown index 8c24f0b8..86952eba 100755 --- a/zh-cn/git-cn.html.markdown +++ b/zh-cn/git-cn.html.markdown @@ -5,7 +5,6 @@ contributors: - ["Jake Prather", "http://github.com/JakeHP"] translators: - ["Chenbo Li", "http://binarythink.net"] -filename: LearnGit.txt lang: zh-cn --- diff --git a/zh-cn/haskell-cn.html.markdown b/zh-cn/haskell-cn.html.markdown index cb0de467..8d51f144 100755 --- a/zh-cn/haskell-cn.html.markdown +++ b/zh-cn/haskell-cn.html.markdown @@ -1,6 +1,6 @@ --- language: haskell -filename: learn-haskell.hs +filename: learn-haskell-zh.hs contributors: - ["Adit Bhargava", "http://adit.io"] translators: @@ -404,4 +404,4 @@ qsort (p:xs) = qsort lesser ++ [p] ++ qsort greater 你可以从优秀的 [Learn you a Haskell](http://learnyouahaskell.com/) 或者 [Real World Haskell](http://book.realworldhaskell.org/) -找到优雅不少的入门介绍。
\ No newline at end of file +找到优雅不少的入门介绍。 diff --git a/zh-cn/java-cn.html.markdown b/zh-cn/java-cn.html.markdown index b9ccf61a..9422ac2f 100755 --- a/zh-cn/java-cn.html.markdown +++ b/zh-cn/java-cn.html.markdown @@ -3,7 +3,7 @@ name: java category: language language: java lang: zh-cn -filename: LearnJava.java +filename: LearnJava-zh.java contributors: - ["Jake Prather", "http://github.com/JakeHP"] translators: diff --git a/zh-cn/javascript-cn.html.markdown b/zh-cn/javascript-cn.html.markdown index 3b5cfa94..89fc256e 100755 --- a/zh-cn/javascript-cn.html.markdown +++ b/zh-cn/javascript-cn.html.markdown @@ -2,6 +2,7 @@ language: javascript category: language name: javascript +filename: javascript-zh.js contributors: - ["Adam Brenecki", "http://adam.brenecki.id.au"] translators: diff --git a/zh-cn/php-cn.html.markdown b/zh-cn/php-cn.html.markdown index 3b242ce1..c6ebb515 100755 --- a/zh-cn/php-cn.html.markdown +++ b/zh-cn/php-cn.html.markdown @@ -5,7 +5,7 @@ contributors: - ["Trismegiste", "https://github.com/Trismegiste"] translators: - ["Chenbo Li", "http://binarythink.net"] -filename: learnphp.php +filename: learnphp-zh.php lang: zh-cn --- diff --git a/zh-cn/python-cn.html.markdown b/zh-cn/python-cn.html.markdown index 259e4ed8..764eed54 100755 --- a/zh-cn/python-cn.html.markdown +++ b/zh-cn/python-cn.html.markdown @@ -4,7 +4,7 @@ contributors: - ["Louie Dinh", "http://ldinh.ca"] translators: - ["Chenbo Li", "http://binarythink.net"] -filename: learnpython.py +filename: learnpython-zh.py lang: zh-cn --- diff --git a/zh-cn/racket-cn.html.markdown b/zh-cn/racket-cn.html.markdown new file mode 100644 index 00000000..e17aedda --- /dev/null +++ b/zh-cn/racket-cn.html.markdown @@ -0,0 +1,607 @@ +--- + +language: racket +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 16c0ed67..619e6e92 100644 --- a/zh-cn/ruby-cn.html.markdown +++ b/zh-cn/ruby-cn.html.markdown @@ -1,6 +1,6 @@ --- language: ruby -filename: learnruby.rb +filename: learnruby-zh.rb lang: zh-cn contributors: - ["David Underwood", "http://theflyingdeveloper.com"] @@ -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 diff --git a/zh-cn/scala-cn.html.markdown b/zh-cn/scala-cn.html.markdown index 9f776090..1ce41ac6 100644 --- a/zh-cn/scala-cn.html.markdown +++ b/zh-cn/scala-cn.html.markdown @@ -1,6 +1,6 @@ --- language: Scala -filename: learnscala.scala +filename: learnscala-zh.scala contributors: - ["George Petrov", "http://github.com/petrovg"] - ["Dominic Bou-Samra", "http://dbousamra.github.com"] |