Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As @WorBlux pointed out, you have some parenthesis problems. Besides that, I have a couple of tips:</p> <ul> <li>It's a bit clearer if you use nested <code>if</code>s to separate conditions</li> <li>Your conditions are not correct, the equality cases are missing</li> <li>If the conditions are right, it won't be necessary to have a catch-all <code>else</code> case</li> <li>You should declare a helper procedure for performing the actual squared sum</li> </ul> <p>This is what I mean:</p> <pre><code>(define (sumsq x y) (+ (* x x) (* y y))) (define (toptwosq a b c) (if (&gt;= a b) (if (&gt;= b c) (sumsq a b) (sumsq a c)) (if (&gt;= a c) (sumsq b a) (sumsq b c)))) </code></pre> <p>The same code can be written as follows using <code>cond</code>, notice how to correctly express the conditions in such a way that all cases are covered:</p> <pre><code>(define (toptwosq a b c) (cond ((and (&gt;= a b) (&gt;= b c)) (sumsq a b)) ((and (&gt;= a b) (&lt; b c)) (sumsq a c)) ((and (&lt; a b) (&gt;= a c)) (sumsq b a)) ((and (&lt; a b) (&lt; a c)) (sumsq b c)))) </code></pre> <p>The last condition can be replaced with an <code>else</code>. It's not a "catch-all", we're certain that at this point no more cases remain to be considered:</p> <pre><code>(define (toptwosq a b c) (cond ((and (&gt;= a b) (&gt;= b c)) (sumsq a b)) ((and (&gt;= a b) (&lt; b c)) (sumsq a c)) ((and (&lt; a b) (&gt;= a c)) (sumsq b a)) (else (sumsq b c)))) </code></pre> <p>And finally, if we're smart we can get rid of one case (the first and third cases are the same) and simplify the conditions even more:</p> <pre><code>(define (toptwosq a b c) (cond ((or (&gt;= a b c) (and (&gt;= a c) (&gt; b a))) (sumsq a b)) ((and (&gt;= a b) (&gt; c b)) (sumsq a c)) (else (sumsq b c)))) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload