Divisible Function Scheme

2019-08-30 17:17发布

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条回答
迷人小祖宗
2楼-- · 2019-08-30 17:46

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
查看更多
登录 后发表回答