Monday, August 09, 2010

Seq.unfold

Don’t know why this had escaped me for so long!

 

let PascalsTriangle =
let nextLine aLine =
let partialLine =
aLine
|> Seq.pairwise
|> Seq.map (fun (x,y) -> x+y)
|> Seq.toList
List.append (1::partialLine) [1]
[1] |> Seq.unfold (fun s -> Some(s, nextLine s))

Compare to: Pascal’s Triangle in F#

No comments: