Created
October 25, 2014 19:24
-
-
Save davejachimiak/ac9059f854fbaaa82506 to your computer and use it in GitHub Desktop.
sicp 1.17 in haskell
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
-- ... | |
-- ... | |
-- [Assume your language doesn't have a multiplication function or | |
-- table. Given `double` and `half` functions,] design a | |
-- multiplication procedure that uses a logarithmic number of steps. | |
double :: Integer -> Integer | |
double x = x + x | |
halve :: Integer -> Integer | |
halve x = x `div` 2 | |
m :: Integer -> Integer -> Integer | |
m x y | |
| y == 0 = 0 | |
| odd y = x + m x (y - 1) | |
| otherwise = m (double x) (halve y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment