问题描述
我正在尝试编写一个函数来确定一个数字是否可以被 2 或 3 整除.根据我在网上阅读的内容,已经有一个 Scheme 谓词可整除?但它对我不起作用.我试过自己写一个,但我不知道如何写一个谓词函数.我能得到什么帮助吗?谢谢!
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!
推荐答案
divisible?
谓词可以用 remainder
过程表示,记住:一个数 n 除以 x
的余数为零,则 code>n 可以被 x
整除.
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)
现在我们可以像这样检查一个数字是否可以被3
整除:
Now we can check if a number is divisible by, say, 3
like this:
(divisible? 42 3)
=> #t
这篇关于可分功能方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!