add ch1 ex3, plus a similar example
This commit is contained in:
parent
4790e8cae6
commit
4d68d2668a
1 changed files with 26 additions and 0 deletions
26
chapter_1/ex_3.rkt
Normal file
26
chapter_1/ex_3.rkt
Normal file
|
@ -0,0 +1,26 @@
|
|||
#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 we want the sum of 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
|
||||
|
Loading…
Add table
Reference in a new issue