arbitrary precision addition using lists of digits

妖精的绣舞 提交于 2019-11-29 17:57:19

reverse is already defined in Racket so there's no need to redefine it.

I have rewritten your code for a version that is clearer (to me, at least):

(define (apa-add l1 l2)

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

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

such as

-> (apa-add '(4 7 9) '(7 8 4))
'(1 2 6 3)
-> (+ 479 784)
1263

car0and cdr0 are functions that help me to continue processing empty lists as a list of zeroes.

I introduced a new variable, carry, which is used to carry a value from iteration to iteration, just as you do it manually.

EDIT 1

The named let is equivalent to the following code:

(define (apa-add l1 l2)

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

  (define (apa-add-helper l1 l2 carry 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)))
          (apa-add-helper (cdr0 l1) (cdr0 l2) (quotient (- ad dn) 10) (cons dn res)))))

  (apa-add-helper (reverse l1) (reverse l2) 0 '()))

EDIT 2

The non tail-recursive version would be

(define (apa-add l1 l2)

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

  (define (apa-add-helper l1 l2 carry)
    (if (and (null? l1) (null? l2) (= 0 carry)) 
        '()
        (let* ((d1 (car0 l1))
               (d2 (car0 l2))
               (ad (+ d1 d2 carry))
               (dn (modulo ad 10)))
          (cons dn (apa-add-helper (cdr0 l1) (cdr0 l2) (quotient (- ad dn) 10))))))

  (reverse (apa-add-helper (reverse l1) (reverse l2) 0)))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!