■ 🕑 4. Chapter 1 │ SICP chapter 1 │ > Building Abstractions with Procedures │ │ https://mitp-content-server.mit.edu/books/content/sectbyfn/books_pres_0/6515/sicp.zip/full-text/book/book-Z-H-9.html#%_chap_1 │ ├─■ 🕑 13. exercise 1.3 │ │ (define (square x) │ │ (* x x)) │ │ (define (sum-squares x y) │ │ (+ (square x) (square y))) │ │ │ │ (define (sum-greater-squares x y z) │ │ (if (> x y) │ │ (if (> y z) │ │ (sum-squares x y) │ │ (sum-squares x z)) │ │ (if (> x z) │ │ (sum-squares y x) │ │ (sum-squares y z)))) │ │ │ │ (sum-greater-squares 1 2 3) │ │ (sum-greater-squares 4 2 3) │ │ (sum-greater-squares 5 1 0) │ │ │ │ │ └─■ 🕑 14. Excercise 1.3 │ │ My attempt at exercise 1.3. it took me a long time as most of my attempts made code that excluded anything that wasn't the maximum number, so i guess finding the "middle" number is what makes this challenging. my first attempts were checking each variable against the others individually (mistake). if x>y then output x otherwise 0, if x>z output x otherwise 0 then multiply both outputs, repeat for each variable (rather silly in retrospect). After this i decided to only compare to the third variable if the first check failed and directly compute the square if the variable wasn't the minimum. Code below: │ │ │ │ #lang sicp │ │ (define (sicp_ex_1.3 x y z) │ │ (+ │ │ (if (< x y) (if (< x z) 0 (* x x)) (* x x)) │ │ (if (< y x) (if (< y z) 0 (* y y)) (* y y)) │ │ (if (< z y) (if (< z x) 0 (* z z)) (* z z)))) │ │ │ │ (sicp_ex_1.3 1 2 3) │ │ result was 13 so it worked │ │ │ ├─■ 🕑 15. corrected exercise 1.3 │ │ Updated to catch PhatCat's fix -- if x, y, and z are the same │ │ value, the code does not work │ │ │ │ (define (square x) │ │ (* x x)) │ │ (define (sum-squares x y) │ │ (+ (square x) (square y))) │ │ │ │ (define (sum-greater-squares x y z) │ │ (if (> x y) │ │ (if (> y z) │ │ (sum-squares x y) │ │ (sum-squares x z)) │ │ (if (> x z) │ │ (sum-squares y x) │ │ (if (= x y z) │ │ (sum-squares x x) │ │ (sum-squares y z))))) │ │ │ └─■ 🕑 16. fix ex 1.3 │ │ summed all 3 squares if x y z were the same. added a check to just force one variable to zero if all the numbers were equal so theres probably a more elegant solution :P. Code: │ │ │ │ #lang sicp │ │ │ │ (define (square x) (* x x)) │ │ │ │ (define (sicp_ex_1.3 x y z) │ │ (+ │ │ (if (= x y) 0 (if (= x z) 0 │ │ (if (< x y) (if (< x z) 0 (square x)) (squarex)))) │ │ (if (< y x) (if (< y z) 0 (square y)) (square y)) │ │ (if (< z y) (if (< z x) 0 (square z)) (square z)))) │ │ │ │ (sicp_ex_1.3 1 2 3) │ │ (sicp_ex_1.3 2 2 2) │ │ (sicp_ex_1.3 2 1 0) │ │ │ └─■ 🕑 17. epic phail │ Fixed the fix │ │ #lang sicp │ │ (define (square x) (* x x)) │ │ (define (sicp_ex_1.3 x y z) │ (+ │ (if (= x y z) 0 │ (if (< x y) (if (< x z) 0 (square x)) (square x)))) │ (if (< y x) (if (< y z) 0 (square y)) (square y)) │ (if (< z y) (if (< z x) 0 (square z)) (square z))) │ │ (sicp_ex_1.3 1 2 3) │ (sicp_ex_1.3 2 2 2) │ (sicp_ex_1.3 2 1 0) │ └─■ 🕑 18. exercise 1.3 [(define (square a) (* a a))
(define (smallest x y z) (if (< x y) (if (< x z) x z) (if (< y z) y z)))
(define (procedure x y z) (- (+ (square x)(square y)(square z))(square (smallest x y z))))