Udostępnij za pośrednictwem


List.fold<'T,'State> — Funkcja (F#)

Dotyczy funkcji f do każdego elementu w kolekcji threading argumentu akumulator za pomocą obliczeń.fold Funkcja przyjmuje drugi argument i dotyczy funkcji f , pierwszy element listy.Następnie źródła wynik do funkcji f z drugiego elementu itd.Zwraca wynik końcowy.Jeśli funkcja wejściowy jest f i elementy są i0...iN, a następnie funkcja oblicza f (... (f s i0) i1 ...) iN.

Ścieżka obszaru nazw/modułu: Microsoft.FSharp.Collections.List

Zestaw: FSharp.Core (w FSharp.Core.dll)

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

// Usage:
List.fold folder state list

Parametry

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

    Funkcja aktualizacji stanu dane wejściowe elementy.

  • state
    Typ:'State

    Stan początkowy.

  • list
    Type: 'Tlist

    Lista wejściowego.

Wartość zwracana

Wartość Stan końcowy.

Uwagi

Ta funkcja o nazwie Fold w skompilowane zestawy.Jeżeli języka, niż F# lub przez odbicie, uzyskują dostęp do funkcji, należy użyć tej nazwy.

Przykład

Poniższy przykład ilustruje użycieList.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
  

Poniższy przykład kodu pokazuje dodatkowe zastosowania List.fold.Uwaga funkcji biblioteki istnieją, które już upakować funkcje realizowane poniżej.Na przykład List.sum jest dostępny do dodawania elementów listy.

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

Dane wyjściowe

  
  

Platformy

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

Informacje o wersji

F# Core wersji biblioteki

Obsługiwane: 2.0, 4.0, przenośne

Zobacz też

Informacje

Collections.List — Moduł (F#)

Microsoft.FSharp.Collections — Przestrzeń nazw (F#)