21 lines
1,009 B
Racket
21 lines
1,009 B
Racket
#lang sicp
|
|
|
|
;Observe that our model of evaluation allows for combinations whose operators are compound expressions. Use this observation to describe the behavior of the following procedure:
|
|
;(define (a-plus-abs-b a b) ((if (> b 0) + -) a b))
|
|
|
|
(define (a-plus-abs-b a b)
|
|
((if (> b 0) + -) a b)
|
|
)
|
|
|
|
(a-plus-abs-b -6 3) ;--> the result is -3
|
|
(a-plus-abs-b 3 -3) ;--> the result is 6
|
|
(a-plus-abs-b -3 -3) ;--> the result is 0
|
|
(a-plus-abs-b -3 3) ;--> the result is 0
|
|
(a-plus-abs-b -3 0) ;--> the result is -3
|
|
|
|
;This tells us that if b is positive, we use + procedure. If not them use - procedure
|
|
;from (a-plus-abs-b -6 3) --> the result shows -3 --> -6 + (3) = -3 // because b = (3) we use + procedure
|
|
;from (a-plus-abs-b 3 -3) --> the result shows 3 --> 3 - (-3) = 6 // because b = (-3) we use - procedure
|
|
;from (a-plus-abs-b -3 -3) --> the result shows 0 --> -3 - (-3) = 0
|
|
;from (a-plus-abs-b -3 3) --> the result shows 0 --> -3 + (3) = 0
|
|
;from (a-plus-abs-b -3 0) --> the result shows -3 --> -3 + (0) = -3
|