list标准函数的模拟

;反序 (  ) -> (  )
(define (rvs x)
(let recur ((x x)(res '()))
(if (null? x)
res
(recur (cdr x) (cons (car x) res))))) ;长度 ( ) ->
(define (len x)
(let recur ((x x)(y ))
(if (null? x)
y
(recur (cdr x) (+ y ))))) ;合并列表 (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 )))) ;引用 ( ) ->
(define (ref x n)
(if (= n)
(car x)
(ref (cdr x) (- n )))) ;成员查询 ( ) -> ( )
(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 '(1 2 3 4))
(mem '(1 2 3 4))
(assv '((2 3) (5 7) (11 13)))
(asq '((2 3) (5 7) (11 13)))

结果:

(       )
( ) ( )
( )
( )
( ) ( )
( )
( )
( )
>
上一篇:原码 补码 反码


下一篇:protobuf序列化算法原理