diff --git a/chapter01/worksheets/solution1.6.2.5.scala b/chapter01/worksheets/solution1.6.2.5.scala new file mode 100644 index 0000000..239f1b8 --- /dev/null +++ b/chapter01/worksheets/solution1.6.2.5.scala @@ -0,0 +1,24 @@ +2/** author: Andreas Röhler */ + +/** + Exercise 1.6.2.5 + Define a function of type List[Double] => List[Double] that “normalizes” the list: + + it finds the element having the largest absolute value and, if that +value is nonzero, divides all elements by that value and returns a new + list; otherwise returns the original list. + + Test with: scala> normalize(List(1.0, -4.0, 2.0)) + res0: List[Double] = List(0.25, 1.0, 0.5) + */ + +def normalize(a: List[Double]): List[Double] = { + + val b = a.map(k => k.abs).max + if (b == 0) a else a.map(k => k.abs).map(_ / 4) +} + +val result = normalize(List(1.0, -4.0, 2.0)) +val expected: List[Double] = List(0.25, 1.0, 0.5) + +assert(result == expected)