; compute the sum of a list of numbers lon ; examples: ; (sum '(2 4 8 2)) -> 16 ; (sum '()) -> 0 (define (sum lon) (if (null? lon) 0 (+ (car lon) (sum (cdr lon))))) ; compute the sum of squares for a list of numbers lon ; examples ; (sum-of-squares '()) -> 0 ; (sum-of-squares '(3 4)) -> 25 ; (sum-of-squares '(2 2 2 2)) -> 16 (define (sum-of-squares lon) (if (null? lon) 0 (+ (* (car lon) (car lon)) (sum-of-squares (cdr lon))))) ; compute the length of a diagonal for an nd box with ; dimensions given by list of numbers ; dimensions-of-box ; examples ; (length-of-diagonal '(3 4)) -> 5 ; (length-of-diagonal '(2 2 2 2)) -> 4 (define (length-of-diagonal dimensions-of-box) (sqrt (sum-of-squares dimensions-of-box))) ; switch is either 'on or 'off ; (flip switch) is what you get when you flip ; examples ; (flip 'on) -> 'off ; (flip 'off) -> 'on (define (flip switch) (if (eq? 'off switch) 'on 'off)) ; determine the state of a light bulb controlled ; by n hotel switches linked together, assuming ; the switches are in the states specified by ; list los ; computes nary xor (parity) ; examples ; (light-bulb-state '(on)) -> 'on ; (light-bulb-state '(off on)) -> 'on ; (light-bulb-state '(on on)) -> 'off ; (light-bulb-state '(off on off on on)) -> 'on (define (light-bulb-state los) (if (null? los) 'off (let ((state (light-bulb-state (cdr los)))) (if (eq? 'on (car los)) (flip state) state))))