Foldr in C#
Sunday, February 10th, 2008Do you know Foldr? Foldr is a function we can find in most of functional programming languages like Haskell (Foldr) or F# (List.Fold_r). It is useful when you need to process all elements in a list and return just one result value (not list), such as ’sum all numbers in a list’.
Foldr is not part of .NET framework, implementation of this function you can find at the end of this article.
Foldr takes list of elements (list), default value (def) which behaves like a last value of the list and function (fun) which is applied to every two adjacent elements.
form: Foldr ( list, def, fun )
How does it work? Let’s say you need to sum all elements in the list…
Theory
form: Foldr ( list, def, fun )
code: Foldr ( { 1, 2, 3, 4 }, 0, + )
execution: 1 + (2 + (3 + (4 + 0))) = 10


Naučte se WPF - český tutoriál