exercise 1: figure out the sum of squares of the minimum two arguments
Write a procedure named 'sum-of-square-min-two' which accepts three
arguments and will figure out the sum of squares of the minimum two
arguments.
$ scheme48
> ,load ex1-sum-square-min-two.scm
>
> (sum-of-square-min-two 1 2 3)
5
> (sum-of-square-min-two 1 2 -3)
10
> (sum-of-square-min-two 2 2 2)
8
> ,exit
$
解答:
;figure out the sum of squares of the minimum two arguments
(define (square x)
(* x x))
(define (min-two-square a b c)
(if (<= a b)
(if (<= b c)
(list (square a) (square b))
(list (square a) (square c)))
(if (<= a c )
(list (square b) (square a))
(list (square b) (square c)))))
(define (sum-data x y)
(+ x y))
(define (sum-of-square-min-two a b c)
(apply sum-data (min-two-square a b c)))
執行結果:
沒有留言:
張貼留言