Expand Minimize

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

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)

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

// Usage:
List.filter predicate list

predicate

Type: 'T -> bool

The function to test the input elements.

list

Type: 'T list

The input list.

A list containing only the elements that satisfy the predicate.

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.

The following example demonstrates the use of List.filter.

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

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
Animals with short names: [("Cats", 4); ("Dogs", 5); ("Mice", 3)]

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

F# Core Library Versions

Supported in: 2.0, 4.0, Portable

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft