Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
sicp 1.18
-- [Design an *iterative* process for multiplying to integers that] uses a
-- logarithmic number of steps.
double :: Integer -> Integer
double x = x + x
halve :: Integer -> Integer
halve x = x `div` 2
multiply :: Integer -> Integer -> Integer
multiply x y = multiply' 0 x y
multiply' :: Integer -> Integer -> Integer -> Integer
multiply' state x y
| y == 0 = state
| odd y = multiply' (state + x) x (y - 1)
| otherwise = multiply' (state + x * halve y) x (halve y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.