In this section,we will compute the Power ( Math.Pow in C# ) function recursively using F#
//////////////////////////
// ninth.fs
//
// A program in F# which demonstrates
// the computation of power ( x ^ n )
//
//
// Written by Praseed Pai K.T.
// http://praseedp.blogspot.com
//
//
//
// fsc ninth.fs
// ninth.exe
//
//--- Recursive computation of Power
//
// x^ 1 = x
// x^(2*n) = (x^2)^n
// X^ (2*n + 1) = x * (x^(2*n) )
//
let rec Pow( a:float , b:int ):float =
if ( b = 1 ) then a
else if b%2 = 0 then Pow(a*a , b / 2 )
else a*Pow(a*a , b / 2 )
//--- compute the power of 8^ 3
let result:float = Pow(8.0,3)
//--------- spit the result...
printf "%f" result
0 comments:
Post a Comment