Printer-friendly versionPrinter-friendly version

The representation of sequences in terms of lists generalizes naturally to represent sequences whose elements may themselves be sequences. For example, we can regard the object ((1 2) 3 4) constructed by

(cons (list 1 2) (list 3 4))

as a list of three items, the first of which is itself a list, (1 2). Indeed, this is suggested by the form in which the result is printed by the interpreter. Figure 2.5 shows the representation of this structure in terms of pairs.

Another way to think of sequences whose elements are sequences is as trees. The elements of the sequence are the branches of the tree, and elements that are themselves sequences are subtrees. Figure 2.6 shows the structure in figure 2.5 viewed as a tree.

Recursion is a natural tool for dealing with tree structures, since we can often reduce operations on trees to operations on their branches, which reduce in turn to operations on the branches of the branches, and so on, until we reach the leaves of the tree. As an example, compare the length procedure of section 2.2.1 with the count-leaves procedure, which returns the total number of leaves of a tree:

(define x (cons (list 1 2) (list 3 4)))

(length x)
3
(count-leaves x)
4

(list x x)
(((1 2) 3 4) ((1 2) 3 4))

(length (list x x))
2

(count-leaves (list x x))
8

To implement count-leaves, recall the recursive plan for computing length:

  • Length of a list x is 1 plus length of the cdr of x.
  • Length of the empty list is 0.

Count-leaves is similar. The value for the empty list is the same:

  • Count-leaves of the empty list is 0.

But in the reduction step, where we strip off the car of the list, we must take into account that the car may itself be a tree whose leaves we need to count. Thus, the appropriate reduction step is

  • Count-leaves of a tree x is count-leaves of the car of x plus count-leaves of the cdr of x.

Finally, by taking cars we reach actual leaves, so we need another base case:

  • Count-leaves of a leaf is 1.

To aid in writing recursive procedures on trees, Scheme provides the primitive predicate pair?, which tests whether its argument is a pair. Here is the complete procedure:[13]

(define (count-leaves x)
  (cond ((null? x) 0)  
        ((not (pair? x)) 1)
        (else (+ (count-leaves (car x))
                 (count-leaves (cdr x))))))

Exercises

Exercise 2.24

in

Suppose we evaluate the expression (list 1 (list 2 (list 3 4))). Give the result printed by the interpreter, the corresponding box-and-pointer structure, and the interpretation of this as a tree (as in figure 2.6).

Exercise 2.25

in

Give combinations of cars and cdrs that will pick 7 from each of the following lists:

(1 3 (5 7) 9)

((7))

(1 (2 (3 (4 (5 (6 7))))))

Exercise 2.26

in

Suppose we define x and y to be two lists:

(define x (list 1 2 3))
(define y (list 4 5 6))

What result is printed by the interpreter in response to evaluating each of the following expressions:

(append x y)

(cons x y)

(list x y)

Exercise 2.27

in

Modify your reverse procedure of exercise 2.18 to produce a deep-reverse procedure that takes a list as argument and returns as its value the list with its elements reversed and with all sublists deep-reversed as well. For example,

(define x (list (list 1 2) (list 3 4)))

x
((1 2) (3 4))

(reverse x)
((3 4) (1 2))

(deep-reverse x)
((4 3) (2 1))

Exercise 2.28

in

Write a procedure fringe that takes as argument a tree (represented as a list) and returns a list whose elements are all the leaves of the tree arranged in left-to-right order. For example,

(define x (list (list 1 2) (list 3 4)))

(fringe x)
(1 2 3 4)

(fringe (list x x))
(1 2 3 4 1 2 3 4)

Exercise 2.29

in

A binary mobile consists of two branches, a left branch and a right branch. Each branch is a rod of a certain length, from which hangs either a weight or another binary mobile. We can represent a binary mobile using compound data by constructing it from two branches (for example, using list):

(define (make-mobile left right)
  (list left right))

A branch is constructed from a length (which must be a number) together with a structure, which may be either a number (representing a simple weight) or another mobile:

(define (make-branch length structure)
  (list length structure))
  1. Write the corresponding selectors left-branch and right-branch, which return the branches of a mobile, and branch-length and branch-structure, which return the components of a branch.
  2. Using your selectors, define a procedure total-weight that returns the total weight of a mobile.
  3. A mobile is said to be balanced if the torque applied by its top-left branch is equal to that applied by its top-right branch (that is, if the length of the left rod multiplied by the weight hanging from that rod is equal to the corresponding product for the right side) and if each of the submobiles hanging off its branches is balanced. Design a predicate that tests whether a binary mobile is balanced.
  4. Suppose we change the representation of mobiles so that the constructors are

    (define (make-mobile left right)
      (cons left right))
    (define (make-branch length structure)
      (cons length structure))

    How much do you need to change your programs to convert to the new representation?

