Design a function that takes in an integer bigger than or equal to 1 and produces a pipe.

;; fit-together : number -> pipe
(define (fit-together faucet-count) ...)

The pipe should contain faucet-count faucets and the minimum number of joints (and no copper or iron pipes).

(equal? (fit-together 1)
        'faucet)
(equal? (fit-together 2)
        (make-joint 'faucet 'faucet))
(equal? (fit-together 3)
        (make-joint 'faucet (make-joint 'faucet 'faucet)))
(equal? (fit-together 4)
        (make-joint (make-joint 'faucet 'faucet) (make-joint 'faucet 'faucet)))
(equal? (fit-together 8)
        (make-joint
         (make-joint (make-joint 'faucet 'faucet) 
                     (make-joint 'faucet 'faucet))
         (make-joint (make-joint 'faucet 'faucet)
                     (make-joint 'faucet 'faucet))))

Hint: if the number of faucets is even, the best arrangement is a single joint with half of the faucets on each side.

Solution

(define (fit-together faucets)
  (cond
    [(= 1 faucets) 'faucet]
    [(even? faucets) 
     (make-joint (fit-together (quotient faucets 2))
                 (fit-together (quotient faucets 2)))]
    [(odd? faucets) 
     (make-joint (fit-together (quotient faucets 2))
                 (fit-together (+ (quotient faucets 2) 1)))]))