Created
October 25, 2014 20:04
-
-
Save davejachimiak/390f1d8b7640228a01ce to your computer and use it in GitHub Desktop.
sicp 1.18
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
-- [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