问题
I'm trying to write a function that determines if a number is divisible by 2 or 3. From what i've read online there is already a Scheme predicate divisible? but it is not working for me. I've tried writing one myself, but I don't know how to write a predicate function. Is there any help I can get? Thanks!
回答1:
The divisible?
predicate can be expressed in terms of the remainder
procedure, remember: a number n
is divisible by x
if the remainder of dividing n
by x
is zero.
(define (divisible? n x)
(zero? (remainder n x))) ; alternatively: (= (remainder n x) 0)
Now we can check if a number is divisible by, say, 3
like this:
(divisible? 42 3)
=> #t
来源:https://stackoverflow.com/questions/21272678/divisible-function-scheme