2015-10-21

posted Oct 21, 2015, 5:50 AM by Samuel Konstantinovich   [ updated Oct 21, 2015, 5:50 AM ]
Goal: More Recursion

Reminder Quiz Tomorrow (only scheme, but no recursion)

Do Now on paper:
1. Write a function (isPrimeDigit n) that takes a 1 digit 
non-negative integer n and returns true when the number is prime, 
false otherwise.

(isPrimeDigit 1) returns #false
(isPrimeDigit 2) returns #true
(isPrimeDigit 3) returns #true
(isPrimeDigit 4) returns #false
etc...

Ask yourself which numbers are prime first. Then write the function.

2. Why is today a special day?


Classwork:
1.Go over countEvenDigits
2.Lets make a function: (countPrimeDigits n) that takes any 
non-negative integer n, and returns the number of digits that 
are prime digits.

The solution for countEvenDigits can be found here:
;remember even? is a built in function. 
;You may use it but you should know how to write it.
(define cED (lambda (n)
              (cond
                 ((and(< n 10)(even? n))    1)
                 ((< n 10)                  0)
                 ((even? n)     (+ 1(cED (quotient n 10))))
                 (else              (cED (quotient n 10))))))
;It can be simplified by using recursion on the 
;last digit AND the rest of the digits as follows:
(define cED (lambda (n)
        (cond
           ((and(< n 10)(even? n)) 1)
           ((< n 10)               0)
            (else                 (+ 
                                   (cED (remainder n 10)) 
                                   (cED (quotient n 10)))))))

Practice your lists, here are 2 more questions you can test on your own:
(define L '(1 5 (8 (9) 2) 3))
;extract 8
(caaddr L)
;extract 9
(caar(cdaddr L)) ;you may write: (caadaddr L) on paper including exams
Comments