Created
May 29, 2020 12:42
-
-
Save RyanSusana/a154c9a6ec178f68ac42c4b52caf18bb to your computer and use it in GitHub Desktop.
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
// 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