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 From 273fa8606b662dbec5b3b0b2fd0d3dfd648e00ab Mon Sep 17 00:00:00 2001 From: Geoffrey Liu Date: Sun, 26 Jun 2016 21:21:13 +0800 Subject: [racket/en] Add more details about Racket (#2278) * Add let* and letrec reference * More elaboration on structs * Add code about predefined car, cdr functions * Mention explicit typing, int to real conversion --- racket.html.markdown | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'racket.html.markdown') diff --git a/racket.html.markdown b/racket.html.markdown index 0fe3f030..96dcaf25 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -114,18 +114,42 @@ some-var ; => 5 "Alice" me) ; => "Bob" +;; let* is like let, but allows you to use previous bindings in creating later bindings +(let* ([x 1] + [y (+ x 1)]) + (* x y)) + +;; finally, letrec allows you to define recursive and mutually recursive functions +(letrec ([is-even? (lambda (n) + (or (zero? n) + (is-odd? (sub1 n))))] + [is-odd? (lambda (n) + (and (not (zero? n)) + (is-even? (sub1 n))))]) + (is-odd? 11)) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 3. Structs and Collections ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Structs +; By default, structs are immutable (struct dog (name breed age)) (define my-pet (dog "lassie" "collie" 5)) my-pet ; => # +; returns whether the variable was constructed with the dog constructor (dog? my-pet) ; => #t +; accesses the name field of the variable constructed with the dog constructor (dog-name my-pet) ; => "lassie" +; You can explicitly declare a struct to be mutable with the #:mutable option +(struct rgba-color (red green blue alpha) #:mutable) +(define burgundy + (rgba-color 144 0 32 1.0)) +(set-color-green! burgundy 10) +(color-green burgundy) ; => 10 + ;;; Pairs (immutable) ;; `cons' constructs pairs, `car' and `cdr' extract the first ;; and second elements @@ -143,6 +167,16 @@ my-pet ; => # ;; and a quote can also be used for a literal list value '(1 2 3) ; => '(1 2 3) +;; Racket has predefined functions on top of car and cdr, to extract parts of a list +(cadr (list 1 2 3)) ; => 2 +(car (cdr (list 1 2 3))) ; => 2 + +(cddr (list 1 2 3)) ; => '(3) +(cdr (cdr (list 1 2 3))) ; => '(3) + +(caddr (list 1 2 3)) ; => 3 +(car (cdr (cdr (list 1 2 3)))) ; => 3 + ;; Can still use `cons' to add an item to the beginning of a list (cons 4 '(1 2 3)) ; => '(4 1 2 3) -- cgit v1.2.3 From 51ce7fda40f3c951b807971587374c56fa9d7916 Mon Sep 17 00:00:00 2001 From: Derek Date: Wed, 30 Nov 2016 15:36:56 -0500 Subject: [racket/en] Expand list section (#2588) * Expand list section * Update backtick --- racket.html.markdown | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'racket.html.markdown') diff --git a/racket.html.markdown b/racket.html.markdown index 96dcaf25..38335bc1 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -164,10 +164,17 @@ my-pet ; => # (cons 1 (cons 2 (cons 3 null))) ; => '(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 +;; a quote can also be used for a literal list value '(1 2 3) ; => '(1 2 3) +;; a quasiquote (represented by the backtick character) with commas +;; can be used to evaluate functions +`(1 ,(+ 1 1) 3) ; => '(1 2 3) -;; Racket has predefined functions on top of car and cdr, to extract parts of a list +;; With lists, car/cdr work slightly differently +(car '(1 2 3)) ; => 1 +(cdr '(1 2 3)) ; => '(2 3) + +;; Racket also has predefined functions on top of car and cdr, to extract parts of a list (cadr (list 1 2 3)) ; => 2 (car (cdr (list 1 2 3))) ; => 2 -- cgit v1.2.3 From 51e14b0e341d5df2fb1bf9174e2c9b7016a4c07c Mon Sep 17 00:00:00 2001 From: Steve Chae Date: Thu, 27 Apr 2017 12:21:10 -0400 Subject: [racket/en] Fix Incorrect Names in Mutable Struct Examples (per Issue #2714) (#2715) * Fix wrong mutable struct namee * Re-added space after ; --- 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 38335bc1..c6b1deba 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -147,8 +147,8 @@ my-pet ; => # (struct rgba-color (red green blue alpha) #:mutable) (define burgundy (rgba-color 144 0 32 1.0)) -(set-color-green! burgundy 10) -(color-green burgundy) ; => 10 +(set-rgba-color-green! burgundy 10) +(rgba-color-green burgundy) ; => 10 ;;; Pairs (immutable) ;; `cons' constructs pairs, `car' and `cdr' extract the first -- cgit v1.2.3