正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 30.1.23 So here is code to do this. For example, the first procedure takes(make .et -c r n) acor vay two vectors and adds them together to get a new vector. It does cor v1) bcor v2》) this by extracting the x and y components of the vectors, adding mke-vect ector eun them separately and then creating a new vector with those C factor (cor vect) values for the components. The second procedure takes a (define (-vect v1 v2) vector and a number, and stretches (or shrinks) the vector by I+vect v1 (scale-vect v2-1)) (define (rotate-vect v angle) that amount (let ('c(cos angle)) Rotation is just a matter of applying some trigonometry to the (s (sin angle))) (make-vect ((c(xcor v) vector to create a new vector a key thing to observe is how we inherit closure from the (+c ecor v)) underlying representation. For example, in +vect, v I could be fs(xcor v)))))) 82 6001S the result of some other +vect operation Slide 30.1.24 Select parts Also note the form: we extract the pieces, do some simpler operations, and then construct a new version of the same type of Compute more primitive ycorlvectn) (define(-vect v1 And notice how this nicely isolates the changes beneath the I+vect v1 (scale-vect v2-1m) abstraction barrier. If we change the abstraction implementation (define (rotate-vectv angle (let ((c (cos angle)) for vectors, none of these procedures need to change! 48m 6001 SICP Slide 30.1.25 Now we can use all these pieces to assemble a picture. The Creating a picture basic idea is to take a list of segments( defined as pairs of vectors using our nice data abstraction). This then gets passed to a procedure that generates a picture, that is, a new procedure with the data embedded within it Note that this make-picture procedure is a higher order procedure, it takes as input a list and creates as output a ocedure 6001 SIC?6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 30.1.23 So here is code to do this. For example, the first procedure takes two vectors and adds them together to get a new vector. It does this by extracting the x and y components of the vectors, adding them separately and then creating a new vector with those values for the components. The second procedure takes a vector and a number, and stretches (or shrinks) the vector by that amount. Rotation is just a matter of applying some trigonometry to the vector to create a new vector. A key thing to observe is how we inherit closure from the underlying representation. For example, in +vect, v1 could be the result of some other +vect operation. Slide 30.1.24 Also note the form: we extract the pieces, do some simpler operations, and then construct a new version of the same type of object. And notice how this nicely isolates the changes beneath the abstraction barrier. If we change the abstraction implementation for vectors, none of these procedures need to change!! Slide 30.1.25 Now we can use all these pieces to assemble a picture. The basic idea is to take a list of segments (defined as pairs of vectors using our nice data abstraction). This then gets passed to a procedure that generates a picture, that is, a new procedure with the data embedded within it. Note that this make-picture procedure is a higher order procedure, it takes as input a list and creates as output a new procedure
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有