Exercise 2.30

in

Define a procedure square-tree analogous to the square-list procedure of exercise 2.21. That is, square-list should behave as follows:

(square-tree
 (list 1
       (list 2 (list 3 4) 5)
       (list 6 7)))
(1 (4 (9 16) 25) (36 49))

Define square-tree both directly (i.e., without using any higher-order procedures) and also by using map and recursion.

Exercise 2.31

in

Abstract your answer to exercise 2.30 to produce a procedure tree-map with the property that square-tree could be defined as

(define (square-tree tree) (tree-map square tree))

Exercise 2.32

in

We can represent a set as a list of distinct elements, and we can represent the set of all subsets of the set as a list of lists. For example, if the set is (1 2 3), then the set of all subsets is (() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3)). Complete the following definition of a procedure that generates the set of subsets of a set and give a clear explanation of why it works:

(define (subsets s)
  (if (null? s)
      (list nil)
      (let ((rest (subsets (cdr s))))
        (append rest (map <??> rest)))))

Mapping over trees

Just as map is a powerful abstraction for dealing with sequences, map together with recursion is a powerful abstraction for dealing with trees. For instance, the scale-tree procedure, analogous to scale-list of section 2.2.1 takes as arguments a numeric factor and a tree whose leaves are numbers. It returns a tree of the same shape, where each number is multiplied by the factor. The recursive plan for scale-tree is similar to the one for count-leaves:

(define (scale-tree tree factor)
  (cond ((null? tree) nil)
        ((not (pair? tree)) (* tree factor))
        (else (cons (scale-tree (car tree) factor)
                    (scale-tree (cdr tree) factor)))))
(scale-tree (list 1 (list 2 (list 3 4) 5) (list 6 7))
            10)
(10 (20 (30 40) 50) (60 70))

Another way to implement scale-tree is to regard the tree as a sequence of sub-trees and use map. We map over the sequence, scaling each sub-tree in turn, and return the list of results. In the base case, where the tree is a leaf, we simply multiply by the factor:

(define (scale-tree tree factor)
  (map (lambda (sub-tree)
         (if (pair? sub-tree)
             (scale-tree sub-tree factor)
             (* sub-tree factor)))
       tree))

Many tree operations can be implemented by similar combinations of sequence operations and recursion.

[13] The order of the first two clauses in the cond matters, since the empty list satisfies null? and also is not a pair. [back]

Exercises

Exercise 2.24

in

Suppose we evaluate the expression (list 1 (list 2 (list 3 4))). Give the result printed by the interpreter, the corresponding box-and-pointer structure, and the interpretation of this as a tree (as in figure 2.6).

Exercise 2.25

in

Give combinations of cars and cdrs that will pick 7 from each of the following lists:

(1 3 (5 7) 9)

((7))

(1 (2 (3 (4 (5 (6 7))))))

Exercise 2.26

in

Suppose we define x and y to be two lists:

(define x (list 1 2 3))
(define y (list 4 5 6))

What result is printed by the interpreter in response to evaluating each of the following expressions:

(append x y)

(cons x y)

(list x y)

Exercise 2.27

in

Modify your reverse procedure of exercise 2.18 to produce a deep-reverse procedure that takes a list as argument and returns as its value the list with its elements reversed and with all sublists deep-reversed as well. For example,

(define x (list (list 1 2) (list 3 4)))

x
((1 2) (3 4))

(reverse x)
((3 4) (1 2))

(deep-reverse x)
((4 3) (2 1))

Exercise 2.28

in

Write a procedure fringe that takes as argument a tree (represented as a list) and returns a list whose elements are all the leaves of the tree arranged in left-to-right order. For example,

(define x (list (list 1 2) (list 3 4)))

(fringe x)
(1 2 3 4)

(fringe (list x x))
(1 2 3 4 1 2 3 4)

Exercise 2.29

in

A binary mobile consists of two branches, a left branch and a right branch. Each branch is a rod of a certain length, from which hangs either a weight or another binary mobile. We can represent a binary mobile using compound data by constructing it from two branches (for example, using list):

(define (make-mobile left right)
  (list left right))

A branch is constructed from a length (which must be a number) together with a structure, which may be either a number (representing a simple weight) or another mobile:

