Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you need to pass <code>+</code> or <code>-</code> as a <em>symbol</em> (not as a procedure), you have to quote it first:</p> <pre><code>'+ '- </code></pre> <p>For example:</p> <pre><code>((equal? gradeletter+- '+) .33) ((equal? gradeletter+- '-) -.33) </code></pre> <p>But from the context, I don't think the <code>gpa-allocator</code> procedure is correct. A grade can be <code>a</code> or <code>a+</code>, the conditions imply that <code>+</code> or <code>-</code> are actual grades, which is wrong.</p> <p>Maybe you should represent grades as strings and check (using <code>string-ref)</code> the first character in the string to determine if it's <code>#\a, #\b, #\c, #\d, #\e</code> and (if the string's length is greater than 1) test if the second character in the string is either <code>#\+</code> or <code>#\-</code>. Then you can determine the appropriate value of the grade by adding the two values. Alternatively, you could pass the grade as a symbol and convert it to string. This is what I mean:</p> <pre><code>(define (gpa-allocator gradeletter+-) (let ((grade (symbol-&gt;string gradeletter+-))) (+ (case (string-ref grade 0) ((#\a #\A) 4) ((#\b #\B) 3) ((#\c #\C) 2) ((#\d #\D) 1) ((#\e #\E) 0) (else 0)) (if (&gt; (string-length grade) 1) (case (string-ref grade 1) ((#\+) 0.33) ((#\-) -0.33) (else 0)) 0)))) </code></pre> <p>Don't forget to test it:</p> <pre><code>(gpa-allocator 'A) =&gt; 4.0 (gpa-allocator 'A+) =&gt; 4.33 (gpa-allocator 'A-) =&gt; 3.67 </code></pre>
 

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