Skip to content

Instantly share code, notes, and snippets.

@RyanSusana
Created May 29, 2020 12:42
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 RyanSusana/a154c9a6ec178f68ac42c4b52caf18bb to your computer and use it in GitHub Desktop.
Save RyanSusana/a154c9a6ec178f68ac42c4b52caf18bb to your computer and use it in GitHub Desktop.
// Here is how I would curry a product function. I really like the word curry, btw.
def product(f: Int => Int)(a: Int, b: Int): Int =
if (a > b) 1 else f(a) * product(f)(a + 1, b)
// Factorial can now be written as follows
// Because factorial is by definition the product of all numbers 1 to n
def factorial(n: Int) = product(identity)(1, n)
// True!
factorial(5) == 120
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment