Revision of 2.2.4 Example: A Picture Language from 17 July 2009 - 11:25pm

The revisions let you track differences between multiple versions of a post.

Printer-friendly versionPrinter-friendly version

This section presents a simple language for drawing pictures that illustrates the power of data abstraction and closure, and also exploits higher-order procedures in an essential way. The language is designed to make it easy to experiment with patterns such as the ones in figure 2.9, which are composed of repeated elements that are shifted and scaled.[22] In this language, the data objects being combined are represented as procedures rather than as list structure. Just as cons, which satisfies the closure property, allowed us to easily build arbitrarily complicated list structure, the operations in this language, which also satisfy the closure property, allow us to easily build arbitrarily complicated patterns.

The picture language

When we began our study of programming in section 1.1, we emphasized the importance of describing a language by focusing on the language’s primitives, its means of combination, and its means of abstraction. We’ll follow that framework here.

Part of the elegance of this picture language is that there is only one kind of element, called a painter. A painter draws an image that is shifted and scaled to fit within a designated parallelogram-shaped frame. For example, there’s a primitive painter we’ll call wave that makes a crude line drawing, as shown in figure 2.10. The actual shape of the drawing depends on the frame — all four images in figure 2.10 are produced by the same wave painter, but with respect to four different frames. Painters can be more elaborate than this: The primitive painter called rogers paints a picture of MIT’s founder, William Barton Rogers, as shown in figure 2.11.[23] The four images in figure 2.11 are drawn with respect to the same four frames as the wave images in figure 2.10.

To combine images, we use various operations that construct new painters from given painters. For example, the beside operation takes two painters and produces a new, compound painter that draws the first painter’s image in the left half of the frame and the second painter’s image in the right half of the frame. Similarly, below takes two painters and produces a compound painter that draws the first painter’s image below the second painter’s image. Some operations transform a single painter to produce a new painter. For example, flip-vert takes a painter and produces a painter that draws its image upside-down, and flip-horiz produces a painter that draws the original painter’s image left-to-right reversed.

Figure 2.12 shows the drawing of a painter called wave4 that is built up in two stages starting from wave:

(define wave2 (beside wave (flip-vert wave)))
(define wave4 (below wave2 wave2))

            

(define wave2                         (define wave4
  (beside wave (flip-vert wave)))       (below wave2 wave2))

Figure 2.12:  Creating a complex figure, starting from the wave painter of figure 2.10.

In building up a complex image in this manner we are exploiting the fact that painters are closed under the language’s means of combination. The beside or below of two painters is itself a painter; therefore, we can use it as an element in making more complex painters. As with building up list structure using cons, the closure of our data under the means of combination is crucial to the ability to create complex structures while using only a few operations.

Once we can combine painters, we would like to be able to abstract typical patterns of combining painters. We will implement the painter operations as Scheme procedures. This means that we don’t need a special abstraction mechanism in the picture language: Since the means of combination are ordinary Scheme procedures, we automatically have the capability to do anything with painter operations that we can do with procedures. For example, we can abstract the pattern in wave4 as

(define (flipped-pairs painter)
  (let ((painter2 (beside painter (flip-vert painter))))
    (below painter2 painter2)))

and define wave4 as an instance of this pattern:

(define wave4 (flipped-pairs wave))

We can also define recursive operations. Here’s one that makes painters split and branch towards the right as shown in figures 2.13 and  2.14:

(define (right-split painter n)
  (if (= n 0)
      painter
      (let ((smaller (right-split painter (- n 1))))
        (beside painter (below smaller smaller)))))

           

     right-split n                   corner-split n

Figure 2.13:  Recursive plans for right-split and corner-split.

We can produce balanced patterns by branching upwards as well as towards the right (see exercise 2.44 and figures 2.13 and  2.14):

(define (corner-split painter n)
  (if (= n 0)
      painter
      (let ((up (up-split painter (- n 1)))
            (right (right-split painter (- n 1))))
        (let ((top-left (beside up up))
              (bottom-right (below right right))
              (corner (corner-split painter (- n 1))))
          (beside (below painter top-left)
                  (below bottom-right corner))))))

            

     (right-split wave 4)         (right-split rogers 4)

            

    (corner-split wave 4)         (corner-split rogers 4)

Figure 2.14:  The recursive operations right-split and corner-split applied to the painters wave and rogers. Combining four corner-split figures produces symmetric square-limit designs as shown in figure 2.9.

By placing four copies of a corner-split appropriately, we obtain a pattern called square-limit, whose application to wave and rogers is shown in figure 2.9:

(define (square-limit painter n)
  (let ((quarter (corner-split painter n)))
    (let ((half (beside (flip-horiz quarter) quarter)))
      (below (flip-vert half) half))))

