Recursion DrRacket方案合同违规预期数量

Recursion DrRacket方案合同违规预期数量,recursion,scheme,apply,Recursion,Scheme,Apply,我开始在Scheme中编码,我不想知道一个数字是否“丰富”。如果一个数x的除数之和大于x的两倍,则该数x是“丰富的” 这是我的代码: #lang scheme (define (abundante x) (cond ((= x 0) #f) ((= x 1) #f) ((> (apply +(divisores x)) (doble x)) #t) (else #f) ) ) ;aux functions (define (doble

我开始在Scheme中编码,我不想知道一个数字是否“丰富”。如果一个数x的除数之和大于x的两倍,则该数x是“丰富的”

这是我的代码:

#lang scheme
(define (abundante x)
  (cond 
    ((= x 0) #f)
    ((= x 1) #f)
    ((> (apply +(divisores x)) (doble x)) #t)
    (else #f)
    )
  )


;aux functions
(define (doble x) (* x 2))

(define (divisores x)
  (cond
    ((= x 1) '(1))
    (else (cons 1 (divisores-aux x 2)))
    )
  )

(define (divisores-aux x y)
  (cond
    ((= x y) '(x))
    ((integer? (/ x y))(cons y (divisores-aux x (+ y 1))))
    (else (divisores-aux x (+ y 1)))
    )
  )
如您所见,我有3个辅助功能: 1) 双倍x:返回双倍x 2) 除数x:返回x的除数 2.1)除数辅助x y:检查x/y是否为整数,然后选择y+1

但是当除数达到x=y时,我遇到了问题。我想返回x,因为x本身就是一个除法器,但DrRacket打印以下错误:

+: contract violation
  expected: number?
  given: y
  argument position: 6th
  other arguments...:
并指示错误是在
apply+(除数x)

如果我返回null或'(),一切正常,但显然我没有得到正确的结果


提前感谢

在divisitores aux的基本情况下有一个bug,如下所示:

'(x)
上述表达式将返回一个以符号
x
作为其单个成员的列表(要了解原因,请阅读文档中的相关内容)。您的意思是,创建一个值为
x
变量的列表:

(list x)
另外,最好使用
余数
测试一个数字是否被另一个数字除。这将解决以下问题:

(define (divisores-aux x y)
  (cond
    ((= x y) (list x))
    ((zero? (remainder x y)) (cons y (divisores-aux x (+ y 1))))
    (else (divisores-aux x (+ y 1)))))
现在,
abundante
工作正常:

(abundante 42)
=> #t
(abundante 45)
=> #f
”(x)
将不会返回值为
x
的列表,而是返回符号
x
。它应该是
(列表x)