F#Lists


Syntax

  • [] // an empty list.

    head::tail // a construction cell holding an element, head, and a list, tail. :: is called the Cons operator.

    let list1 = [ 1; 2; 3 ] // Note the usage of a semicolon.

    let list2 = 0 :: list1 // result is [ 0; 1; 2; 3 ]

    let list3 = list1 @ list2 // result is [ 1; 2; 3; 0; 1; 2; 3 ]. @ is the append operator.

    let list4 = [ 1..3 ] // result is [ 1; 2; 3 ]

    let list5 = [ 1..2..10 ] // result is [ 1; 3; 5; 7; 9 ]

    let list6 = [ for i in 1..10 do if i % 2 = 1 then yield i ] // result is [ 1; 3; 5; 7; 9 ]