summaryrefslogtreecommitdiff
path: root/test/pos/cours2.scala
blob: c96f588791bf8bcd6724ebd9ab526319b8d8a44a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
object m1 {

    def id(x: Int): Double = x;
    def cube(x: Int): Double = x * x * x;
    def reciprocal(x: Int): Double = 1.0/x;

    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 cube(a) + sumCubes(a + 1, b);

    def sumReciprocals(a: Int, b: Int): Double =
      if (a > b) 0
      else 1.0/a + sumReciprocals(a, b);

}

object m2 {

    def id(x: Int): Double = x;
    def cube(x: Int): Double = x * x * x;
    def reciprocal(x: Int): Double = 1.0/x;

    def sum(f: Int => Double, a: Int, b: Int): Double =
      if (a > b) 0
      else f(a) + sum(f, a + 1, b);

    def sumInts(a: Int, b: Int): Double = sum(id, a, b);
    def sumCubes(a: Int, b: Int): Double = sum(cube, a, b);
    def sumReciprocals(a: Int, b: Int): Double = sum(reciprocal, a, b);
}

object m3 {

    def sum(f: Int => Double, a: Int, b: Int): Double =
      if (a > b) 0
      else f(a) + sum(f, a + 1, b);

    def sumInts(a: Int, b: Int): Double = sum((x => x), a, b);
    def sumCubes(a: Int, b: Int): Double = sum((x => x * x * x), a, b);
    def sumReciprocals(a: Int, b: Int): Double = sum((x => 1.0/x), a, b);
}

object m4 {

    def sum(f: Int => Double) = {
      def sumF(a: Int, b: Int): Double =
        if (a > b) 0
        else f(a) + sumF(a + 1, b);
      sumF
    }

    def sumInts = sum(x => x);
    def sumCubes = sum(x => x * x * x);
    def sumReciprocals = sum(x => 1.0/x);

    sumCubes(1, 10) + sumReciprocals(10, 20);
}

object m5 {

    def sum(f: Int => Double): (Int, Int) => Double = (a, b) =>
      if (a > b) 0
      else f(a) + sum(f)(a + 1, b);

    def sumInts = sum(x => x);
    def sumCubes = sum(x => x * x * x);
    def sumReciprocals = sum(x => 1.0/x);

    sumCubes(1, 10) + sumReciprocals(10, 20);
}

object m6 {

    def sum(f: Int => Double)(a: Int, b: Int): Double =
      if (a > b) 0
      else f(a) + sum(f)(a + 1, b);

    def sumInts = sum(x => x);
    def sumCubes = sum(x => x * x * x);
    def sumReciprocals = sum(x => 1.0/x);

    sumCubes(1, 10) + sumReciprocals(10, 20);
}

object m7 {

    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);
    }

    def sumInts = sum(x => x);
    def sumCubes = sum(x => x * x * x);
    def sumReciprocals = sum(x => 1.0/x);

    sumCubes(1, 10) + sumReciprocals(10, 20);
}

object m8 {

  def product(f: Int => Double)(a: Int, step: Int, b: Int): Double =
    if (a > b) 1
    else f(a) * product(f)(a + step, step, b);

  val pi = 8 * product(x => x * x)(4, 2, 40) / product(x => x * x)(3, 2, 40);
}

object m9 {

  def accumulate[t](combiner: (t, t) => t, nullValue: t, f: Int => t, next: Int => Int)
		   (a: Int, b: Int): t =
    if (a > b) nullValue
    else combiner(f(a), accumulate(combiner, nullValue, f, next)(next(a), b));

  def inc(x: Int) = x + 1;

  def sum(f: Int => Double) =
    accumulate((x: Double, y: Double) => x + y, 0, f, inc);
  def product(f: Int => Double) =
    accumulate((x: Double, y: Double) => x * y, 1, f, inc);
}