26 lines
No EOL
978 B
Racket
26 lines
No EOL
978 B
Racket
#lang sicp
|
|
|
|
;Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.
|
|
|
|
(define (sum-of-the-squares-of-two-largest x y z) ;define function that will take all input
|
|
(define (squares a) (* a a))
|
|
(cond ((and (<= x y) (<= x z)) (+ (squares y) (squares z)))
|
|
((and (<= y x) (<= y z)) (+ (squares x) (squares z)))
|
|
(else (+ (squares x) (squares y)))
|
|
)
|
|
)
|
|
|
|
(sum-of-the-squares-of-two-largest 4 2 1) ;the result is 20
|
|
|
|
;what if we want the sum of the squares of the smallest nubers???
|
|
|
|
(define (sum-of-the-squares-of-two-smallest x y z) ;define function that will take all input
|
|
(define (squares a) (* a a))
|
|
(cond ((and (>= x y) (>= x z)) (+ (squares y) (squares z)))
|
|
((and (>= y x) (>= y z)) (+ (squares x) (squares z)))
|
|
(else (+ (squares x) (squares y)))
|
|
)
|
|
)
|
|
|
|
(sum-of-the-squares-of-two-smallest 1 5 3) ;the result should be 10
|
|
|