Divisible Function Scheme

自闭症网瘾萝莉.ら 提交于 2019-12-12 04:18:43

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!