compiler-errors - 方案契约(Contract)违约错误

标签 compiler-errors scheme racket

(define (real x) (car x))
(define (imag x) (cdr x))
(define (complex-sqrt x)
    (define (first a b) (sqrt (/ (+ a
                              (sqrt (+
                                     (* a a)
                                     (* b b))))
                           2)))
    (define (second a b) (sqrt (/ (+ (* -1 a)
                               (sqrt (+
                                      (* a a)
                                      (* b b))))
                            2)))
(cons (first (real x) (imag x)) (second (real x) (real y))))
(complex-sqrt '(2 3))

当我尝试评估(第一个a b)和(第二个a b)时,我不断收到错误消息,指出“*违反契约(Contract):预期数量”。我不确定此信息是否相关,但是输入将始终是2个数字的列表。

最佳答案

您尚未提供完整的示例,因此很难说出问题所在。
也许您使用错误类型的参数调用complex-sqrt

这对我有用:

#lang r5rs
(define (real x) (car x))
(define (imag x) (cdr x))
(define (complex-sqrt x)
  (define (first a b) (sqrt (/ (+ a
                                  (sqrt (+
                                         (* a a)
                                         (* b b))))
                               2)))
  (define (second a b) (sqrt (/ (+ (* -1 a)
                                   (sqrt (+
                                          (* a a)
                                          (* b b))))
                                2)))
  (cons (first  (real x) (imag x))
        (second (real x) (imag x))))

(display (complex-sqrt (cons 1 2)))
(newline)

关于compiler-errors - 方案契约(Contract)违约错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40027057/

相关文章:

arrays - 获取Scheme中向量的第一个元素

scheme - Dr Scheme 中的图形表示

math - Racket /方案中的总和平方

recursion - 我在最后一次递归调用函数时得到了 "scheme application not a procedure"

functional-programming - 理解 Let/cc 和 throw in racket

java - Java ArrayList自变量给出预期的<Identifier>

mongodb - Playframework 2.3x : scala. concurrent.Future[play.api.mvc.Result])play.api.mvc.Action[play.api.mvc.AnyContent]

typescript - 使用索引访问时如何避免 TS2322 "Type any is not assignable to type never"

compiler-errors - 即使我已初始化视频系统,也未初始化

recursion - 方案无限递归