(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/