[22] The picture language is based on the language Peter Henderson created to construct images like M.C. Escher’s “Square Limit” woodcut (see Henderson 1982). The woodcut incorporates a repeated scaled pattern, similar to the arrangements drawn using the square-limit procedure in this section. [back]
[23] William Barton Rogers (1804-1882) was the founder and first president of MIT. A geologist and talented teacher, he taught at William and Mary College and at the University of Virginia. In 1859 he moved to Boston, where he had more time for research, worked on a plan for establishing a “polytechnic institute,” and served as Massachusetts’s first State Inspector of Gas Meters.

When MIT was established in 1861, Rogers was elected its first president. Rogers espoused an ideal of “useful learning” that was different from the university education of the time, with its overemphasis on the classics, which, as he wrote, “stand in the way of the broader, higher and more practical instruction and discipline of the natural and social sciences.” This education was likewise to be different from narrow trade-school education. In Rogers’s words:

The world-enforced distinction between the practical and the scientific worker is utterly futile, and the whole experience of modern times has demonstrated its utter worthlessness.

Rogers served as president of MIT until 1870, when he resigned due to ill health. In 1878 the second president of MIT, John Runkle, resigned under the pressure of a financial crisis brought on by the Panic of 1873 and strain of fighting off attempts by Harvard to take over MIT. Rogers returned to hold the office of president until 1881.

Rogers collapsed and died while addressing MIT’s graduating class at the commencement exercises of 1882. Runkle quoted Rogers’s last words in a memorial address delivered that same year:

As I stand here today and see what the Institute is, I call to mind the beginnings of science. I remember one hundred and fifty years ago Stephen Hales published a pamphlet on the subject of illuminating gas, in which he stated that his researches had demonstrated that 128 grains of bituminous coal — ”

Bituminous coal,” these were his last words on earth. Here he bent forward, as if consulting some notes on the table before him, then slowly regaining an erect position, threw up his hands, and was translated from the scene of his earthly labors and triumphs to “the tomorrow of death,” where the mysteries of life are solved, and the disembodied spirit finds unending satisfaction in contemplating the new and still unfathomable mysteries of the infinite future.

In the words of Francis A. Walker (MIT’s third president):

All his life he had borne himself most faithfully and heroically, and he died as so good a knight would surely have wished, in harness, at his post, and in the very part and act of public duty.

[back]

Exercises

Exercise 2.44

in

Define the procedure up-split used by corner-split. It is similar to right-split, except that it switches the roles of below and beside.

Exercise 2.45

in

Right-split and up-split can be expressed as instances of a general splitting operation. Define a procedure split with the property that evaluating

(define right-split (split beside below))
(define up-split (split below beside))

produces procedures right-split and up-split with the same behaviors as the ones already defined.

Exercise 2.46

in

A two-dimensional vector v running from the origin to a point can be represented as a pair consisting of an x-coordinate and a y-coordinate. Implement a data abstraction for vectors by giving a constructor make-vect and corresponding selectors xcor-vect and ycor-vect. In terms of your selectors and constructor, implement procedures add-vect, sub-vect, and scale-vect that perform the operations vector addition, vector subtraction, and multiplying a vector by a scalar:

Exercise 2.47

in

Here are two possible constructors for frames:

(define (make-frame origin edge1 edge2)
  (list origin edge1 edge2))

(define (make-frame origin edge1 edge2)
  (cons origin (cons edge1 edge2)))

For each constructor supply the appropriate selectors to produce an implementation for frames.

Exercise 2.48

in

A directed line segment in the plane can be represented as a pair of vectors — the vector running from the origin to the start-point of the segment, and the vector running from the origin to the end-point of the segment. Use your vector representation from exercise 2.46 to define a representation for segments with a constructor make-segment and selectors start-segment and end-segment.

Exercise 2.49

in
Use segments->painter to define the following primitive painters:

  1. The painter that draws the outline of the designated frame.
  2. The painter that draws an “X” by connecting opposite corners of the frame.
  3. The painter that draws a diamond shape by connecting the midpoints of the sides of the frame.
  4. The wave painter.

Exercise 2.50

in

Define the transformation flip-horiz, which flips painters horizontally, and transformations that rotate painters counterclockwise by 180 degrees and 270 degrees.

Exercise 2.51

in

Define the below operation for painters. Below takes two painters as arguments. The resulting painter, given a frame, draws with the first painter in the bottom of the frame and with the second painter in the top. Define below in two different ways — first by writing a procedure that is analogous to the beside procedure given above, and again in terms of beside and suitable rotation operations (from exercise 2.50).

Exercise 2.52

in

Make changes to the square limit of wave shown in figure 2.9 by working at each of the levels described above. In particular:

  1. Add some segments to the primitive wave painter of exercise 2.49 (to add a smile, for example).
  2. Change the pattern constructed by corner-split (for example, by using only one copy of the up-split and right-split images instead of two).
  3. Modify the version of square-limit that uses square-of-four so as to assemble the corners in a different pattern. (For example, you might make the big Mr. Rogers look outward from each corner of the square.)

Comments

Xorauguynagmqgh

