list标准函数的模拟
;反序 (1 2 3) -> (3 2 1) (define (rvs x) (let recur ((x x)(res '())) (if (null? x) res (recur (cdr x) (cons (car x) res))))) ;长度 (1 2 3) -> 3 (define (len x) (let recur ((x x)(y 0)) (if (null? x) y (recur (cdr x) (+ y 1))))) ;合并列表 (x ...) (y ...) -> (x ... y ...) (define (apd x y) (let recur ((x x)) (if (null? x) y (cons (car x) (recur (cdr x)))))) ;切割教程版 (define (tail x n) (if (zero? n) x (tail (cdr x) (- n 1)))) ;引用 (1 2 3 4) 2 -> 3 (define (ref x n) (if (= 0 n) (car x) (ref (cdr x) (- n 1)))) ;成员查询 2 (1 2 2 3) -> (2 2 3) (define (mem i x) (let recur ((x x)) (if (null? x) #f (if (equal? (car x) i) x (recur (cdr x)))))) ;类字典查询 k2 ((k1 v1)(k2 v2)(k3 v3)...) ->(k2 v2) (define (asq k x) (let recur ((x x)) (if (null? x) #f (if (equal? (caar x) k) (car x) (recur (cdr x)))))) (define x '(1 2 3 4)) (define y '(5 6 7 8)) (append x y) (apd x y) (length x) (len x) (reverse x) (rvs x) (list-tail '(1 2 3) 1) (tail '(1 2 3) 1) (list-ref '(1 2 3) 2) (ref '(1 2 3) 2) (member 2 '(1 2 3 4)) (mem 2 '(1 2 3 4)) (assv 5 '((2 3) (5 7) (11 13))) (asq 5 '((2 3) (5 7) (11 13)))
结果:
(1 2 3 4 5 6 7 8) (1 2 3 4 5 6 7 8) 4 4 (4 3 2 1) (4 3 2 1) (2 3) (2 3) 3 3 (2 3 4) (2 3 4) (5 7) (5 7) >