-
Notifications
You must be signed in to change notification settings - Fork 1
/
2.29.tex
44 lines (40 loc) · 1.15 KB
/
2.29.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
\documentclass[a4paper,12pt]{article}
\usepackage{listings}
\lstset{language=Lisp}
\begin{document}
\begin{lstlisting}
(define (make-mobile left right)
(list left right))
(define (make-branch length structure)
(list length structure))
(define (left-branch m) (car m))
(define (right-branch m) (cadr m))
(define (branch-length b) (car b))
(define (branch-structure b) (cadr b))
(define (branch-weight b)
(let ((s (branch-structure b)))
(if (number? s)
s
(total-weight s))))
(define (total-weight m)
(+ (branch-weight (left-branch m))
(branch-weight (right-branch m))))
(define (balanced? m)
(define (torque b)
(* (branch-length b) (branch-weight b)))
(= (torque (left-branch m))
(torque (right-branch m))))
\end{lstlisting}
If we change the representation of mobiles, we only need to redefine
the selectors as follows,
\begin{lstlisting}
(define (make-mobile left right)
(cons left right))
(define (make-branch length structure)
(cons length structure))
(define (left-branch m) (car m))
(define (right-branch m) (cdr m))
(define (branch-length b) (car b))
(define (branch-structure b) (cdr b))
\end{lstlisting}
\end{document}