Advertisement
timothy235

sicp-2-2-2-hierarchical-structures

Feb 20th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 6.16 KB | None | 0 0
  1. #lang racket
  2.  
  3. ;;;;;;;;;;
  4. ;; 2.24 ;;
  5. ;;;;;;;;;;
  6.  
  7. (list 1 (list 2 (list 3 4)))
  8. ;; '(1 (2 (3 4)))
  9.  
  10. ;; Note that, at each nesting level, there is one cons cell for each list element.
  11. ;; If the list element is an atom, the car points to the atom.  If the list
  12. ;; element is a sub-list, the car points to a new cons cell.
  13.  
  14. ;; car | cdr ----> car | cdr ----> empty
  15.  ;; |               |
  16.  ;; 1              car | cdr ----> car | cdr ----> empty
  17.                  ;; |               |
  18.                  ;; 2              car | cdr ----> car | cdr ----> empty
  19.                                  ;; |               |
  20.                                  ;; 3               4
  21.  
  22. ;; (list 1 (list 2 (list 3 4)))
  23.     ;; |       \
  24.     ;; 1       (list 2 (list 3 4))
  25.                 ;; |       \
  26.                 ;; 2       (list 3 4)
  27.                             ;; |   \
  28.                             ;; 3    4
  29.  
  30. ;;;;;;;;;;
  31. ;; 2.25 ;;
  32. ;;;;;;;;;;
  33.  
  34. ;; Note that a car composed with n - 1 cdr's selects the n-th element of a list.
  35.  
  36. (define nl1 (list 1 3 (list 5 7) 9))
  37. (car (cdr (car (cdr (cdr nl1)))))
  38. ;; 7
  39. (second (third nl1))
  40. ;; 7
  41.  
  42. (define nl2 (list (list 7)))
  43. (car  (car nl2))
  44. ;; 7
  45. (first (first nl2))
  46. ;; 7
  47.  
  48. (define nl3 (list 1 (list 2 (list 3 (list 4 (list 5 (list 6 7)))))))
  49. (car (cdr (car (cdr (car (cdr (car (cdr (car (cdr (car (cdr nl3))))))))))))
  50. ;; 7
  51. (second (second (second (second (second (second nl3))))))
  52. ;; 7
  53.  
  54. ;;;;;;;;;;
  55. ;; 2.26 ;;
  56. ;;;;;;;;;;
  57.  
  58. (define x (list 1 2 3))
  59. (define y (list 4 5 6))
  60.  
  61. (append x y)
  62. ;; '(1 2 3 4 5 6)
  63. (cons x y)
  64. ;; '((1 2 3) 4 5 6)
  65. (list x y)
  66. ;; '((1 2 3) (4 5 6))
  67.  
  68. ;;;;;;;;;;
  69. ;; 2.27 ;;
  70. ;;;;;;;;;;
  71.  
  72. (define (my-reverse lst)
  73.   (define (iter old-lst new-lst)
  74.     (if (empty? old-lst)
  75.       new-lst
  76.       (iter (rest old-lst) (cons (first old-lst) new-lst))))
  77.   (iter lst empty))
  78.  
  79. (my-reverse (list (list 1 2) (list 3 4)))
  80. ;; '((3 4) (1 2))
  81.  
  82. (define (deep-reverse lst)
  83.   (define (iter old-lst new-lst)
  84.     (cond [(empty? old-lst) new-lst]
  85.           [(pair? (first old-lst))
  86.            (iter (rest old-lst)
  87.                  (cons (deep-reverse (first old-lst)) new-lst))]
  88.           [else (iter (rest old-lst)
  89.                       (cons (first old-lst) new-lst))]))
  90.   (iter lst empty))
  91.  
  92. (deep-reverse (list (list 1 2) (list 3 4)))
  93. ;; '((4 3) (2 1))
  94.  
  95. ;;;;;;;;;;
  96. ;; 2.28 ;;
  97. ;;;;;;;;;;
  98.  
  99. ;; Use recursion instead of iteration to keep the order.
  100. ;; Use append for sub-lists instead of cons to flatten the list.
  101.  
  102. (define (fringe lst)
  103.   (cond [(empty? lst) empty]
  104.         [(pair? (first lst))
  105.          (append (fringe (first lst)) (fringe (rest lst)))]
  106.         [else (cons (first lst) (fringe (rest lst)))]))
  107.  
  108. (fringe (list (list 1 2) (list 3 4)))
  109. ;; '(1 2 3 4)
  110.  
  111. ;;;;;;;;;;
  112. ;; 2.29 ;;
  113. ;;;;;;;;;;
  114.  
  115. (define (make-mobile left right) (list left right))
  116. (define (make-branch len str) (list len str))
  117. (define (left-branch mobile) (first mobile))
  118. (define (right-branch mobile) (second mobile))
  119. (define (branch-length branch) (first branch))
  120. (define (branch-structure branch) (second branch)) ; can be a weight or a mobile
  121.  
  122. (define (is-branch-mobile? branch)
  123.   (list? (branch-structure branch)))
  124.  
  125. (define (total-branch-weight branch)
  126.   (if (is-branch-mobile? branch)
  127.     (total-weight (branch-structure branch))
  128.     (branch-structure branch)))
  129.  
  130. (define (total-weight mobile)
  131.   (+ (total-branch-weight (left-branch mobile))
  132.      (total-branch-weight (right-branch mobile))))
  133.  
  134. (define (branch-balanced? branch)
  135.   (if (is-branch-mobile? branch)
  136.     (balanced? (branch-structure branch))
  137.     #t))
  138.  
  139. (define (balanced? mobile)
  140.   (and (branch-balanced? (left-branch mobile))
  141.        (branch-balanced? (right-branch mobile))
  142.        (= (* (branch-length (left-branch mobile))
  143.              (total-branch-weight (left-branch mobile)))
  144.           (* (branch-length (right-branch mobile))
  145.              (total-branch-weight (right-branch mobile))))))
  146.  
  147. (define mobile1
  148.   (make-mobile (make-branch 1 2)
  149.                (make-branch 2 1)))
  150. (total-weight mobile1)
  151. ;; 3
  152. (balanced? mobile1)
  153. ;; #t
  154.  
  155. (define mobile2
  156.   (make-mobile (make-branch 1 3)
  157.                (make-branch 2 2)))
  158. (total-weight mobile2)
  159. ;; 5
  160. (balanced? mobile2)
  161. ;; #f
  162.  
  163. (define mobile3
  164.   (make-mobile (make-branch 3 2)
  165.                (make-branch 2 mobile1)))
  166. (total-weight mobile3)
  167. ;; 5
  168. (balanced? mobile3)
  169. ;; #t
  170.  
  171. ;; If we changed the mobile and branch constructors to use cons instead of list,
  172. ;; then we would have to change the selectors to use first and rest instead of
  173. ;; first and second.  Nothing else would have to change.
  174.  
  175. ;;;;;;;;;;
  176. ;; 2.30 ;;
  177. ;;;;;;;;;;
  178.  
  179. (define (square-tree tree)
  180.   (cond [(empty? tree) empty]
  181.         [(pair? (first tree))
  182.          (cons (square-tree (first tree))
  183.                (square-tree (rest tree)))]
  184.         [else (cons (sqr (first tree))
  185.                     (square-tree (rest tree)))]))
  186.  
  187. (square-tree (list (list 1 2) (list 3 4)))
  188. ;; '((1 4) (9 16))
  189.  
  190. (define (square-tree2 tree)
  191.   (map (lambda (sub-tree)
  192.          (if (pair? sub-tree)
  193.            (square-tree2 sub-tree)
  194.            (sqr sub-tree)))
  195.        tree))
  196.  
  197. (square-tree2 (list (list 1 2) (list 3 4)))
  198. ;; '((1 4) (9 16))
  199.  
  200. ;;;;;;;;;;
  201. ;; 2.31 ;;
  202. ;;;;;;;;;;
  203.  
  204. (define (tree-map proc tree)
  205.   (map (lambda (sub-tree)
  206.          (if (pair? sub-tree)
  207.            (tree-map proc sub-tree)
  208.            (proc sub-tree)))
  209.        tree))
  210.  
  211. (define (square-tree3 tree) (tree-map sqr tree))
  212.  
  213. (square-tree3 (list (list 1 2) (list 3 4)))
  214. ;; '((1 4) (9 16))
  215.  
  216. ;;;;;;;;;;
  217. ;; 2.32 ;;
  218. ;;;;;;;;;;
  219.  
  220. (define (subsets s)
  221.   (cond [(empty? s) (list empty)]
  222.         [else
  223.           (define rst (subsets (rest s)))
  224.           (append rst
  225.                   (map (lambda (st) (cons (first s) st))
  226.                        rst))]))
  227.  
  228. ;; For any element x of a non-empty set, the set of all subsets can be evenly
  229. ;; split into two collections, those subsets that contain x and those that don't.
  230. ;; Furthermore, the collection of subsets that do contain x looks exactly like
  231. ;; the collection of subsets that do not contain x, except that each has had x
  232. ;; added to it.
  233.  
  234. (subsets (list 1 2))
  235. ;; '(() (2) (1) (1 2))
  236.  
  237. (subsets (list 1 2 3))
  238. ;; '(() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement