Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>1) For integer division in scheme, use the <i>quotient</i> function, as in (quotient 9 2) gives 4.<BR> 2) For integer modulus in scheme, use the <i>remainder</i> function, as in (remainder 9 2) gives 1.</p> <p>Scheme is based on recursion, as you stated. To solve a recursive problem, you have to state the solution in terms of a smaller instances of the problem: "The number of 6 digits in a number N is ... " and for ... you state the solution in terms of the solution of part of N.</p> <p>The easiest part of N to recur on is N/10, which is all but the last digit of N. Then, to solve this recursively, you should assume that you know the answer to (num (quotient N 10)). Call that value X. How does knowing N and X tell you how many digits is in N?</p> <p>If the last digit of N is 6, then the solution is X+1. Otherwise, the solution is X. How can you tell if the last digit is 6? Use the remainder function.</p> <pre><code>(define (num N) ; check cases (cond ; case 1) negative condition ((&lt; N 0) (num (- N))) ; case 2) zero condition, the terminal case ((= N 0) 0) ; case 3) recursive case with a 6 as the last digit ((= (remainder N 10) 6) (+ X 1)) ; case 4) recursive case without a 6 as the last digit (#t X) )) </code></pre> <p>Now you just have to substitute the recursive assumption in for X, so for example case 3 becomes</p> <pre><code>; case 3 ((= (remainder N 10) 6) (+ (num (quotient N 10)) 1)) </code></pre> <p>Change X for case 4 as well and you'll get the solution.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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