层次性结构
2016-05-08 23:27
239 查看
(define (append a b) (if (null? a) b (cons (car a) (append (cdr a) (cons (car a) b))))) (define nil (list)) (define (count-leaves x) (cond ((null? x) 0) ((not (pair? x)) 1) (else (+ (count-leaves (car x)) (count-leaves (cdr x)))))) ;; (define (reverse l) (define (reverse-iter tmp ans) (if (null? tmp) ans (reverse-iter (cdr tmp) (cons (car tmp) ans)))) (reverse-iter l nil)) ;2.27 (define (deep-reverse l) (define (deep-reverse-in tmp ans) (cond ((null? tmp) ans) ((pair? (car tmp)) (deep-reverse-in (cdr tmp) (cons (deep-reverse-in (car tmp) nil) ans))) (else (deep-reverse-in (cdr tmp) (cons (car tmp) ans))))) (deep-reverse-in l nil)) ;2.28 (define (fringe t) (define (fringe-in tmp ans) (cond ((null? tmp) ans) ((not (pair? tmp)) (cons tmp ans)) (else (fringe-in (car tmp) (fringe-in (cdr tmp) ans))))) (fringe-in t nil)) (fringe (list (list 1 (list 5 6 7)) (list 3 4) 8)) ;2.29 (define (make-mobile left right) (cons left right)) (define (make-branch length structure) (cons length structure)) (define (left-branch x) (car x)) (define (right-branch x) (cdr x)) (define (branch-length x) (car x)) (define (branch-structure x) (cdr x)) (define (total-weight t) (if (not (pair? t)) t (+ (total-weight (branch-structure (left-branch t))) (total-weight (branch-structure (right-branch t)))))) (define (mobile-balance t) (or (not (pair? t)) (and (mobile-balance (branch-structure (left-branch t))) (mobile-balance (branch-structure (right-branch t))) (= (* (total-weight (branch-structure (left-branch t))) (branch-length (left-branch t))) (* (total-weight (branch-structure (right-branch t))) (branch-length (right-branch t)))))))
相关文章推荐
- 5678
- HTML5--》details
- 20145331实验五 Java网络编程及安全
- Android-View的手势分发
- Android知识导图-View的手势分发
- matlab 字符串处理
- STL vector的复制
- Dr.Elephant Rest API -【Dr.Elephant系列文章-7】
- Android绘图常用方法汇总
- C:条件判断
- CUDA学习笔记五
- eclipse NDK jni配置
- HTML字体属性
- /etc/ld.so.conf文件
- Hack语言的类型系统
- 解决PLSQL developer 乱码问题
- ToolBar代替Actionbar在AppCompatActivity的使用
- statusBar动画
- 第十周学习进度
- eclipse类、方法添加作者与功能描述注释