zoukankan      html  css  js  c++  java
  • SICP_2.24-2.29

    这一次的习题让我发现对cons和list的理解完全不是那么回事  参考资料

      1 #lang racket
      2 
      3 ;;;;;;;;;;;;;;;2.24
      4 
      5 (list 1 (list 2 (list 3 4)))
      6 
      7 ;;;;;;;;;;;;;;;2.25
      8 
      9 ;;;;;;;;;;;;(cdr (list 1 2)) 将产生list (car (list 1 2))
     10 ;;;;;;;;;;;;产生integer
     11 ;;;;;;;;;;;;(car '(1 . 2)) -> 1
     12 ;;;;;;;;;;;;(cdr '(1 . 2)) -> 2
     13 ;;;;;;;;;;;;注意序对和表的区别!!!
     14 (car (cdr (car (cdr (cdr '(1 2 (5 7) 9))))))
     15 (car (car '((7))))
     16 (car (cdr (car (cdr (car (cdr (car (cdr (car (cdr (car (cdr
     17                                '(1 (2 (3 (4 (5 (6 7))))))
     18                                ))))))))))))
     19 
     20 ;;;;;;;;;;;;;;2.26
     21 (define x (list 1 2 3))
     22 (define y (list 4 5 6))
     23 
     24 (append x y)
     25 (cons x y)
     26 (list x y)
     27 ;;;;;;;;;;;;2.27
     28 (define (deep-reverse tree)
     29   (cond ((null? tree) '())
     30         ((not (pair? tree)) tree)
     31         (else (append (deep-reverse (cdr tree))
     32                       (list (deep-reverse (car tree)))))))
     33 
     34 (deep-reverse '('(3 4) '(1 2)))
     35 ;;;;;;;;;;;;;;2.28
     36 (define (fringe tree)
     37   (cond ((null? tree) '())
     38         ((not (pair? tree))  (list tree))
     39         (else (append (fringe (car tree))
     40                       (fringe (cdr tree))))))
     41 
     42 (define lis1 (list (list 1 2) (list 3 4)))
     43 
     44 ;;test
     45 (fringe lis1)
     46 (fringe (list lis1 lis1))
     47 
     48 ;;;;;;;;;;;;;;2.29
     49 (define (make-mobile left right)
     50   (list left right))
     51 
     52 (define (make-branch length structure)
     53   (list length structure))
     54 
     55 (define (left-branch mobile)
     56   (car mobile))
     57 (define (right-branch mobile)
     58   (car (cdr mobile)))
     59 
     60 (define (branch-length branch)
     61   (car branch))
     62 (define (branch-structure branch)
     63   (car (cdr branch)))
     64 ;;;;;;;;;;;;;;;;;;;;;;;;;;;思路不清晰时的写法
     65 ;(define (total-weight mobile)
     66  ; (cond ((null? mobile) 0)
     67   ;      ((not (pair? mobile)) mobile)
     68    ;     (else (+ (total-weight (branch-structure (left-branch mobile)))
     69     ;             (total-weight (branch-structure (right-branch mobile)))))))
     70 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
     71 (define (total-weight2 mobile)
     72   (+ (branch-weight (left-branch mobile))
     73      (branch-weight (right-branch mobile))))
     74 
     75 (define (branch-weight branch)
     76   (if (have-mobile? branch)
     77       (total-weight2 (branch-structure branch))
     78       (branch-structure branch)))
     79 
     80 (define (have-mobile? branch)
     81   (pair? (branch-structure branch)))
     82 ;;test
     83 (define mobile (make-mobile (make-branch 10 2)
     84                             (make-branch 10 (make-mobile (make-branch 10 20)
     85                                                       (make-branch 10 20)))))
     86 
     87 (define mobile2 (make-mobile (make-branch 10 20)
     88                             (make-branch 11 20)))
     89 
     90 ;(total-weight2 mobile)
     91 ;(total-weight mobile)
     92 ;(total-weight2 mobile2)
     93 ;(total-weight mobile2)
     94 
     95 ;;;;;;;;;;torque
     96 (define (branch-torque branch)
     97   (* (branch-length branch)
     98      (branch-weight branch)))
     99 
    100 (define (branch-balanced? branch)
    101   (if (pair? (branch-structure branch))
    102       (balanced? (branch-structure branch))
    103       #t))
    104 
    105 (define (balanced? mobile)
    106   (and (= (branch-torque (left-branch mobile))
    107           (branch-torque (right-branch mobile)))
    108        (branch-balanced? (left-branch mobile))
    109        (branch-balanced? (right-branch mobile))))
    110 ;;test
    111 (define a (make-mobile (make-branch 2 2) (make-branch 2 2)))
    112 (define b (make-mobile (make-branch 2 7) (make-branch 6 5)))
    113 
    114 (balanced? a)
    115 (balanced? b)
    116 
    117 ;;;;;;;;;;;;;;;;;(define (make-mobile left right)
    118 ;;;;;;;;;;;;;;;;;;        (cons left right))
    119 ;;;;;;;;;;;;;;;;;(define (right-branch mobile)
    120 ;;;;;;;;;;;;;;;;;;         (cdr branch))
    121 ;;;;;;;;;;;;;;;;;;记住list和cons list总是有个nil的
    Yosoro
  • 相关阅读:
    Android Media Playback 中的MediaPlayer的用法及注意事项(二)
    Android Media Playback 中的MediaPlayer的用法及注意事项(一)
    34. Search for a Range
    33. Search in Rotated Sorted Array
    32. Longest Valid Parentheses
    31. Next Permutation下一个排列
    30. Substring with Concatenation of All Words找出串联所有词的子串
    29. Divide Two Integers
    28. Implement strStr()子串匹配
    27. Remove Element
  • 原文地址:https://www.cnblogs.com/tclan126/p/6399731.html
Copyright © 2011-2022 走看看