Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Scala Version
- // def mapR(f: Int => Int, combine: (Int, Int) => Int, zero: Int)(a: Int, b: Int): Int = {
- // if (a > b) zero
- // else combine(f(a), mapR(f, combine, zero)(a+1, b))
- // }
- // def productR(f: Int => Int)(a: Int, b: Int): Int = mapR(f, (x,y) => x*y, 1)(a,b)
- // def sumR(f: Int => Int)(a: Int, b: Int): Int = mapR(f, (x,y) => x+y, 0)(a,b)
- // sumR(x => x)(1,2)
- // productR(x => x)(3,4)
- // sumR(x => x+1)(1,2)
- // Swift Version
- func mapR(f: Int -> Int, combine: (Int, Int) -> Int, zero: Int)(a: Int, b: Int) -> Int {
- if (a > b) { return zero }
- return combine(f(a), mapR(f, combine, zero)(a: a+1, b: b))
- }
- func product(f: Int -> Int)(a: Int, b: Int) -> Int { return mapR(f, {(x, y) -> Int in return x * y}, 1)(a: a, b: b) }
- func sum(f: Int -> Int)(a: Int, b: Int) -> Int { return mapR(f, {(x,y) -> Int in return x + y}, 0)(a: a, b: b) }
- println(sum( {(x: Int) -> Int in return x} )(a: 1, b: 2))
- println(product( {(x: Int) -> Int in return x} )(a: 3, b: 4))
- println(sum( {(x: Int) -> Int in return x + 1} )(a: 1, b: 2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement