From f542ed357d6057ee0cf54efea32590c9f29a442b Mon Sep 17 00:00:00 2001 From: "Duong H. Nguyen" Date: Sun, 5 Jan 2014 21:25:12 +0700 Subject: Add input/output section for Racket --- racket.html.markdown | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'racket.html.markdown') diff --git a/racket.html.markdown b/racket.html.markdown index adacd91d..8c15d447 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -6,6 +6,7 @@ contributors: - ["th3rac25", "https://github.com/voila"] - ["Eli Barzilay", "https://github.com/elibarzilay"] - ["Gustavo Schmidt", "https://github.com/gustavoschmidt"] + - ["Duong H. Nguyen", "https://github.com/cmpitg"] --- Racket is a general purpose, multi-paradigm programming language in the Lisp/Scheme family. @@ -600,6 +601,45 @@ vec ; => #(1 2 3 4) ;; expected: positive? ;; given: -5 ;; more details.... + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 11. Input & output +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; Racket has this concept of "port", which is very similar to file descriptor +;; in other languages + +;; Open "/tmp/tmp.txt" and write "Hello World" +;; This would trigger an error if the file's already existed +(define out-port (open-output-file "/tmp/tmp.txt")) +(displayln "Hello World" out-port) +(close-output-port out-port) + +;; Append to "/tmp/tmp.txt" +(define out-port (open-output-file "/tmp/tmp.txt" + #:exists 'append)) +(displayln "Hola mundo" out-port) +(close-output-port out-port) + +;; Read from the file again +(define in-port (open-input-file "/tmp/tmp.txt")) +(displayln (read-line in-port)) +; => "Hello World" +(displayln (read-line in-port)) +; => "Hola mundo" +(close-input-port in-port) + +;; Alternatively, with call-with-output-file you don't need to explicitly +;; close the file +(call-with-output-file "/tmp/tmp.txt" + #:exists 'update ; Rewrite the content + (λ (out-port) + (displayln "World Hello!" out-port))) + +;; And call-with-input-file does the same thing for input +(call-with-input-file "/tmp/tmp.txt" + (λ (in-port) + (displayln (read-line in-port)))) ``` ## Further Reading -- cgit v1.2.3 From 17f4ff8588acfa4a0306f01b56de7abe4d9d9c89 Mon Sep 17 00:00:00 2001 From: "Duong H. Nguyen" Date: Sun, 5 Jan 2014 21:29:27 +0700 Subject: Fix Racket style --- racket.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'racket.html.markdown') diff --git a/racket.html.markdown b/racket.html.markdown index 8c15d447..856fa75c 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -617,7 +617,7 @@ vec ; => #(1 2 3 4) ;; Append to "/tmp/tmp.txt" (define out-port (open-output-file "/tmp/tmp.txt" - #:exists 'append)) + #:exists 'append)) (displayln "Hola mundo" out-port) (close-output-port out-port) -- cgit v1.2.3 From 6801c7056d0e2d2eac269ff81a969c29775f6e4a Mon Sep 17 00:00:00 2001 From: "Duong H. Nguyen" Date: Sun, 5 Jan 2014 21:37:35 +0700 Subject: Improve language usage in Racket guide --- racket.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'racket.html.markdown') diff --git a/racket.html.markdown b/racket.html.markdown index 856fa75c..eddc00bf 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -606,8 +606,8 @@ vec ; => #(1 2 3 4) ;; 11. Input & output ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Racket has this concept of "port", which is very similar to file descriptor -;; in other languages +;; Racket has this concept of "port", which is very similar to file +;; descriptors in other languages ;; Open "/tmp/tmp.txt" and write "Hello World" ;; This would trigger an error if the file's already existed -- cgit v1.2.3 From f2575c7f3835eba041b525d57a2049a062617581 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Thu, 20 Mar 2014 12:37:13 -0500 Subject: Fix typos from issue 447. --- racket.html.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'racket.html.markdown') diff --git a/racket.html.markdown b/racket.html.markdown index eddc00bf..6abc8759 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -196,7 +196,7 @@ my-pet ; => # (hash-ref m 'd 0) ; => 0 ;; Use `hash-set' to extend an immutable hash table -;; (Returns the extended hash instdead of mutating it) +;; (Returns the extended hash instead of mutating it) (define m2 (hash-set m 'd 4)) m2 ; => '#hash((b . 2) (a . 1) (d . 4) (c . 3)) @@ -224,7 +224,7 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) <-- no `d' (define hello-world (lambda () "Hello World")) (hello-world) ; => "Hello World" -;; You can shorten this using the function definition syntatcic sugae: +;; You can shorten this using the function definition syntactic sugar: (define (hello-world2) "Hello World") ;; The () in the above is the list of arguments for the function @@ -496,7 +496,7 @@ vec ; => #(1 2 3 4) ;; 8. Classes and Objects ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Create a class fish% (-% is idomatic for class bindings) +;; Create a class fish% (-% is idiomatic for class bindings) (define fish% (class object% (init size) ; initialization argument @@ -552,7 +552,7 @@ vec ; => #(1 2 3 4) (set! i (add1 i)))) ;; Macros are hygienic, you cannot clobber existing variables! -(define-syntax-rule (swap! x y) ; -! is idomatic for mutation +(define-syntax-rule (swap! x y) ; -! is idiomatic for mutation (let ([tmp x]) (set! x y) (set! y tmp))) -- cgit v1.2.3 From 8f904480c02a28b966ef8827d71bae534778995f Mon Sep 17 00:00:00 2001 From: Keyan Zhang Date: Fri, 13 Mar 2015 14:04:20 -0400 Subject: detailed explanation of eq?, eqv?, and equal? References: 1. http://docs.racket-lang.org/reference/booleans.html 2. http://docs.racket-lang.org/reference/eval-model.html 3. https://groups.google.com/forum/#!topic/plt-scheme/T1k49HMl450 4. http://stackoverflow.com/questions/16299246/what-is-the-difference-between-eq-eqv-equal-and-in-scheme 5. http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-14.html --- racket.html.markdown | 48 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) (limited to 'racket.html.markdown') diff --git a/racket.html.markdown b/racket.html.markdown index 6abc8759..e345db8b 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -7,6 +7,7 @@ contributors: - ["Eli Barzilay", "https://github.com/elibarzilay"] - ["Gustavo Schmidt", "https://github.com/gustavoschmidt"] - ["Duong H. Nguyen", "https://github.com/cmpitg"] + - ["Keyan Zhang", "https://github.com/keyanzhang"] --- Racket is a general purpose, multi-paradigm programming language in the Lisp/Scheme family. @@ -282,16 +283,49 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) <-- no `d' ;; for numbers use `=' (= 3 3.0) ; => #t -(= 2 1) ; => #f +(= 2 1) ; => #f + +;; `eq?' returns #t if 2 arguments refer to the same object (in memory), +;; #f otherwise. +;; In other words, it's a simple pointer comparison. +(eq? '() '()) ; => #t, since there exists only one empty list in memory +(let ([x '()] [y '()]) + (eq? x y)) ; => #t, same as above -;; for object identity use `eq?' -(eq? 3 3) ; => #t -(eq? 3 3.0) ; => #f (eq? (list 3) (list 3)) ; => #f +(let ([x (list 3)] [y (list 3)]) + (eq? x y)) ; => #f — not the same list in memory! + +(let* ([x (list 3)] [y x]) + (eq? x y)) ; => #t, since x and y now point to the same stuff + +(eq? 'yes 'yes) ; => #t +(eq? 'yes 'no) ; => #f + +(eq? 3 3) ; => #t — be careful here + ; It’s better to use `=' for number comparisons. +(eq? 3 3.0) ; => #f + +(eq? (expt 2 100) (expt 2 100)) ; => #f +(eq? (integer->char 955) (integer->char 955)) ; => #f + +(eq? (string-append "foo" "bar") (string-append "foo" "bar")) ; => #f + +;; `eqv?' supports the comparison of number and character datatypes. +;; for other datatypes, `eqv?' and `eq?' return the same result. +(eqv? 3 3.0) ; => #f +(eqv? (expt 2 100) (expt 2 100)) ; => #t +(eqv? (integer->char 955) (integer->char 955)) ; => #t + +(eqv? (string-append "foo" "bar") (string-append "foo" "bar")) ; => #f -;; for collections use `equal?' -(equal? (list 'a 'b) (list 'a 'b)) ; => #t -(equal? (list 'a 'b) (list 'b 'a)) ; => #f +;; `equal?' supports the comparison of the following datatypes: +;; strings, byte strings, pairs, mutable pairs, vectors, boxes, +;; hash tables, and inspectable structures. +;; for other datatypes, `equal?' and `eqv?' return the same result. +(equal? 3 3.0) ; => #f +(equal? (string-append "foo" "bar") (string-append "foo" "bar")) ; => #t +(equal? (list 3) (list 3)) ; => #t ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 5. Control Flow -- cgit v1.2.3 From 960ee4a1856db8eadb96277bb2422edfa8f2a81c Mon Sep 17 00:00:00 2001 From: Gabriel Halley Date: Wed, 7 Oct 2015 23:11:24 -0400 Subject: removing whitespace all over --- racket.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'racket.html.markdown') diff --git a/racket.html.markdown b/racket.html.markdown index e345db8b..0fe3f030 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -285,7 +285,7 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) <-- no `d' (= 3 3.0) ; => #t (= 2 1) ; => #f -;; `eq?' returns #t if 2 arguments refer to the same object (in memory), +;; `eq?' returns #t if 2 arguments refer to the same object (in memory), ;; #f otherwise. ;; In other words, it's a simple pointer comparison. (eq? '() '()) ; => #t, since there exists only one empty list in memory @@ -320,7 +320,7 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) <-- no `d' (eqv? (string-append "foo" "bar") (string-append "foo" "bar")) ; => #f ;; `equal?' supports the comparison of the following datatypes: -;; strings, byte strings, pairs, mutable pairs, vectors, boxes, +;; strings, byte strings, pairs, mutable pairs, vectors, boxes, ;; hash tables, and inspectable structures. ;; for other datatypes, `equal?' and `eqv?' return the same result. (equal? 3 3.0) ; => #f -- cgit v1.2.3