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

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

Den Delimarsky|Last Updated: 8/18/2017
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)


// 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: 'Tlist

The input list.

Return Value

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.


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);
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

Version Information

F# Core Library Versions

Supported in: 2.0, 4.0, Portable

See Also

Collections.List Module (F#)

Microsoft.FSharp.Collections Namespace (F#)

© 2020 Microsoft