This project is read-only.
Declaring a simple recursive function

Category:
Recursion
Description: Declaring a simple recursive function
Code:
let SampleRec1() =
let rec fib n = if n < 2 then 1 else fib (n-1) + fib (n-2)
for i = 1 to 10 do
printfn "fib %d = %d" i (fib i)

//--------------------------------------------------------

Execution Result:
fib 1 = 1
fib 2 = 2
fib 3 = 3
fib 4 = 5
fib 5 = 8
fib 6 = 13
fib 7 = 21
fib 8 = 34
fib 9 = 55
fib 10 = 89

Last edited Sep 14, 2011 at 3:26 AM by ttliu2000, version 1

Comments

No comments yet.