6 people like it.

Fibonacci sequence with fold

Fibonacci nth term with fold

1: 
let fibonacci n = [|1..n|] |> Array.fold (fun (a,b) _ -> b, a + b) (0,1) |> fst
val fibonacci : n:int -> int

Full name: Script.fibonacci
val n : int
module Array

from Microsoft.FSharp.Collections
val fold : folder:('State -> 'T -> 'State) -> state:'State -> array:'T [] -> 'State

Full name: Microsoft.FSharp.Collections.Array.fold
val a : int
val b : int
val fst : tuple:('T1 * 'T2) -> 'T1

Full name: Microsoft.FSharp.Core.Operators.fst

More information

Link:http://fssnip.net/rT
Posted:9 years ago
Author:Andriy Tolstoy
Tags: fibonacci , fold , array