2017-10-11 6 views
0

を期待:は、私はSICPを学んでいるが、私は奇妙な質問会い、最近の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. OS:win10
  2. ラング:ラケットv6.10.1

これは、整数のパラメータを期待して残りの部分を返します 私は残りの部分に整数を与えたと思います。誰かが私のコードに何が間違っているのかを伝えることができます。私は大きな困惑の中にいる。少し早いですがお礼を。

答えて

0

このエラーは、手続きが宣言時に.が意味するものであり、arg34のリストとして解釈される、複数の引数の不明な数で動作するために発生します。手続きが呼び出されたときに正しいargsを渡しますが、再帰を初めて呼び出すと失敗します。

解決策の1つは、リストを2番目の引数として渡す代わりに、複数の引数に常にプロシージャを適用することです。これがエラーの原因となります。試してみてください:

(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

関連する問題