Advertisement
illiquid

Exercise 1.3

May 28th, 2023 (edited)
1,650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 0.82 KB | Source Code | 0 0
  1. #lang sicp
  2.  
  3. ;Exercise 1.3: Define a procedure that takes three numbers as arguments
  4. ;and returns the sum of the squares of the two larger numbers.
  5.  
  6. (define (square x) (* x x))
  7. ;sum of squares
  8. (define (sumOfSquares a b) (+ (square a) (square b)))
  9.  
  10. (define (sumTwoLargestSquares op1 op2 op3)
  11.  
  12.   ;compare the three numbers and get the largest number
  13.   (define (largestNumber x y z)
  14.     (cond((and(> x y) (> x z)) x)
  15.        ((and(> y x) (> y z)) y)
  16.        ((and(> z x) (> z y)) z)))
  17.  
  18.   ;compare the three numbers and get the second largest number
  19.   (define (secondLargestNumber x y z)
  20.     (cond((and(> x y) (< x z)) x)
  21.        ((and(> y x) (< y z)) y)
  22.        ((and(> z x) (< z y)) z)))
  23.  
  24.   ;return the sum of the squares of the two
  25.   (sumOfSquares largestNumber secondLargestNumber))
  26.  
  27. (sumTwoLargestSquares 1 2 3)
Tags: SCHEME sicp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement