Created
July 5, 2009 06:04
-
-
Save hakobe/140855 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| def iteration(op: (Double, Double) => Double, init: Int)(f: Int => Double)(a: Int, b:Int) :Double = { | |
| def iter(a: Int, result: Double) :Double = { | |
| if (a > b) result | |
| else iter(a+1, op(f(a),result)) | |
| } | |
| iter(a,init) | |
| } | |
| //def sum(f: Int => Double)(a: Int, b: Int) :Double = { | |
| // def iter(a: Int, result: Double) :Double = { | |
| // if (a > b) result | |
| // else iter(a+1, f(a)+result) | |
| // } | |
| // iter(a,0) | |
| //} | |
| val sum = iteration( (x, y) => x + y, 0) _ | |
| println(sum(x => x)(0,10)) | |
| //def product(f: Int => Double)(a: Int, b: Int) :Double = { | |
| // def iter(a: Int, result: Double) :Double = { | |
| // if (a > b) result | |
| // else iter(a+1, f(a)*result) | |
| // } | |
| // iter(a,1) | |
| //} | |
| val product = iteration( (x ,y) => x * y, 1) _ | |
| println(product(x => x)(1,10)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment