Last active
November 7, 2019 02:08
-
-
Save chrislewis/56c155fc690cd8daf08b6d7a692ecce4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- solves bug of (9 + 1) in the case of a carry and mismatched list lengths | |
addTwoNumbers (l1 : tail1) (l2 : tail2) carry = | |
(mod sum 10) : (addTwoNumbers tail1 tail2 newCarry) where | |
sum = l1 + l2 + carry | |
newCarry = div sum 10 | |
addTwoNumbers (l1 : tail1) [] carry = | |
(mod (l1 + carry) 10) : (addTwoNumbers tail1 [] (div (l1 + carry) 10)) | |
addTwoNumbers [] (l2 : tail2) carry = | |
(mod (l2 + carry) 10) : (addTwoNumbers [] tail2 (div (l2 + carry) 10)) | |
addTwoNumbers [] [] carry = | |
if carry > 0 then (carry : []) else [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment