Table of contents
TOC
Collapse the table of content
Expand the table of content

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

Den Delimarsky|Last Updated: 7/5/2016
|
1 Contributor

Returns a new collection containing only the elements of the collection for which the given predicate returns true.

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

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

Syntax

// Signature:
List.filter : ('T -> bool) -> 'T list -> 'T list

// Usage:
List.filter predicate list

Parameters

predicate Type: 'T ->bool

The function to test the input elements.

list Type: 'Tlist

The input list.

Return Value

A list containing only the elements that satisfy the predicate.

Remarks

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

Example

let evenOnlyList = List.filter (fun x -> x % 2 = 0) [1; 2; 3; 4; 5; 6]

Output

The resulting list is [2; 4; 6].****The following example shows another typical use for List.filter.

    let data = [("Cats",4);
                ("Dogs",5);
                ("Mice",3);
                ("Elephants",2)]
    let res = data |> List.filter (fun (nm,x) -> nm.Length <= 4)
    printfn "Animals with short names: %A" res

Output

Animals with short names: [("Cats", 4); ("Dogs", 5); ("Mice", 3)]

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

Collections.List Module (F#)

Microsoft.FSharp.Collections Namespace (F#)

© 2016 Microsoft