# scheme4r - （1） 起手式

Scheme 是lisp 的一个方言，简约美观，易于学习和实现，lisp的全称是列表处理器(LISt Processor) 的缩写。

``````;; 数字
1
;; 布尔
#f
;; 字符
#\a``````

``1 + 2``

``+ 1 2``

``1 + 2 + 3 + 4 + 5 + 6``

`` + 1 2 3 4 5 6 ``

``if 条件 表达1 [表达式2]``

``if (> 1 2) 1 2``

``````let max = (x, y) => (
if (x > y) {
x
} else {
y
}
)
let a = max(1,2)``````

``````(define max (lambda (x y) (
if (> x y) x y
)))
(define a (max 1 2))``````

``````((lambda (x y) (
if (> x y) x y
)) 1 2)
``````

``````(define cons (lambda (x y) (
(lambda (f) (f x y))
)))``````

``````(define car (lambda (g) (
(g (lambda (a b) a))
)))``````

" (g (lambda (a b) a))" 这又是什么？ 先用代入替换的方式 ，把g这个变量替换成入参"(lambda (f) (f x y))"

``((lambda (f) (f x y)) (lambda (a b) a))``

``((lambda (a b) a) x y)``

``((lambda (a b) a) 1 2)``

``(car (cons 1 2))``

``(car (lambda(f) (f 1 2)))``

"(car (lambda(f) (f 1 2)))" 会产生一个表达式 “(g (lambda (a b) a))” 将g这个形参替换成实际传入的值 "(lambda(f) (f 1 2))",会得到表示式

``((lambda(f) (f 1 2)) (lambda (a b) a))``

f形参再次替换成实参 "(lambda (a b) a)“ 会得到表示式

``((lambda (a b) a) 1 2)``

``````(define cdr (lambda (g) (
(g (lambda (a b) b))
)))``````

``set! 变量名 表达式``

``````(define cons (lambda (x y) (
(lambda (f) (
f x y (lambda (a) (set! x a)) (lambda (b) (set! y b))
))
)))``````

``````(define car (lambda (g) (
g (lambda (x y sx sy) x)
)))``````

``````(define car (lambda (g) (
g (lambda (x y sx sy) y)
)))``````

``````(define set-car! (lambda (g a) (
g (lambda (x y sx sy) (sx a))
)))``````

``````(define set-car! (lambda (g b) (
g (lambda (x y sx sy) (sy b))
)))``````

``````(
(define c (cons 1 2))
(set-car! c 3)
(car c))``````

1.2k 声望
224 粉丝
0 条评论