2 people like it.

Pascal's Triangle

Creates a jagged 2d list of Pascal's Triangle.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
let pascal rows = // Create pascal's triangle as a jagged 2d list
  let pascalRow row = //Calculates a row of the triangle
    List.init (row + 1) (fun i -> i) //Create list of which the element values are the index
    |> List.scan (fun prev index -> //prev = previous element of this row, index=current element number of row
      if index = 0 then //Special case for the 1st element
        1
      else //Calculate the element at this index based on the previous value
        prev * (row + 1 - index)/index) 1
    |> List.tail //Remove the collected value to obtain the row
  List.init rows pascalRow // Create a new array of pascal row

pascal 3 //Four rows of Pascal's triangle, including row 0,1,2
// [[1]; [1; 1]; [1; 2; 1]]
val pascal : rows:int -> int list list

Full name: Script.pascal
val rows : int
val pascalRow : (int -> int list)
val row : int
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member GetSlice : startIndex:int option * endIndex:int option -> 'T list
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val init : length:int -> initializer:(int -> 'T) -> 'T list

Full name: Microsoft.FSharp.Collections.List.init
val i : int
val scan : folder:('State -> 'T -> 'State) -> state:'State -> list:'T list -> 'State list

Full name: Microsoft.FSharp.Collections.List.scan
val prev : int
val index : int
val tail : list:'T list -> 'T list

Full name: Microsoft.FSharp.Collections.List.tail
Raw view New version

More information

Link:http://fssnip.net/7X5
Posted:1 month ago
Author:Kayden Tebau
Tags: algebra , list , lists , math , pascal , triangle