Skip to content

Instantly share code, notes, and snippets.

@epost
Created July 7, 2012 16:46
Show Gist options
  • Save epost/3067166 to your computer and use it in GitHub Desktop.
Save epost/3067166 to your computer and use it in GitHub Desktop.
Mischa's adder
import Data.Digits
carry r i x y | i == 0 = 0
| x + y + carry r (i-1) x y <= r-1 = 0
| True = 1
-- currently works iff x and y have same # of digits, due to zip3
add x y =
sum [ (x_i + y_i + carry r i x y)*r^i | (x_i, y_i, i) <- (zip3 xs ys [0..])]
where r = 10
xs = digits r x
ys = digits r y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment