Learned some of Scala's basic grammar http://twitter.github.io/scala_school/zh_cn/index.html,
Try Scala's functional programming.
How to implement with functional programming: SUM (f, a, b) =
s = 0
For i in [A, b]
S+=f (i)
return s
This is the pseudo-code for imperative programming, C/s + +.
1. Implementation 1.
1 def sum (f:int=>int, A:int, b:int): Int = {2 if(a > B) 0 else F (a) + SUM (a+1 , B) 3 }
SUM (x=>x, 1, 5)
SUM (x=>x*x*x, 1, 5)
2. Implementation 2
Here are three parameters, F, a, B. Is it possible to abstract sum into an input of f, and the output is a function of G: (int, int) =>int? And then use the generated G effect to a, B?
1 def sum (f:int=>int):(int, int) =>int = {2 def func (A:int, b:int): int = { 3 if (A > B) 0 else F (a) +func (a+1, b) 4 } 5 }
SUM (x=>x) (1,5)
SUM (x=>x*x*x) (1,5)
The f is abstracted out.
Scala Functional programming