exercise-4.scm (746B)
1 (define-library (sicp solutions chapter-1 exercise-4) 2 (import (scheme base)) 3 (import (srfi srfi-64)) 4 (export 5 a-plus-abs-b 6 a-plus-abs-b-2) 7 8 (begin 9 (define (a-plus-abs-b a b) 10 ((if (> b 0) + -) a b)) 11 12 ;; The result of the "if" expression will be either the procedure "+" or 13 ;; the procedure "-", which is then applied to "a" and "b". 14 15 ;; a-plus-abs-b, when b > 0, is (+ a b), which is equivalent to (+ 16 ;; a (+ b)) 17 ;; 18 ;; when b = 0, (- a 0), which is equivalent to (+ a 0) 19 ;; 20 ;; and a when b <= 0, (- a b), equivalent to (+ a (- b)) 21 22 ;; Considering the definition of the absolute function, we could 23 ;; replace a-plus-abs-b with: 24 25 (define (a-plus-abs-b-2 a b) 26 (+ a (abs b)))))