List.fold2<'T1,'T2,'State> — Funkcja (F#)

Dotyczy funkcji odpowiednie elementy zbiorów dwóch wątków argumentu akumulator za pomocą obliczeń.Kolekcje muszą mieć identyczne rozmiarów.Jeśli funkcja wejściowy jest f i elementy są i0...iN i j0...jN następnie oblicza f (... (f s i0 j0)...) iN jN.

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

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

// Signature:
List.fold2 : ('State -> 'T1 -> 'T2 -> 'State) -> 'State -> 'T1 list -> 'T2 list -> 'State

// Usage:
List.fold2 folder state list1 list2

Parametry

  • folder
    Typ:'State -> 'T1 -> 'T2 -> 'State

    Funkcja aktualizacji stanu dane wejściowe elementy.

  • state
    Typ:'State

    Stan początkowy.

  • list1
    Type: 'T1list

    Pierwsza lista wejściowego.

  • list2
    Type: 'T2list

    Wejściowy drugiej listy.

Wartość zwracana

Wartość Stan końcowy.

Wyjątki

Wyjątek

Warunek

ArgumentException

Zgłoszony podczas wprowadzania listy różnią się długością.

Uwagi

Ta funkcja o nazwie Fold2 w skompilowane zestawy.Jeśli funkcja dostęp z języka .net niż F# lub przez odbicie, należy użyć tej nazwy.

Przykład

Poniższy przykład kodu ilustruje użycie List.fold2.

// Use List.fold2 to perform computations over two lists (of equal size) at the same time. 
// Example: Sum the greater element at each list position. 
let sumGreatest list1 list2 = List.fold2 (fun acc elem1 elem2 ->
                                              acc + max elem1 elem2) 0 list1 list2

let sum = sumGreatest [1; 2; 3] [3; 2; 1]
printfn "The sum of the greater of each pair of elements in the two lists is %d." sum

Dane wyjściowe

  

Poniższy przykład kodu ilustruje użycie List.fold2 obliczyć saldo końcowe konta bankowego po serii transakcji.Dwie listy wejściowe reprezentuje typ transakcji (lub wycofanie) i Kwota transakcji.

// Discriminated union type that encodes the transaction type. 
type Transaction =
    | Deposit
    | Withdrawal

let transactionTypes = [Deposit; Deposit; Withdrawal]
let transactionAmounts = [100.00; 1000.00; 95.00 ]
let initialBalance = 200.00

// Use fold2 to perform a calculation on the list to update the account balance. 
let endingBalance = List.fold2 (fun acc elem1 elem2 ->
                                match elem1 with
                                | Deposit -> acc + elem2
                                | Withdrawal -> acc - elem2)
                                initialBalance
                                transactionTypes
                                transactionAmounts
printfn "%f" endingBalance

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