0

I'm trying to learn scheme and trying some solutions from this thread. SICP Exercise 1.3 request for comments

I'm also interested in emacs, so I start both together. In emacs I'm using Racket v6.1.

My problem, strange behavior of one solution:

(define (square x) (* x x))

(define (sum-of-squares x y)                                                       
    (+ (square x) (square y))) 

(define (min x y)                                                                  
    (if (< x y) x y)) 

(define (min x y)                                                                  
    (if (< x y) x y))

(define (solution a b c)                                                            
    (sum-of-squares (square (max a b)) (square (max c (min a b)))))

(solution 2 3 4)
337
(solution 1 2 3)
97

No clue what's going on. Expected first 25 and second 13.

gutschy
  • 1
  • 2

1 Answers1

2

So this can easily be explained by substitution:

(solution 2 3 4) is the same as:

(sum-of-squares (square (max 2 3)) (square (max 4 (min 2 3))))
(sum-of-squares (square 3) (square 4))
(sum-of-squares 9 16)
(+ (square 9) (square 16))
(+ 81 256)
; ==> 337

So I hope you have seen why you don't get 25 and can go fix that?

Sylwester
  • 47,942
  • 4
  • 47
  • 79
  • Yes, yes, I have to look third times, but know I get it. I do the square double. Thanks man, I have no talent to become a programmer but I like it to do. :) – gutschy Nov 17 '19 at 21:45