def sum(f: Int=>Int)(a:Int)(b:Int): Int ={
@annotation.tailrec
def loop(n:Int, acc:Int):Int = {
println("n:"+n+",acc:"+acc)
if(n>b) {
acc
}
else {
loop(n+1,acc+f(n))
}
}
loop(a, 0)
}
sum(x=>x*x)(1)(5)
@annotation.tailrec
def loop(n:Int, acc:Int):Int = {
println("n:"+n+",acc:"+acc)
if(n>b) {
acc
}
else {
loop(n+1,acc+f(n))
}
}
loop(a, 0)
}
sum(x=>x*x)(1)(5)
其实python也还行,就是没有filter这个函数
def QuickSort(li=[]):
if len(li) <= 1:
return li
else:
return QuickSort([x for x in li if x < li[0]]) + \
[x for x in li if x == li[0]] + \
QuickSort([x for x in li if x > li[0]])
def QuickSort(li=[]):
if len(li) <= 1:
return li
else:
return QuickSort([x for x in li if x < li[0]]) + \
[x for x in li if x == li[0]] + \
QuickSort([x for x in li if x > li[0]])
2017-05-26