(define (make-branch length structure)
  (list length structure))
  1. Write the corresponding selectors left-branch and right-branch, which return the branches of a mobile, and branch-length and branch-structure, which return the components of a branch.
  2. Using your selectors, define a procedure total-weight that returns the total weight of a mobile.
  3. A mobile is said to be balanced if the torque applied by its top-left branch is equal to that applied by its top-right branch (that is, if the length of the left rod multiplied by the weight hanging from that rod is equal to the corresponding product for the right side) and if each of the submobiles hanging off its branches is balanced. Design a predicate that tests whether a binary mobile is balanced.
  4. Suppose we change the representation of mobiles so that the constructors are

    (define (make-mobile left right)
      (cons left right))
    (define (make-branch length structure)
      (cons length structure))

    How much do you need to change your programs to convert to the new representation?

Exercise 2.30

in

Define a procedure square-tree analogous to the square-list procedure of exercise 2.21. That is, square-list should behave as follows:

(square-tree
 (list 1
       (list 2 (list 3 4) 5)
       (list 6 7)))
(1 (4 (9 16) 25) (36 49))

Define square-tree both directly (i.e., without using any higher-order procedures) and also by using map and recursion.

Exercise 2.31

in

Abstract your answer to exercise 2.30 to produce a procedure tree-map with the property that square-tree could be defined as

(define (square-tree tree) (tree-map square tree))

Exercise 2.32

in

We can represent a set as a list of distinct elements, and we can represent the set of all subsets of the set as a list of lists. For example, if the set is (1 2 3), then the set of all subsets is (() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3)). Complete the following definition of a procedure that generates the set of subsets of a set and give a clear explanation of why it works:

(define (subsets s)
  (if (null? s)
      (list nil)
      (let ((rest (subsets (cdr s))))
        (append rest (map <??> rest)))))

Comments

OZGyAlVfWvXHkzt

If your articles are always this hlepufl, “I’ll be back.”

giubbotti woolrich

2012woolricharticparka.com 65418 Canada Goose Femme 58672 doudounecanadagoosefrance.fr
Canada Goose Femme 49596 Canada Goose Jacket 64323 canadagoosejacketsaleonline.com
Canada Goose Sale 22614 Canada Goose Outlet 63937 Canada Goose
Canada Goose 14788 woolrich artic parka 36372 Canada Goose Femme
Canada Goose Jacket 26817 Canada Goose 53212 2012newcanadagooseoutlet.com

慶弔スタンプ 安い

いずれにしても篆刻は主に篆書で作品作りをし ますので、楷書の文字とは趣が大きく異なります。次の工程の「校字」で文字同志のバランスを考えながら、選文を行うとよいでしょう。 こ偽造(これは真正な印鑑と見間違えるようなものをいいます。 明らかににせ物とわかるようなものであれば.意味はありませ ん。)されて,その偽造された印鑑で,あなたの重要な財産, 自宅(不動産)や乗用車が他人に売却され,あるいは銀行預#file_links[D:\3text-fb\Projects\fc2\fc2ci.txt,1,N] 金を解約されて弓丨き出されれば,困るのはあなた自身です。 ピッキング犯罪などの多発化にともない1預貯金過誤払被害 は社会問題化しています。
また、これらの線から創り出される印は、皆同じ表情の線から生 まれるのではなく、一 印のうちにあらわれる各点画の表情には、そ れぞれに変化を見せたいものです。私たちの毎日の生活を考えてみ ても、単調よりも変化に満ちた日々でありたいものです。しかし、 あまりに放縦になりすぎると、変化を求めすぎて破綻してしまうこ とにもなります。要は、変化を求めてもなお、全局の調和のとれた状況を生み出す.役印の生地、文字、ボタン式、大きさは依官によって。『明史~輿服誌」に載せて:皇帝は宝璽計17、玉材、玉で箸文、皇後の印章金ボタンで、亀、依周尺方五寸9分、厚さ一寸7分。他の皇太子、プリンセス、親王妃など、世子世子のゴールド?亀ボタン……百官官印、洪武初鋳印刷局規定:“正品、銀印3台、者の3寸の4分、厚さ一寸。正二品、銀印二台、侧に三寸2分、厚さ8分……甚だしきに至っては正八八側から、铜印2寸、厚さ2分5厘;正九九铜印、方から、一寸9分、厚さ2分2厘と、未入流、銅条記が、一寸三分、長2寸五分、厚さ2分一厘は、三品以下、全て直ボタン、九叠篆書。彼は日展常務理事を務め、謙慎書道会#file_links[D:\3text-fb\Projects\fc2\fc2ci.txt,1,N] 総務、読売書道展総務、西冷印社の名誉理事、北斗文会長、日本?印刻家連盟(別名の日本篆刻会)の会長など。その作品は相続い日本芸術院奖、恩賜賞、日本文部大臣賞を、日本の天皇に発表した「勲三等瑞宝章」など。1994年また許可日本芸術院会員になって、日本ではこの篆刻界」は第一人者。

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <hr> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.
  • Adds typographic refinements.

More information about formatting options