0

以下是我已经研究了几天的问题的代码。我遇到的问题是我打电话时出于某种原因:

(apa-multi '(7 3 1 2) '(6 1 4))

回报是:

'(4 8 9 5 6 8)

它应该输出的答案是

'(4 4 8 9 5 6 8)

当我打电话时:

(apa-multi '(3 1 2) '(6 1 4))

输出是:

 '(1 9 1 5 6 8)

哪个是对的。

我已经多次调试了我的代码,但我似乎无法找出问题所在(顺便说一句,我知道我编写的“remove-empty”函数很可能是不必要的)。谁能告诉我这里哪里出错了?(我对这个问题的目标是将任意精度的数字保持为列表格式,我无法创建一个从 list->num 或 num->list 转换数字的函数。)我相信我已经提供了所有必要的代码让某人弄清楚我的目标,但如果没有,请告诉我。我对此的提示是“ d = dndn−1 ...d1 乘以 e = emem−1 ...e1 可以通过规则 de=d∗e1 +10∗(d∗em em-1...e2).)"

(define (remove-empty L)
 (define (remove-empty-h L accum)
   (cond ((null? L) accum)
      ((null? (car L)) 
      (remove-empty (cdr L)))
      (else (cons (car L) (remove-empty-h (cdr L) accum)))))
 (remove-empty-h L '()))

(define (apa-add lst1 lst2)
 (define (apa-add-h lst1 lst2 carry)
  (cond ((and (null? lst1) (null? lst2)) 
             (if (not (= 0 carry)) 
                 (list carry)
                 '()))
       ((null? lst1)  (append (apa-add-h lst1 '() carry)
                              (list (+ (car (reverse-l lst2)) carry))
                              (reverse-l(cdr (reverse-l lst2)))))
       ((null? lst2)  (append (apa-add-h '() lst2 carry)
                              (list (+ (car (reverse-l lst1)) carry)))
                              (reverse-l(cdr (reverse-l lst1))))
       (else 
          (append (apa-add-h (cdr lst1) (cdr lst2) (quotient (+ (car lst1) (car lst2) carry) 10)) 
                 (list (modulo (+ (car lst1) (car lst2) carry) 10))))))
   (apa-add-h (reverse-l lst1) (reverse-l lst2) 0))

(define (d-multiply lst factor)
  (define (d-multiply-h lst factor carry)
    (cond ((null? lst) (if (= carry 0)
                        '()
                        (list carry)))
       ((>= (+ (* (car lst) factor) carry) 10)
        (append  ;(list (check-null-and-carry-mult lst carry))
                 (d-multiply-h (cdr lst) factor (quotient (+ (* (car lst) factor) carry) 10))
                 (list (modulo (+ (* (car lst) factor) carry) 10))))         

       (else (append   ;(list (check-null-and-carry-mult lst carry))
                      (d-multiply-h (cdr lst) factor (quotient(+ (* (car lst) factor) carry) 10))
                      (list (+ (* (car lst) factor) carry))))))
  (remove-empty (d-multiply-h (reverse-l lst) factor 0)))

   (define (nlength l)
     (if (null? l)
       0
       (+ 1 (nlength (cdr l)))))


(define (apa-multi d e)
 (define temp '())
  (cond ((= (max (nlength e) (nlength d)) (nlength e))
      (set! temp e)
      (set! e d)
      (set! d temp))
     (else
      (set! temp d)
      (set! d e)
      (set! e temp)))

(define (apa-multi-h d e)
  (cond ((null? e) (list 0))
       (else (append  (apa-add (d-multiply d (car e)) 
                       (append (apa-multi-h d (cdr e)) (list 0)))))))
 (apa-multi-h d (reverse-l e)))
4

2 回答 2

1

您的代码不起作用的原因是您的 apa-add 已损坏。例如:

> (apa-add '(7 3 1 2) '(6 1 4))
'(9 2 6)
> (+ 7312 614)
7926

如果您使用有效的 apa-add,您的其余代码似乎可以工作,至少对于您的 2 个示例。

我承认我没有尝试完全理解您的代码;糟糕的格式和设置!最后的程序让我想从头开始。因此,即使您可以简单地纠正您的 apa-add,也可以看看我的版本,因为它更短并且可能更容易理解。

基于我之前对apa-add乘法的回答是 apa-adding 的问题,一次将一个列表乘以一个数字,并在中间乘法的末尾添加零,就像您手动执行的操作一样:

(define (car0 lst) (if (empty? lst) 0 (car lst)))
(define (cdr0 lst) (if (empty? lst) empty (cdr lst)))

(define (apa-add l1 l2) ; apa-add (see https://stackoverflow.com/a/19597007/1193075)
  (let loop ((l1 (reverse l1)) (l2 (reverse l2)) (carry 0) (res '()))
    (if (and (null? l1) (null? l2) (= 0 carry)) 
        res
        (let* ((d1 (car0 l1)) (d2 (car0 l2)) (ad (+ d1 d2 carry)) (dn (modulo ad 10)))
          (loop (cdr0 l1) (cdr0 l2) (quotient (- ad dn) 10) (cons dn res))))))

(define (mult1 n lst) ; multiply a list by one digit
  (let loop ((lst (reverse lst)) (carry 0) (res '()))
    (if (and (null? lst) (= 0 carry))
        res
        (let* ((c (car0 lst)) (m (+ (* n c) carry)) (m0 (modulo m 10)))
          (loop (cdr0 lst) (quotient (- m m0) 10) (cons m0 res))))))

(define (apa-multi l1 l2) ; full multiplication
  (let loop ((l2 (reverse l2)) (app '()) (res '()))
    (if (null? l2) 
        res
        (let* ((d2 (car l2)) (m (mult1 d2 l1)) (r (append m app)))
          (loop (cdr l2) (cons '0 app) (apa-add r res))))))
于 2013-10-26T21:47:23.850 回答
0

不知道为什么它不起作用,所有这些追加和反转都很难遵循,并且不确定所有这些集合发生了什么!东西。将状态放入尾调用中更容易遵循并且通常启动效率更高。

   (define (apa-add . Lists)
      (define (cdrs-no-null L)
                   (cond ((null? L) '())
                         ((null? (cdar l)) (cdrs-no-null (cdr L)))
                         (else (cons (cdar l) (cdrs-no-null (cdr l))))))
        (let loop ((carry 0) (Lists (map reverse Lists)) (sum '()))
              (if (null? Lists)
                  (if (zero? carry) sum (cons carry sum))
                  (loop (quotient (fold + carry (map car Lists)) 10)
                        (cdrs-no-null Lists)
                        (cons (modulo  (fold + carry (map car Lists)) 10) sum)))))



       (define (apa-mult . Lists)
            (define (mult-by-factor n order L)
              (let loop ((order order) (L (reverse L)) (carry 0) (sum '()))
                (cond ((> order 0) (loop (- order 1) L carry (cons 0 sum)))
                      ((null? L) (if (zero? carry) 
                                     sum 
                                     (cons carry sum))) ;;bug here if carry > 9
                      (else (loop 0 
                                  (cdr L) 
                                  (quotient (+ carry (* n (car L))) 10) 
                                  (cons (modulo (+ carry (* n (car L))) 10) sum))))))
             (define (apa-mult2 L1 L2)
               (let ((rL1 (reverse L1))
                     (rL2 (reverse L2))
                     (zip-with-order
                        (lambda (L) 
                          (let loop ((order 0) (L L) (accum '()))
                             (if (null? L) 
                                 accum
                                 (loop (+ 1 order) 
                                       (cdr L)  
                                       (cons (cons (car L) order) accum)))))))
                   (fold apa-add '(0) (map (lambda (x) 
                                              (mult-by-factor (car x) (cdr x) L2))
                                           (zip-with-order rl1)))))
            (fold apa-mult2 '(1) Lists)))

(apa-mult '(3 1 2) '(6 1 4)))

;值 7: (1 9 1 5 6 8)

(apa-mult '(2 0 0) '(3 1 2) '(6 1 4))

;值 8: (3 8 3 1 3 6 0 0)

(apa-mult '(7 3 1 2) '(6 1 4))

;值 9: (4 4 8 9 5 6 8)

于 2013-10-26T23:16:45.190 回答