F# – Lists ”; Previous Next In F#, a list is an ordered, immutable series of elements of the same type. It is to some extent equivalent to a linked list data structure. The F# module, Microsoft.FSharp.Collections.List, has the common operations on lists. However F# imports this module automatically and makes it accessible to every F# application. Creating and Initializing a List Following are the various ways of creating lists − Using list literals. Using cons (::) operator. Using the List.init method of List module. Using some syntactic constructs called List Comprehensions. List Literals In this method, you just specify a semicolon-delimited sequence of values in square brackets. For example − let list1 = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10] The cons (::) Operator With this method, you can add some values by prepending or cons-ing it to an existing list using the :: operator. For example − let list2 = 1::2::3::4::5::6::7::8::9::10::[];; [] denotes an empty list. List init Method The List.init method of the List module is often used for creating lists. This method has the type − val init : int -> (int -> ”T) -> ”T list The first argument is the desired length of the new list, and the second argument is an initializer function, which generates items in the list. For example, let list5 = List.init 5 (fun index -> (index, index * index, index * index * index)) Here, the index function generates the list. List Comprehensions List comprehensions are special syntactic constructs used for generating lists. F# list comprehension syntax comes in two forms − ranges and generators. Ranges have the constructs − [start .. end] and [start .. step .. end] For example, let list3 = [1 .. 10] Generators have the construct − [for x in collection do … yield expr] For example, let list6 = [ for a in 1 .. 10 do yield (a * a) ] As the yield keyword pushes a single value into a list, the keyword, yield!, pushes a collection of values into the list. The following function demonstrates the above methods − Example Live Demo (* using list literals *) let list1 = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10] printfn “The list: %A” list1 (*using cons operator *) let list2 = 1 :: 2 :: 3 :: [] printfn “The list: %A” list2 (* using range constructs*) let list3 = [1 .. 10] printfn “The list: %A” list3 (* using range constructs *) let list4 = [”a” .. ”m”] printfn “The list: %A” list4 (* using init method *) let list5 = List.init 5 (fun index -> (index, index * index, index * index * index)) printfn “The list: %A” list5 (* using yield operator *) let list6 = [ for a in 1 .. 10 do yield (a * a) ] printfn “The list: %A” list6 (* using yield operator *) let list7 = [ for a in 1 .. 100 do if a % 3 = 0 && a % 5 = 0 then yield a] printfn “The list: %A” list7 (* using yield! operator *) let list8 = [for a in 1 .. 3 do yield! [ a .. a + 3 ] ] printfn “The list: %A” list8 When you compile and execute the program, it yields the following output − The list: [1; 2; 3; 4; 5; 6; 7; 8; 9; 10] The list: [1; 2; 3] The list: [1; 2; 3; 4; 5; 6; 7; 8; 9; 10] The list: [”a”; ”b”; ”c”; ”d”; ”e”; ”f”; ”g”; ”h”; ”i”; ”j”; ”k”; ”l”; ”m”] The list: [(0, 0, 0); (1, 1, 1); (2, 4, 8); (3, 9, 27); (4, 16, 64)] The list: [1; 4; 9; 16; 25; 36; 49; 64; 81; 100] The list: [15; 30; 45; 60; 75; 90] The list: [1; 2; 3; 4; 2; 3; 4; 5; 3; 4; 5; 6] Properties of List Data Type The following table shows various properties of list data type − Property Type Description Head ”T The first element. Empty ”T list A static property that returns an empty list of the appropriate type. IsEmpty bool true if the list has no elements. Item ”T The element at the specified index (zero-based). Length int The number of elements. Tail ”T list The list without the first element. The following example shows the use of these properties − Example Live Demo let list1 = [ 2; 4; 6; 8; 10; 12; 14; 16 ] // Use of Properties printfn “list1.IsEmpty is %b” (list1.IsEmpty) printfn “list1.Length is %d” (list1.Length) printfn “list1.Head is %d” (list1.Head) printfn “list1.Tail.Head is %d” (list1.Tail.Head) printfn “list1.Tail.Tail.Head is %d” (list1.Tail.Tail.Head) printfn “list1.Item(1) is %d” (list1.Item(1)) When you compile and execute the program, it yields the following output − list1.IsEmpty is false list1.Length is 8 list1.Head is 2 list1.Tail.Head is 4 list1.Tail.Tail.Head is 6 list1.Item(1) is 4 Basic Operators on List The following table shows the basic operations on list data type − Value Description append : ”T list → ”T list → ”T list Returns a new list that contains the elements of the first list followed by elements of the second. average : ”T list → ^T Returns the average of the elements in the list. averageBy : (”T → ^U) → ”T list → ^U Returns the average of the elements generated by applying the function to each element of the list. choose : (”T → ”U option) → ”T list → ”U list Applies the given function to each element of the list. Returns the list comprised of the results for each element where the function returns Some. collect : (”T → ”U list) → ”T list → ”U list For each element of the list, applies the given function. Concatenates all the results and return the combined list. concat : seq<”T list> → ”T list Returns a new list that contains the elements of each the lists in order. empty :
Category: fsharp
Aug
10