Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Expand Minimize

List.choose<'T,'U> Function (F#)

Applies the given function f to each element x of the list. Returns the list comprised of the results for each element where the function returns Some(f(x)).

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

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

// Signature:
List.choose : ('T -> 'U option) -> 'T list -> 'U list

// Usage:
List.choose chooser list


Type: 'T -> 'U option

The function to generate options from the elements.


Type: 'T list

The input list.

The list comprising the values selected from the chooser function.

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

The following code demonstrates the use of List.choose to select capitalized words out of a list of words.

let listWords = [ "and"; "Rome"; "Bob"; "apple"; "zebra" ]
let isCapitalized (string1:string) = System.Char.IsUpper string1.[0]
let results = List.choose (fun elem ->
    match elem with
    | elem when isCapitalized elem -> Some(elem + "'s")
    | _ -> None) listWords
printfn "%A" results


["Rome's"; "Bob's"]

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

F# Core Library Versions

Supported in: 2.0, 4.0, Portable

© 2015 Microsoft