Code:
(define (isPrime? x)
;; private procedures
(define (isComposite? i)
(if (= (remainder x i) 0)
#t
#f))
;; this is not defined yet. What this method will for two conditions:
;; 1) if i is less than x-1 and if the remainder of x and i is zero, then it's
;; not a prime number and the return value of isPrime is false.
;; 2) If i = x-1 and isComposite is false, then the number is prime and the return
;; value of isPrime is true.
;; 3) If the previous two conditions are false, then isPrimeIteration
;; recursively calls itself with i+1 as an argument.
;; what I don't know how to do is have a cond block and have the consequent
;; be return value for the entire method
Start out by implementing exactly what you described. The "return value" is merely whatever the last line executed evaluates to.
Code:
(define (isPrimeIteration i)
(cond
((< i (- x 1))
(if (zero? (modulo x i))
#f ) ) ; <== last line executed if these conditions are met
((= i (- x 1))
(if (= (isComposite? i) #f)
#t ) ) ; <== last line executed if these conditions are met
(else
(isPrimeIteration (+ i 1)) ) ) )
(isPrimeIteration 2) )
There is a serious problem though. If i=x-1 then isComposite? will never evaluate to true and your iteration will never end for prime x's.
I would recommend forgetting about your isComposite? function (just test the modulo for zero). Focus on the three possibilities for i as it increments each time through the iteration:
Code:
(define (isPrimeIteration i)
(cond
((< i x)
(if (zero? (modulo x i))
; ... what to do if remainder is zero
; ... what to do if remainder is non-zero
) )
((= i x)
(if (zero? (modulo x i))
; ... what to do if remainder is zero
; ... what to do if remainder is non-zero
) )
(else
; ... what to do if i is greater than x
) )
(isPrimeIteration 2) )
Bookmarks