Skip to content

Instantly share code, notes, and snippets.

@chrislewis
Last active November 7, 2019 02:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrislewis/56c155fc690cd8daf08b6d7a692ecce4 to your computer and use it in GitHub Desktop.
Save chrislewis/56c155fc690cd8daf08b6d7a692ecce4 to your computer and use it in GitHub Desktop.
-- 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