solution.scm 1001 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. (import
  2. (scheme base))
  3. (define (add-two l1 l2)
  4. (let loop ((in1 l1)
  5. (in2 l2)
  6. (out '())
  7. (carry? #f))
  8. (define next1 (if (null? in1)
  9. 0
  10. (car in1)))
  11. (define next2 (if (null? in2)
  12. 0
  13. (car in2)))
  14. (cond
  15. ((or (not (null? in1))
  16. (not (null? in2))
  17. carry?)
  18. (let* ((sum-val (+ next1
  19. next2
  20. (if carry? 1 0)))
  21. (next-carry? (>= sum-val 10))
  22. (actual-val (if next-carry?
  23. (- sum-val 10)
  24. sum-val)))
  25. (loop (if (null? in1)
  26. in1
  27. (cdr in1))
  28. (if (null? in2)
  29. in2
  30. (cdr in2))
  31. (cons actual-val out)
  32. next-carry?)))
  33. (else
  34. (reverse out)))))
  35. (define (solution2 arg1 arg2)
  36. (add-two arg1 arg2))