Share via


List.findIndex<'T>-Funktion (F#)

Aktualisiert: August 2010

Gibt den Index des ersten Elements in der Liste zurück, das das angegebene Prädikat erfüllt. Löst KeyNotFoundException aus, wenn kein solches Element vorhanden ist.

Namespace/Modulpfad: Microsoft.FSharp.Collections.List

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

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

// Usage:
List.findIndex predicate list

Parameter

  • predicate
    Typ: 'T -> bool

    Die Funktion zum Testen der Eingabeelemente.

  • list
    Typ: 'T list

    Die Eingabeliste.

Ausnahmen

Ausnahme

Bedingung

ArgumentException

Wird ausgelöst, wenn das Prädikat für alle Elemente der Liste false ergibt.

Rückgabewert

Der Index des ersten Elements, das das Prädikat erfüllt.

Hinweise

Der Name dieser Funktion in kompilierten Assemblys lautet FindIndex. Verwenden Sie diesen Namen, wenn Sie in einer anderen .NET-Sprache als F# oder durch Reflektion auf die Funktion zugreifen.

Beispiel

Im folgenden Code wird veranschaulicht, wie List.findIndex verwendet wird. Außerdem wird das Verhalten mit dem von List.find. verglichen.

let list1 = [ 2 .. 100 ]
let delta = 1.0e-10
let isPerfectSquare (x:int) =
    let y = sqrt (float x)
    abs(y - round y) < delta
let isPerfectCube (x:int) =
    let y = System.Math.Pow(float x, 1.0/3.0)
    abs(y - round y) < delta
let element = List.find (fun elem -> isPerfectSquare elem && isPerfectCube elem) list1
let index = List.findIndex (fun elem -> isPerfectSquare elem && isPerfectCube elem) list1
printfn "The first element that is both a square and a cube is %d and its index is %d." element index

Output

  

Plattformen

Windows 7, Windows Vista SP2, Windows XP SP3, Windows XP x64 SP2, Windows Server 2008 R2, Windows Server 2008 SP2, Windows Server 2003 SP2

Versionsinformationen

F#-Runtime

Unterstützt in: 2.0, 4.0

Silverlight

Unterstützt in: 3

Siehe auch

Weitere Ressourcen

Collections.List-Modul (F#)

Microsoft.FSharp.Collections-Namespace (F#)

Änderungsprotokoll

Datum

Versionsgeschichte

Grund

August 2010

Codebeispiel hinzugefügt.

Informationsergänzung.