Sdílet prostřednictvím


List.fold<'T,'State> – funkce (F#)

Použije funkci f pro každý prvek kolekce threading argument akumulátorové prostřednictvím výpočtu.fold Funkce druhý argument a použije funkci f a první prvek seznamu.Potom kanály tento výsledek do funkce f druhý prvek a tak dále.Vrací konečný výsledek.Jestliže je vstupní funkce f a jsou i0...iN, pak tato funkce vypočítá f (... (f s i0) i1 ...) iN.

Cesta k oboru názvů nebo modul: Microsoft.FSharp.Collections.List

Sestavení: FSharp.Core (v 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

    Funkce Aktualizovat stav dané vstupní prvky.

  • state
    Typ:'State

    Počáteční stav.

  • list
    Type: 'Tlist

    Vstupní seznam.

Vrácená hodnota

Hodnota konečného stavu.

Poznámky

Tato funkce se nazývá Fold v kompilovaný sestavení.Pokud přistupujete k funkci jazyka než F# nebo prostřednictvím reflexe, tento název použijte.

Příklad

Následující příklad ukazuje použití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
  

Následující příklad kódu ukazuje další využití List.fold.Poznámka: funkcí knihovny existují, již zapouzdřit funkce implementována níže.Například List.sum je k dispozici pro přidávání prvků seznamu.

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

Výsledek

  
  

Platformy

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

Informace o verzi

F# základní verze knihovny

Podporovány: 2.0, 4.0, přenosné

Viz také

Referenční dokumentace

Collections.List – modul (F#)

Microsoft.FSharp.Collections – obor názvů (F#)