Skip to content

Instantly share code, notes, and snippets.

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