{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

notes45 - The defines that produce the individual shapes...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
The define s that produce the individual shapes look no different from before, but the results are different: Each shape is a function, not a list structure. So to get the area of the shape circle3 we invoke that shape with the proper message: (circle3 ’area) . That notation is a little awkward so we provide a little “syntactic sugar” that allows us to say (area circle3) as in the past: ;;;;; In file cs61a/lectures/2.4/msg.scm (define (operate op obj) (obj op)) (define (area shape) (operate ’area shape)) (define (perimeter shape) (operate ’perimeter shape)) Message passing may seem like an overly complicated way to handle this problem of shapes, but we’ll see next week that it’s one of the key ideas in creating object-oriented programming. Message passing becomes much more powerful when combined with the idea of local state that we’ll learn next week. We seem to have abandoned tagged data; every shape type is just some function, and it’s hard to tell which type of shape a given function represents. We could combine message passing with tagged data, if desired,
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Ask a homework question - tutors are online