Sunday, 29 September 2013

Why this function is not tail recursive?

Why this function is not tail recursive?

I have wrtten the following function for pascal triangle. Why the call
pascal_cont(a-1, b-1, (x:Int) => pascal_cont(a, b-1, (y:Int)=> cont (x + y)))
is not tail recursive ?
def pascal(c: Int, r: Int): Int = {
def pascal_cont(a:Int,b:Int,cont: (Int)=>Int) : Int ={
if ((a==0)||(a==b))
cont(1)
else
pascal_cont(a-1, b-1, (x:Int) => pascal_cont(a, b-1, (y:Int)=> cont
(x + y)))
}
pascal_cont(c,r,n => n)
}

No comments:

Post a Comment