List.fold<'T,'State> İşlevi (F#)

Geçerli bir işlev f accumulator argüman hesaplaması aracılığıyla her koleksiyon öğesi için iş parçacığı oluşturma.fold İşlev ikinci bağımsız değişken alır ve işlevi uygular f ve listedeki ilk öğe.Daha sonra tabloya işlev bu sonuç akışları f yanı sıra ikinci öğe ve böyle devam eder.Nihai sonuç verir.Giriş işlevi ise, f ve öğeleri, i0...iN, sonra da bu işlevi hesaplar f (... (f s i0) i1 ...) iN.

Ad alanı/modül yolu: Microsoft.FSharp.Collections.List

Derleme: FSharp.Core (FSharp.Core.dll),

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

// Usage:
List.fold folder state list

Parametreler

  • folder
    Türü:'State -> 'T -> 'State

    Verilen giriş öğelerine göre durumu güncellemek için işlev.

  • state
    Türü:'State

    Başlangıç durumu.

  • list
    Type: 'Tlist

    Giriş listesi.

Dönüş Değeri

Son durum değeri.

Notlar

Bu işlev adlı Fold kodları derlenmiş derlemeleri.İşlev yansıtma veya F# dışındaki bir dilde erişiyorsanız, bu adı kullanın.

Örnek

Aşağıdaki örnek kullanımını göstermektedir.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
  

Aşağıdaki kod örneği ek kullanımları gösterilmektedir List.fold.Kitaplığı işlevlerini zaten altına uygulanmış işlevleri kapsülleyen bulunduğunu unutmayın.Örneğin, List.sum tüm öğeleri listesi eklemek kullanılabilir.

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

Çıktı

  
  

Platformlar

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

Sürüm Bilgisi

F# Çekirdek Kitaplığı sürümleri

Desteklenen: 2.0, 4.0, Portable

Ayrıca bkz.

Başvuru

Collections.List Modülü (F#)

Microsoft.FSharp.Collections Ad Alanı (F#)