List.fold<'T,'State>-Funktion (F#)

Wendet eine Funktion f auf jedes Element der Auflistung an, wobei ein Akkumulatorargument in der Berechnung mitgeführt wird.Die fold-Funktion verwendet das zweite Argument, und wendet die Funktion f auf das Argument und auf das erste Element in der Liste an.Anschließend wird dieses Ergebnis zusammen mit dem zweiten Element usw. an die Funktion f übergeben.Sie gibt das Endergebnis zurück.Ist die Eingabefunktion f und sind die Elemente i0...iN berechnet diese Funktion f (... (f s i0) i1 ...) iN.

Namespace/Modulpfad: Microsoft.FSharp.Collections.List

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

// Signature:
List.fold : ('State -> 'T -> 'State) -> 'State -> 'T list -> 'State

// Usage:
List.fold folder state list

Parameter

  • folder
    Typ: 'State -> 'T -> 'State

    Die Funktion, mit der der Zustand der Eingabeelemente aktualisiert wird.

  • state
    Typ: 'State

    Der Ausgangszustand.

  • list
    Typ: 'Tlist

    Die Eingabeliste.

Rückgabewert

Der endgültige Zustandswert.

Hinweise

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

Beispiel

Das folgende Beispiel veranschaulicht die Verwendung von List.fold.

let data = [("Cats",4);
            ("Dogs",5);
            ("Mice",3);
            ("Elephants",2)]
let count = List.fold (fun acc (nm,x) -> acc+x) 0 data
printfn "Total number of animals: %d" count
  

Im folgenden Codebeispiel werden weitere Verwendungsmöglichkeiten von List.fold veranschaulicht.Beachten Sie, dass Bibliotheksfunktionen vorhanden sind, die bereits die Funktionalität kapseln, die unten implementiert wird.Beispielsweise ist List.sum verfügbar, um alle Elemente einer Liste hinzuzufügen.

let sumList list = List.fold (fun acc elem -> acc + elem) 0 list
printfn "Sum of the elements of list %A is %d." [ 1 .. 3 ] (sumList [ 1 .. 3 ])

// The following example computes the average of a list.
let averageList list = (List.fold (fun acc elem -> acc + float elem) 0.0 list / float list.Length)

// The following example computes the standard deviation of a list.
// The standard deviation is computed by taking the square root of the
// sum of the variances, which are the differences between each value
// and the average.
let stdDevList list =
    let avg = averageList list
    sqrt (List.fold (fun acc elem -> acc + (float elem - avg) ** 2.0 ) 0.0 list / float list.Length)

let testList listTest =
    printfn "List %A average: %f stddev: %f" listTest (averageList listTest) (stdDevList listTest)

testList [1; 1; 1]
testList [1; 2; 1]
testList [1; 2; 3]

// List.fold is the same as to List.iter when the accumulator is not used.
let printList list = List.fold (fun acc elem -> printfn "%A" elem) () list
printList [0.0; 1.0; 2.5; 5.1 ]

// The following example uses List.fold to reverse a list.
// The accumulator starts out as the empty list, and the function uses the cons operator
// to add each successive element to the head of the accumulator list, resulting in a
// reversed form of the list.
let reverseList list = List.fold (fun acc elem -> elem::acc) [] list
printfn "%A" (reverseList [1 .. 10])

Output

  
  

Plattformen

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

Versionsinformationen

F#-Kern-Bibliotheks-Versionen

Unterstützt in: 2,0, 4,0, portablen

Siehe auch

Referenz

Collections.List-Modul (F#)

Microsoft.FSharp.Collections-Namespace (F#)