added lesson 2

This commit is contained in:
Louis Heredero 2025-03-03 17:04:55 +01:00
parent 31629ac479
commit f66fdac8b4
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
2 changed files with 26 additions and 0 deletions

View File

@ -0,0 +1,6 @@
def mul(x: Int, y: Int): Int = x * y
def mulCurry(x: Int) = (y :Int) => x * y
def mulCurryScala(x: Int)(y: Int): Int = x * y

20
src/Lesson2/SumInts.sc Normal file
View File

@ -0,0 +1,20 @@
def sumInts(a: Int, b: Int): Double =
if (a > b) 0 else a + sumInts(a + 1, b)
def sumCubes(a: Int, b: Int): Double =
if (a > b) 0 else a * a * a + sumCubes(a + 1, b)
def sumRec(a: Int, b: Int): Double =
if (a > b) 0 else 1.0 / a + sumRec(a + 1, b)
def mapReduce(f: Int => Double, a: Int, b: Int): Double =
if (a > b) 0 else f(a) + mapReduce(f, a + 1, b)
sumInts(3, 5)
sumCubes(1, 3)
sumRec(1, 3)
mapReduce(i => i, 3, 5)
mapReduce(i => i*i*i, 1, 3)
mapReduce(i => 1.0/i, 1, 3)