In case you have ever been interested into how to display Pascal's triangle in F#, here is a solution! Went with a little bit of a different approach than usual since F# is a functional language.

raw

```
// Here is the general strategy I went with:
//
// Example, starting with the second row:
// previous row = 1 | 1
//
// Using the previous row, append a 0 to the beginning (so [1 | 1] becomes [0 | 1 | 1])
// and combine with the same sequence, but with a zero stuck on the end (so [1 | 1] becomes [1 | 1 | 0])
//
// Sequence 1: 0 | 1 | 1
// Sequence 2: 1 | 1 | 0
//
// Now sum the values together which are at the same index
//
// 0 | 1 | 1
// + 1 | 1 | 0
// -----------
// 1 | 2 | 1
//
// We can do this recursively from each row, to get the next row
//
// 1
// 0 | 1
// + 1 | 0
// -------
// 1 | 1
// 0 | 1 | 1
// + 1 | 1 | 0
// -----------
// 1 | 2 | 1
// 0 | 1 | 2 | 1
// + 1 | 2 | 1 | 0
// ---------------
// 1 | 3 | 3 | 1
// 0 | 1 | 3 | 3 | 1
// + 1 | 3 | 3 | 1 | 0
// -------------------
// 1 | 4 | 6 | 4 | 1
//
// The results leave us with pascal's triange
//
// 1
// 1 | 1
// 1 | 2 | 1
// 1 | 3 | 3 | 1
// 1 | 4 | 6 | 4 | 1
//
```

Knowing all of that, here is the F# which gets it all done. By the way, the reason for using BigIntegers ( [0I] <- the I means BigInteger) is because these number grow very large, very quickly.

raw

```
let pascalshelper previousRow =
// Create a sequence with a zero prepended
let leftseq = seq {yield 0I; yield! previousRow}
// Create a sequence with a zero appended
let rightseq = seq {yield! previousRow; yield 0I}
// Zip up the two sequences by adding the values together at each index
Seq.map2 (fun a b -> a + b) leftseq rightseq |> Seq.toList
let rec pascals depth =
// Base case is the first row, which just has a single item
let currentRow = match depth with
| 1 -> [ 1I; ]
| _ -> pascals(depth - 1) |> pascalshelper
// Print out the list
printfn "%A" currentRow
currentRow
[<EntryPoint>]
let main argv =
pascals 10 |> ignore
0 // exit 0
```

Here is a bit shorter (mostly because I made the variable/function names shorter) solution which does the exact same thing, but is a little harder to follow. Also, instead of using "yield", Seq.append can be used to concatenate two arrays.

raw

```
let h l =
Seq.map2 (fun a b -> a + b) (Seq.append [0I] l) (Seq.append l [0I]) |> Seq.toList
let rec p x =
let r = match x with
| 1 -> [1I]
| _ -> p(x - 1) |> h
printfn "%A" r
r
[<EntryPoint>]
let main argv =
p 10 |> ignore
0
```

There you have it! To show a comparison, here is the same thing in C#

raw

```
class Program
{
static BigInteger[] PascalsHelper(BigInteger[] previousList)
{
var zero = new[] { new BigInteger(0) };
var leftSide = zero.Concat(previousList);
var rightSide = previousList.Concat(zero);
var result = new BigInteger[previousList.Length + 1];
using (var leftEnum = leftSide.GetEnumerator())
using (var rightEnum = rightSide.GetEnumerator())
{
// We know the length of the enumerables, so no need to bounds check the MoveNext() calls.
for(var i = 0; i < result.Length; i++)
{
leftEnum.MoveNext();
rightEnum.MoveNext();
result[i] = leftEnum.Current + rightEnum.Current;
}
}
return result;
}
static BigInteger[] Pascals(int depth)
{
var current = 1 == depth ? new[] { new BigInteger(1) }
: PascalsHelper(Pascals(depth - 1));
Console.WriteLine(String.Join(" ", current));
return current;
}
static void Main(string[] args)
{
Pascals(10);
}
}
```

The solution doesn't seem very "C#" to me. But it works!