Skip to content

Instantly share code, notes, and snippets.

@davejachimiak
Created October 25, 2014 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davejachimiak/ac9059f854fbaaa82506 to your computer and use it in GitHub Desktop.
Save davejachimiak/ac9059f854fbaaa82506 to your computer and use it in GitHub Desktop.
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