blob: c88bdc91d2756182d12fc87258a0fcb0bd40f617 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#lang racket
(define/contract (add-to-array-form num k)
(-> (listof exact-integer?) exact-integer? (listof exact-integer?))
(define rev-num (reverse num))
(define raw-array-form
(cond [(= k 0) num]
[else
(for/fold ([sum-list '()]
[addend k]
[carry 0]
#:result sum-list)
([place (append rev-num
(make-list (add1 (order-of-magnitude k)) 0))])
(let* ([place-sum (+ place carry (modulo addend 10))]
[to-carry (quotient place-sum 10)]
[new-place (modulo place-sum 10)])
(values (cons new-place sum-list)
(quotient addend 10)
to-carry)))]))
(match-define-values
(result _) (drop-common-prefix raw-array-form
(make-list (length raw-array-form) 0)))
(cond [(empty? result) '(0)]
[else result]))
|