DrRacket 程序体帮助(布尔奇数?x) [英] DrRacket procedure body help (boolean-odd? x)

查看:35
本文介绍了DrRacket 程序体帮助(布尔奇数?x)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

奇数的迭代版本?对于非负整数参数,可以使用 and、or 和 not 编写.为此,您必须利用 and 和 or 是特殊形式的事实,它们按从左到右的顺序评估它们的参数,一旦确定值就退出.写 (boolean-odd? x) 不使用 if 或 cond,而是使用 and, or, not (boolean) 代替.您可以使用 + 和 -,但不要使用商、余数、/等.

An iterative version of odd? for non-negative integer arguments can be written using and, or, and not. To do so, you have to take advantage of the fact that and and or are special forms that evaluate their arguments in order from left to right, exiting as soon as the value is determined. Write (boolean-odd? x) without using if or cond, but using and, or, not (boolean) instead. You may use + and -, but do not use quotient, remainder, /, etc.

推荐答案

正奇数可以定义为 1 + 2n.因此奇数是:

A positive odd number can be defined as 1 + 2n. Thus an odd number is:

  • 如果 x 是 1
  • 如果 x 大于 1 and x-2 是奇数.
  • If x is 1
  • If x is greater than 1 and x-2 is odd.

因此,尾递归/迭代的*解决方案如下所示:

Thus one* solution that is tail recursive/iterative looks like this:

(define (odd? x)
  (or (= ...)          ; #t if 1
      (and ...         ; #f if less than 1
           (odd? ...))); recurse with 2 less

*玩过它,有很多方法可以做到这一点,并且仍然可以迭代并且没有 if/cond.

*having played around with it it's many ways to do do this and still have it iterative and without if/cond.

这篇关于DrRacket 程序体帮助(布尔奇数?x)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