metlixDiomi xaikalitag draibiaNaicky http://usillumaror.com - iziananatt inseveinjergy http://gussannghor.com fluegiolf

http://www.birkenstockstore2013.com

Birkenstock over 200birkenstock outlet

Choose the one with Chaussures Birkenstockthick sole birkenstock pas cherto provide discount birkenstock sandals,birkenstock clogs cheap,birki clogs,shoe clogs,men s birkenstocks, even BMX cyclistsarmani watches are using eh same kind of shoes, since it sticks well in the pedals andarmani watches uk its thick flat shoes is utilized as a brake. Furthermore, the shoes are popular because of its strength. These kinds of shoes are very expensive because of having high quality materialbirkenstock outlet used and the additional features which provide the shoes the advantages as compared to other brands. Also these shoes should be manufactured and designed Unisex Birkenstockaccording to the safety features associated with the sports. The price of these shoes will be according on the features which the shoes have.

http://www.birkenstockstore2013.com

Birkenstock over 200birkenstock outlet

Choose the one with Chaussures Birkenstockthick sole birkenstock pas cherto provide discount birkenstock sandals,birkenstock clogs cheap,birki clogs,shoe clogs,men s birkenstocks, even BMX cyclistsarmani watches are using eh same kind of shoes, since it sticks well in the pedals andarmani watches uk its thick flat shoes is utilized as a brake. Furthermore, the shoes are popular because of its strength. These kinds of shoes are very expensive because of having high quality materialbirkenstock outlet used and the additional features which provide the shoes the advantages as compared to other brands. Also these shoes should be manufactured and designed Unisex Birkenstockaccording to the safety features associated with the sports. The price of these shoes will be according on the features which the shoes have.

Birkenstock sandals

A short Assessment On Water Master WatchesAqua Master Watches carries a personal preference pertaining to high-class types that are fairly altered attractively in to a completely amazing and also unusual choice of watches. armani watches uk Water Master Precious stone enjoy can be uncommon way to notify time. armani watches for women Your dial is big and intensely sorted, how big is true will be decreased to give the person along with total ease and comfort. With all the moving of your time, the necessity of watches has been increased towards the huge degree. To perform increasing demand of those,Armani Watches UK Aqua Get better at Watches have already been introduced with more acceptable actions along with water repellent stainless cases. armani watches for men They are for sale in over A hundred and fifty developments and styles and thus perform the need of individuals involving sporting sophisticated along with top quality watches. An elegant and delightful turquoise get better at watches help customers to achieve the present day search. To deliver involve contemporary and stylish people, Turquoise Learn Watches are generally launched embellished with diamond. Extremely certified as well as extremely skilled workers are mixed up in development of the brand new range decorator watches. birkenstock kairo They’re reviewed with a large amount of celebrities these kinds of 50 Dime, The writer Unces, Ribbon Incredible, essica Simpson , Medical doctor Dre, and much more. This is reality that merely specialist stars, sportsmen too individual the watch. birkenstock boston As an extraordinary willing compound,armani watches for men ceramic is purchased within the watch making business most abundant in the latest systems. Precisely what finest with regards to diamond jewelry is that it might accompany the most up-to-date stylish trend. There are numerous styles and designs that may build diamonds match dissimilar fashion, actually Hiphop. Birkenstock outlet Rap is the fashionable development that many of the youthful technology people comply with. birkenstock france perhaps superstars or perhaps movie superstars might follow the most recent Rap style. Rap jewellery will be in many cases indicated because fancy, extravagant and also catchy add-ons that would as well work as indication of reputation for many. Birkenstock Gizeh This might celebrate or present a person good results and the life high quality these are suffering from. birkenstock pas cher This is not basically regarding exhibiting costly and lavish jewelry piecies, nevertheless is really as properly with regards to enjoying lifestyle. Earthenware may be converted in to a substantial survival and damage resistive material cheap armani watches by making use of the latest systems. birkenstock paris For that reason, Ceramic Watches tend to be intensely searched soon after for grace, class and strength. birkenstock mayari In addition Turquoise Get better at girls Watches tend to be well-known for his or her precision and also supporting fashion. Turquoise Learn girls Watches tend to be comprehensive intermix vintage and also modern day design using the power of Aqua Grasp Watches. birkenstock kairo

http://www.armaniwatchesukgather.org.uk/
http://www.birkenstockstore2013.com
http://www.premierchaussonsfrance.fr
http://www.odg.cat/armani-watches.html
http://www.odg.cat/birkenstockfrench.html

beats by dre clearance

cheap monster beats Before I tell you about my experience, cheap monster beats let me tell you a bit more about these headphones. beats by dre solo According to the information I’ve read, Dr. beats by dre solo Dre worked with Monster Cable and Robert Brunner, dre beats an industrial designer, dre beats for more than two years to perfect these headphones. beats by dre clearance In Dr. Dre’s words,People aren’t hearing all the music beats by dre clearance.

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