[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
This commit is contained in:
Geoffrey Liu 2016-06-26 21:21:13 +08:00 committed by ven
parent 19ac1e8eeb
commit 273fa8606b
2 changed files with 42 additions and 0 deletions

View File

@ -114,18 +114,42 @@ some-var ; => 5
"Alice" "Alice"
me) ; => "Bob" 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 ;; 3. Structs and Collections
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Structs ;; Structs
; By default, structs are immutable
(struct dog (name breed age)) (struct dog (name breed age))
(define my-pet (define my-pet
(dog "lassie" "collie" 5)) (dog "lassie" "collie" 5))
my-pet ; => #<dog> my-pet ; => #<dog>
; returns whether the variable was constructed with the dog constructor
(dog? my-pet) ; => #t (dog? my-pet) ; => #t
; accesses the name field of the variable constructed with the dog constructor
(dog-name my-pet) ; => "lassie" (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) ;;; Pairs (immutable)
;; `cons' constructs pairs, `car' and `cdr' extract the first ;; `cons' constructs pairs, `car' and `cdr' extract the first
;; and second elements ;; and second elements
@ -143,6 +167,16 @@ my-pet ; => #<dog>
;; and a quote can also be used for a literal list value ;; and a quote can also be used for a literal list value
'(1 2 3) ; => '(1 2 3) '(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 ;; Can still use `cons' to add an item to the beginning of a list
(cons 4 '(1 2 3)) ; => '(4 1 2 3) (cons 4 '(1 2 3)) ; => '(4 1 2 3)

View File

@ -24,6 +24,12 @@ val phone_no = 5551337
val pi = 3.14159 val pi = 3.14159
val negative_number = ~15 (* Yeah, unary minus uses the 'tilde' symbol *) val negative_number = ~15 (* Yeah, unary minus uses the 'tilde' symbol *)
(* Optionally, you can explicitly declare types. This is not necessary as
ML will automatically figure out the types of your values. *)
val diameter = 7926 : int
val e = 2.718 : real
val name = "Bobby" : string
(* And just as importantly, functions: *) (* And just as importantly, functions: *)
fun is_large(x : int) = if x > 37 then true else false fun is_large(x : int) = if x > 37 then true else false
@ -31,6 +37,8 @@ fun is_large(x : int) = if x > 37 then true else false
val tau = 2.0 * pi (* You can multiply two reals *) val tau = 2.0 * pi (* You can multiply two reals *)
val twice_rent = 2 * rent (* You can multiply two ints *) val twice_rent = 2 * rent (* You can multiply two ints *)
(* val meh = 1.25 * 10 *) (* But you can't multiply an int and a real *) (* val meh = 1.25 * 10 *) (* But you can't multiply an int and a real *)
val yeh = 1.25 * (Real.fromInt 10) (* ...unless you explicitly convert
one or the other *)
(* +, - and * are overloaded so they work for both int and real. *) (* +, - and * are overloaded so they work for both int and real. *)
(* The same cannot be said for division which has separate operators: *) (* The same cannot be said for division which has separate operators: *)