#lang racket
;;https://www.scheme.com/tspl4/start.html#./start:h4
(let ((x 2))
(+ x 3))
(let ((y 3))
(+ 2 y))
(let ((x 2) (y 3))
(+ x y))
;(let ((var expr) ...) body1 body2 ...)
(+ (* 4 4) (* 4 4))
(let ((a (* 4 4)))
(+ a a))
;中括号经常被用于绑定一个let表达式
(let ([list1 '(a b c)]
[list2 '(d e f)])
(cons (cons (car list1)
(car list2))
(cons (car (cdr list1))
(car (cdr list2)))));构造两个pair而不是两个list
;用于绑定函数
(let ([f +])
(f 2 3))
(let ([f +] [x 2])
(f x 3))
(let ([f +] [x 2] [y 3])
(f x y))
;let的绑定仅在let体内可见
(let ([+ *])
(+ 2 3))
(+ 2 3)
;嵌套let
(let ([a 4] [b -3])
(let ([a-squared (* a a)]
[b-squared (* b b)])
(+ a-squared b-squared)))
;嵌套的let,body体中,只有inner的let绑定可见
;The inner binding for x is said to shadow the outer binding
(let ([x 1])
(let ([x (+ x 1)])
(+ x x)))
;Shadowing may be avoided by choosing different names for variables
(let ([x 1])
(let ([new-x (+ x 1)])
(+ new-x new-x)))
;(+ (- (* 3 a) b) (+ (* 3 a) b))
;(let ([*3a (* 3 a)])
; (+ (- *3a b)
; (+ *3a b)))
(cons (car (list 'a 'b 'c)) (cdr (list 'a 'b 'c)))
(let ([l (list 'a 'b 'c)])
(cons (car l)
(cdr l)))
(let ([x 9])
(* x
(let ([x (/ x 3)])
(+ x x))))
(let ([x 'a] [y 'b])
(list (let ([x 'c]) (cons x y))
(let ([y 'd]) (cons x y))))
(let ([x 'a] [y 'b])
(list (let ([new-x 'c]) (cons new-x y))
(let ([new-y 'd]) (cons x new-y))))
(let ([x '((a b) c)])
(cons (let ([x (cdr x)])
(car x))
(let ([x (car x)])
(cons (let ([x (cdr x)])
(car x))
(cons (let ([x (car x)])
x)
(cdr x))))))
(let ([x '((a b) c)])
(cons (let ([new-x (cdr x)])
(car new-x))
(let ([new-x2 (car x)])
(cons (let ([new-x3 (cdr new-x2)])
(car new-x3))
(cons (let ([new-x4 (car new-x2)])
new-x4)
(cdr new-x2))))))
;lambda
(lambda (x) (+ x x))
;(lambda (var ...) body1 body2 ...)
((lambda (x) (+ x x)) (* 3 4))
;将一个lambda绑定到一个变量上
(let ([double (lambda (x) (+ x x))])
(list (double (* 3 4))
(double (/ 99 11))
(double (- 2 7))))
(let ([double-cons (lambda (x) (cons x x))])
(double-cons 'a))
;they may be collapsed into a single procedure by adding an additional argument
;参数压缩
(let ([double-any (lambda (f x) (f x x))])
(list (double-any + 13)
(double-any cons 'a)))
;lambda和let互相嵌套
(let ([x 'a])
(let ([f (lambda (y) (list x y))])
(f 'b)))
;The answer is that the same bindings that were in effect when the procedure was created are in effect again when the procedure is applied
;创建过程时生效的绑定在应用过程时再次生效
(let ([f (let ([x 'sam])
(lambda (y z) (list x y z)))])
(f 'i 'am))
(let ([f (let ([x 'sam])
(lambda (y z) (list x y z)))])
(let ([x 'not-sam])
(f 'i 'am)))
;lambda 参数
;x没有被()包裹,则x为一个list
;all of the actual parameters are put into a single list and the single variable is bound to this list.
;In the first two examples, the procedure named f accepts any number of arguments.
;These arguments are automatically formed into a list to which the variable x is bound
(let ([f (lambda x x)])
(f 1 2 3 4))
(let ([f (lambda x x)])
(f))
;y为一个list
(let ([g (lambda (x . y) (list x y))])
(g 1 2 3 4))
;z为一个list
(let ([h (lambda (x y . z) (list x y z))])
(h 'a 'b 'c 'd))
(let ([f (lambda (x) x)])
(f 'a))
(let ([f (lambda x x)])
(f 'a))
(let ([f (lambda (x . y) x)])
(f 'a))
(let ([f (lambda (x . y) y)])
(f 'a))
(lambda (f x) (f x))
(lambda (x) (+ x x))
;f: unbound identifier in: f
;(lambda (x y) (f x y))
(lambda (x)
(cons x (f x y)))
|