Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
sicp 1.17 in haskell
-- ...
-- ...
-- [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
You can’t perform that action at this time.