Yes, I’m indeed asking a super easy but meaningful question. I’m wondering a really elegant (without sacrificing algorithm complexity) answer.
But here I’m asking for the most elegant way
There are some answers to this question:
Pascal’s Triangle 1
Pascal’s Triangle 2
and also fssnip. net / 2b (remove the blank)
Don’t know if the following qualifies as elegant, but it is basically the same as Gabriel Garcia’s Haskell answer from Stackoverflow:
let pascal =
let rec loop row = seq {
yield row
yield! loop [1; yield! Seq.map2 (+) row (List.tail row); 1]
}
loop [1]
This is elegant, isn’t it? Short and fast. What else do you need?
let rec PascalsTriangle = seq {
yield [1];
for aLine in PascalsTriangle ->
List.append (1::Seq.toList (Seq.map (fun (x,y) -> x+y) (Seq.pairwise aLine))) ([1])
}
printfn"%A" (PascalsTriangle |> Seq.take 10 |> Seq.toList)
???