2017-10-11 52 views
0

最近,我正在学习SiC颗粒,但我遇到一个奇怪的问题:约球拍其余部分预计PARAMS

Error: 
remainder: contract violation 
    expected: integer? 
    given: '(3 4 5 6) 
    argument position: 1st 
    other arguments...: 
    2 

这里是我的代码

(define (same-parity sample . other) 
     (if (null? other) 
      (cons sample '()) 
      (if (= (remainder sample 2) (remainder (car other) 2)) 
        (cons (car other) (same-parity sample (cdr other))) 
        (same-parity sample (cdr other))))) 

    (same-parity 1 2 3 4 5 6) 
  1. 操作系统:win10
  2. lang:racket v6.10.1

它告诉其余的整数参数 我想我给了一个整数余数不是一个列表。那么有人可以告诉我我的代码有什么问题。我很尴尬。预先感谢。

回答

0

发生此错误是因为您的过程在未知数量的多个参数上运行 - 这是.在声明过程时的含义,other被解释为参数列表。它在调用过程时传递正确的参数,但是当我们第一次调用递归时,它失败了。

一种解决方案是确保我们始终适用于多个参数的方法,而不是传递列表的第二个参数,这是你的代码现在正在做,导致错误。试试这个:

(define (same-parity sample . other) 
    (if (null? other) 
     (cons sample '()) 
     (if (= (remainder sample 2) (remainder (car other) 2)) 
      (cons (car other) (apply same-parity sample (cdr other))) 
      (apply same-parity sample (cdr other))))) 
+0

谢谢它完美的解决了我的问题 – Ceuvres