Naming
- Use something shorter and more generic, like
a
andb
, instead oflist1
andlist2
curry
is mispelled -- should becarry
- Use something shorter and more generic, like
Consider defining a class to represent numbers. That way, you can easily change the representation in the future.
I don't think your implementation is tail-recursive. This will lead to stack-overflows for large lists. This might or might not be a problem, depending on your application.
Use "word-at-a-time" thinking only as a last resort. In this case, you are directly recursing over the lists -- one element ("word") at a time. Try to find a higher-level abstraction, such as
map
, that you can use.In this particular case, in order to handle the carries, you really need some sort of "
map
plus accumulator" abstraction. To my knowledge, there is no such function built-in to Scala. So you might have to write your own. Still, I would suggest implementing a generic, higher-order function to do this. That way, you can reuse that function later for other things.Try to decompose your problem. In this case, the problem is already fairly simple. Still, you might (or might not -- I'm not sure) benefit from drawing inspiration from hardware adders.
Nathan Davis
- 895
- 4
- 14