为什么我在方案中得到*变量误差?

时间:2021-09-03 19:45:14
(define (even-odd x)
(if ( ==(% x 2 ) 0) (1) (0)))

when i call( even-odd 5 ) i get this message

当我打电话(偶数)时,我得到了这个信息

;Unbound variable: %
;To continue, call RESTART with an option number:
; (RESTART 11) => Specify a value to use instead of %.
; (RESTART 10) => Define % to a given value.
; (RESTART 9) => Return to read-eval-print level 9.
; (RESTART 8) => Return to read-eval-print level 8.
; (RESTART 7) => Return to read-eval-print level 7.
; (RESTART 6) => Return to read-eval-print level 6.
; (RESTART 5) => Return to read-eval-print level 5.
; (RESTART 4) => Return to read-eval-print level 4.
; (RESTART 3) => Return to read-eval-print level 3.
; (RESTART 2) => Return to read-eval-print level 2.
; (RESTART 1) => Return to read-eval-print level 1.

;Start debugger? (y or n):

,开始调试器吗?(y或n):

Am i doing something wrong ?Also let me know how to select text in edwin.

我做错什么了吗?请告诉我如何在edwin中选择文本。

3 个解决方案

#1


4  

You have a few problems here; namely that you're mistaken in the names of the procedures you're trying to call.

这里有一些问题;也就是说,你在你要调用的程序的名称中弄错了。

  • % should be modulo
  • %应模
  • == should be eqv?
  • = =应该eqv ?
  • As zvrba noted, remove parenthesis surrounding 0 and 1
  • 如zvrba所述,删除围绕0和1的括号

or, if you're simply trying to tell whether a number is even or odd, a simpler and cleaner way to do this is to simply use the builtin even?.

或者,如果你只是想知道一个数字是偶数还是奇数,一个更简单、更简洁的方法就是使用内建偶数。

(even? 5)
> #f

or if you actually want 0 and 1 as a result, a cleaner expression could be

或者如果你想要得到0和1,一个更简洁的表达式可以是

(if (even? x) 1 0)

#2


3  

You must omit parentheses around the return values (1 and 0). What you wrote tries to invoke procedures named 1 and 0. Also, the modulus operator isn't %, but is named otherwise [check the manual; I forgot it -- probably mod or rem].

您必须在返回值(1和0)周围省略括号,您所写的尝试调用的过程称为1和0。此外,模量运算符不是%,而是以其他名称命名[检查手册;我忘了,可能是mod或rem。

#3


3  

This is a more idiomatic way of writing the even-odd procedure:

这是一种更为习惯的写奇偶过程的方式:

(define (even-odd x)
    (if (zero? (modulo x 2))
        #t
        #f))

A bit shorter:

有点短:

(define (even-odd x)
    (zero? (modulo x 2)))

Or even better, use the built-in procedure even?:

甚至更好,使用内置程序?

(even? x)

#1


4  

You have a few problems here; namely that you're mistaken in the names of the procedures you're trying to call.

这里有一些问题;也就是说,你在你要调用的程序的名称中弄错了。

  • % should be modulo
  • %应模
  • == should be eqv?
  • = =应该eqv ?
  • As zvrba noted, remove parenthesis surrounding 0 and 1
  • 如zvrba所述,删除围绕0和1的括号

or, if you're simply trying to tell whether a number is even or odd, a simpler and cleaner way to do this is to simply use the builtin even?.

或者,如果你只是想知道一个数字是偶数还是奇数,一个更简单、更简洁的方法就是使用内建偶数。

(even? 5)
> #f

or if you actually want 0 and 1 as a result, a cleaner expression could be

或者如果你想要得到0和1,一个更简洁的表达式可以是

(if (even? x) 1 0)

#2


3  

You must omit parentheses around the return values (1 and 0). What you wrote tries to invoke procedures named 1 and 0. Also, the modulus operator isn't %, but is named otherwise [check the manual; I forgot it -- probably mod or rem].

您必须在返回值(1和0)周围省略括号,您所写的尝试调用的过程称为1和0。此外,模量运算符不是%,而是以其他名称命名[检查手册;我忘了,可能是mod或rem。

#3


3  

This is a more idiomatic way of writing the even-odd procedure:

这是一种更为习惯的写奇偶过程的方式:

(define (even-odd x)
    (if (zero? (modulo x 2))
        #t
        #f))

A bit shorter:

有点短:

(define (even-odd x)
    (zero? (modulo x 2)))

Or even better, use the built-in procedure even?:

甚至更好,使用内置程序?

(even? x)