SICP Exercise 2.45

Question

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 behaviours as the ones already defined.

Answer

Here is a split implementation that uses a helper function called split-recursive to return a lambda function that will either perform a right or up split.

(define (split λ-a λ-b)
   (define (split-recursive painter n)
       (if (= n 0)
           painter
           (let ((smaller (split-recursive painter (- n 1))))
             (λ-a painter (λ-b smaller smaller)))))
  (λ (painter n) (split-recursive painter n)))