What is a catamorphism and can it be implemented in C# 3.0?

后端 未结 5 688
闹比i
闹比i 2020-12-04 13:22

I\'m trying to learn about catamorphisms and I\'ve read the Wikipedia article and the first couple posts in the series of the topic for F# on the Inside F# blog.

5条回答
  •  遥遥无期
    2020-12-04 14:14

    I understand that it's a generalization of folds (i.e., mapping a structure of many values to one value, including a list of values to another list).

    I wouldn't say one value.It maps it into another structure.

    Maybe an example would clarify.let's say summation over a list.

    foldr (\x -> \y -> x + y) 0 [1,2,3,4,5]

    Now this would reduce to 15. But actually,it can be viewed mapping to a purely syntactic structure 1 + 2 + 3 + 4 + 5 + 0. It is just that the programming language(in the above case,haskell) knows how to reduce the above syntactic structure to 15.

    Basically,a catamorphism replaces one data constructor with another one.In case of above list,

    [1,2,3,4,5] = 1:2:3:4:5:[] (: is the cons operator,[] is the nil element) the catamorphism above replaced : with + and [] with 0.

    It can be generalized to any recursive datatypes.

提交回复
热议问题