Scheme 通过乘法过程计算方案中负y值的递归函数

Scheme 通过乘法过程计算方案中负y值的递归函数,scheme,racket,Scheme,Racket,到目前为止,我只考虑正数和负数x值。我不确定如何解释负y值。这是我的 (define (Multiply x y) (if (= y 0) 0 (+x (Multiply x ( - y 1)) (if (< y 0) //here is where i try to account for negative numbers. (+ x( - 0 (Multiply x(+ y 1)))))) ) (Multiply 8 3) //outputs 24 (Multiply -9 3) //

到目前为止,我只考虑正数和负数x值。我不确定如何解释负y值。这是我的

(define (Multiply x y)
(if (= y 0)
0
(+x (Multiply x ( - y 1))
(if (< y 0) //here is where i try to account for negative numbers.
(+ x( - 0 (Multiply x(+ y 1))))))
)
(Multiply 8 3) //outputs 24
(Multiply -9 3) //outputs -27
(定义(乘以x y)
(如果(=y 0)
0
(+x(乘以x(-y 1))
(if(
我需要找到一种方法来解释负的y值。有点困惑怎么做。 非常新的方案,任何东西都会有帮助。提前感谢。

(定义(两个符号a和b相同)
(define (both-same-sign a b)
  (if (and (> a 0) (> b 0)) #t
  (if (and (< a 0) (< b 0)) #t
  #f)))

(define (make-positive n)
  (if (< n 0) (- n)
  n))

(define (multiply x y)
  (define a (make-positive x))
  (define b (make-positive y))
  (define (multiplier a b)
    (if (or (= a 0) (= b 0)) 0
    (+ a (multiplier a (- b 1)))))
  (define result (multiplier a b))
  (if (both-same-sign x y) result
   (- result)))
(如果(和(>a0)(>b0))#t (如果(和(
我打算建议在递归中只处理正数,并使用包装器将负输入变为正,然后在只有一个原始输入为负时对结果求反。包装器看起来像
(define(multiply a b)(let((p(multiply unsigned(abs a)(abs b)))(if(xor(a<0)(b<0))(-p)p))
在您的示例中,(无符号乘法):未定义;您将定义该函数。我只是试图演示如何将符号从乘法中分离出来。您是说我应该使用超函数吗?我必须通过一系列加法来实现乘法过程。