Exercise 2.44: 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.
Higher-order operations
In addition to abstracting patterns of combining painters, we can work at a
higher level, abstracting patterns of combining painter operations. That is,
we can view the painter operations as elements to manipulate and can write
means of combination for these elements—procedures that take painter
operations as arguments and create new painter operations.
For example, flipped-pairs and square-limit each arrange four
copies of a painter’s image in a square pattern; they differ only in how they
orient the copies. One way to abstract this pattern of painter combination is
with the following procedure, which takes four one-argument painter operations
and produces a painter operation that transforms a given painter with those
four operations and arranges the results in a square. Tl, tr,
bl, and br are the transformations to apply to the top left copy,
the top right copy, the bottom left copy, and the bottom right copy,
respectively.
(define (square-of-four tl tr bl br)
(lambda (painter)
(let ((top (beside (tl painter)
(tr painter)))
(bottom (beside (bl painter)
(br painter))))
(below bottom top))))
Then flipped-pairs can be defined in terms of square-of-four as
follows:
(define (flipped-pairs painter)
(let ((combine4
(square-of-four identity
flip-vert
identity
flip-vert)))
(combine4 painter)))
and square-limit can be expressed as
(define (square-limit painter n)
(let ((combine4
(square-of-four flip-horiz
identity
rotate180
flip-vert)))
(combine4 (corner-split painter n))))
练习 2.44:定义 corner-split 所使用的过程 up-split。它与 right-split 类似,只是将 below 和 beside 的角色互换。
高阶操作
除了对组合画家 (painters) 的模式进行抽象,我们还可以在更高层次上对组合画家操作的模式进行抽象。也就是说,我们可以把画家操作视为可被操纵的元素,并为这些元素编写组合的方法——即以画家操作为参数、创建新画家操作的过程。
例如,flipped-pairs 和 square-limit 都将一个画家的图像以四份正方形排列方式布置,它们的区别仅在于如何定向各副本。对这种画家组合模式进行抽象的一种方式是使用下列过程,它接受四个单参数的画家操作,并产生一个画家操作,将给定画家分别用这四个操作变换后,把结果排列成正方形。tl、tr、bl 和 br 分别是应用于左上角、右上角、左下角和右下角副本的变换。
(define (square-of-four tl tr bl br)
(lambda (painter)
(let ((top (beside (tl painter)
(tr painter)))
(bottom (beside (bl painter)
(br painter))))
(below bottom top))))
然后,flipped-pairs 可以用 square-of-four 定义如下:
(define (flipped-pairs painter)
(let ((combine4
(square-of-four identity
flip-vert
identity
flip-vert)))
(combine4 painter)))
square-limit 可以表示为
(define (square-limit painter n)
(let ((combine4
(square-of-four flip-horiz
identity
rotate180
flip-vert)))
(combine4 (corner-split painter n))))
(define (square-of-four tl tr bl br)
(lambda (painter)
(let ((top (beside (tl painter)
(tr painter)))
(bottom (beside (bl painter)
(br painter))))
(below bottom top)))) (define (flipped-pairs painter)
(let ((combine4
(square-of-four identity
flip-vert
identity
flip-vert)))
(combine4 painter))) (define (square-limit painter n)
(let ((combine4
(square-of-four flip-horiz
identity
rotate180
flip-vert)))
(combine4 (corner-split painter n))))