(define greatest-triangular-number-smaller-than (lambda (n) (define iter (lambda (i ti) (if (<= n (+ i ti)) ti (iter (+ i 1) (+ ti i))))) (iter 1 0))) (define smallest-triangular-number-not-smaller-than (lambda (n) (define iter (lambda (i ti) (if (>= ti n) ti (iter (+ i 1) (+ ti i))))) (iter 1 0))) ;; bijectively map a nonnegative integral point (x, y) in 2-space ;; to a positive integer (define point->number (lambda (x y) (+ (/ (* (+ x y) (+ x y 1)) 2) x 1))) ;; find the x-component under the mapping point->number (define x-component-of (lambda (n) (- (- n (greatest-triangular-number-smaller-than n)) 1))) ;; find the y-component under the mapping point->number (define y-component-of (lambda (n) (- (smallest-triangular-number-not-smaller-than n) n))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Example procedures that make use of the above mapping ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define make-game-state point->number) (define size-of-pile (lambda (game-state pile-number) (if (= pile-number 1) (x-component-of game-state) (y-component-of game-state))))