Partager via


List.init<'T> Function (F#)

Creates a list by calling the given generator on each index.

Namespace/Module Path: Microsoft.FSharp.Collections.List

Assembly: FSharp.Core (in FSharp.Core.dll)

// Signature:
List.init : int -> (int -> 'T) -> 'T list

// Usage:
List.init length initializer

Parameters

  • length
    Type: int

    The length of the list to generate.

  • initializer
    Type: int -> 'T

    The function to generate an element from an index.

Return Value

The list of generated elements.

Remarks

This function is named Initialize in compiled assemblies. If you are accessing the function from a language other than F#, or through reflection, use this name.

Example

The following code shows how to use List.init.

printfn "List of squares: %A" (List.init 10 (fun index -> index * index))

Output

List of squares: [0; 1; 4; 9; 16; 25; 36; 49; 64; 81]

Platforms

Windows 8, Windows 7, Windows Server 2012, Windows Server 2008 R2

Version Information

F# Core Library Versions

Supported in: 2.0, 4.0, Portable

See Also

Reference

Collections.List Module (F#)

Microsoft.FSharp.Collections Namespace (F#)