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 cube(x: Int) : Int = x*x*x | |
| def sumCubes(a:Int, b:Int): Int= | |
| if(a>b) 0 | |
| else | |
| cube(a) + sumCubes(a+1, b) |
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 sum(f: Int => Int, a: Int, b:Int) : Int = | |
| if(a >b) 0 | |
| else f(a) + sum(f, a+1, b) |
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 sumCube(a:Int, b:Int) : Int = def sum(cube, a, b) | |
| def sumFactorials(a:Int, b:Int): Int = def sum(fact, a, b) |
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 cube(x:Int): Int = x*x*x | |
| def fact(x:Int): Int = if(x == 0) 1 else fact(x – 1) |
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 sum(f: Int => Int, a:Int, b: Int) = { | |
| def loop(a: Int, acc: Int): Int = | |
| if (a > b) acc | |
| else | |
| loop(a+1, f(a) + acc) | |
| loop(a,0) | |
| } | |
| sum(x => x*x*x, 3,5) |
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 sum(f: Int => Int) (a: Int, b: Int): Int = | |
| if(a>b) 0 | |
| else f(a) + sum(f)(a+1, b) |
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 product(f: Int => Int)(a: Int, b: Int): Int = | |
| if (a > b) 1 | |
| else f(a) * product(f)(a+1, b) |
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 product(f: Int => Int)(a: Int, b: Int): Int = | |
| if (a > b) 1 | |
| else f(a) * product(f)(a+1, b) | |
| def fact(n: Int) = product(x=>x)(1,n) |
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
| class Rational(x: Int, y: Int){ | |
| def numer = x | |
| def denom = y | |
| } |
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 addRational(r: Rational, s: Rational): Rational = | |
| new Rational(r.numer * s.denom + s.numer * r.denom, | |
| r.denom * s.denom) |
OlderNewer