简体   繁体   English

这个折叠树函数如何在Haskell中工作

[英]How this Fold Tree Function works in Haskell

Here i am trying to understand this function which folds a tree to one value. 在这里,我试图理解这个将树折叠成一个值的函数。 It shows that foldTree takes two functions as arguments, it applies first function to the element of the Tree a and then passes the result to second function (b->b->b) which again performs some operation and produces final result. 它显示foldTree将两个函数作为参数,它将第一个函数应用于Tree a的元素,然后将结果传递给第二个函数(b->b->b) ,再次执行某些操作并生成最终结果。

foldTree :: (a -> b) -> (b -> b -> b) -> Tree a -> b
foldTree f g (Leaf x) = f x
foldTree f g (Node tl tr) = g (foldTree f g tl) (foldTree f g tr) 

I am trying following input to see how it works. 我正在尝试关注输入,看看它是如何工作的。

Input 1:  foldTree (*2) (\x y -> x + y) (Leaf 6)
Input 2:  foldTree (*2) (\x y -> x + y) (Node (Node (Leaf 1) (Leaf 2)) (Leaf 4))

It returns 它回来了

Output 1 : 12
Output 2 : 14

The problem i am facing in understanding is where the second function argument operation is performed? 我在理解中遇到的问题是执行第二个函数参数操作的位置? and how it actually passes the values to the second function, apparently second function takes two values as arguments but first function only returns one value... from where the second value is passed? 以及它如何实际将值传递给第二个函数,显然第二个函数将两个值作为参数但第一个函数只返回一个值...从第二个值传递的位置? Can i say it took result of first function twice so that x and y values are same? 我能说两次第一次函数的结果,以便xy值相同吗? because second function (\\xy -> x + y) takes two arguments? 因为第二个函数(\\xy -> x + y)两个参数? but then result would not be same as mentioned in output 1 and 2. 但结果与输出1和2中提到的结果不同。

Secondly does it return the final result after performing second function or it returns the result after applying only the first function because i am confused even i removed second function it produces the same results. 其次,它在执行第二个函数后返回最终结果,或者在仅应用第一个函数后返回结果,因为我很困惑,即使我删除了第二个函数,它也会产生相同的结果。

Thirdly what is the purpose of g here outside of two curly brackets? 第三, g在两个大括号之外的目的是什么? ***g*** (foldTree fg tl) (foldTree fg tr) it took it as a Data constructor from the start or what? ***g*** (foldTree fg tl) (foldTree fg tr)它从一开始就把它当作数据构造函数或者是什么? I know data constructor can be constructed as smart constructors but here how it treated. 我知道数据构造函数可以构造为智能构造函数,但在这里它是如何处理的。

I am so confused understanding this, may be i am complicating a lot of concepts in this one? 我很难理解这一点,可能是我在这个问题上复杂化了很多概念? Please don't hesitate to go into details. 请不要犹豫,详细了解。

To understand the result of foldTree fg tree you can use this technique: 要了解foldTree fg tree的结果,您可以使用以下技术:

  • Write down the value of tree using the constructors, eg in your example we have (Node (Node (Leaf 1) (Leaf 2)) (Leaf 4)) . 使用构造函数记下tree的值,例如在我们的示例中(Node (Node (Leaf 1) (Leaf 2)) (Leaf 4))
  • Syntactically replace Leaf with f and Node with g . f语法替换Leaf ,用g替换Node For the previous example, we get (g (g (f 1) (f 2)) (f 4)) . 对于前面的例子,我们得到(g (g (f 1) (f 2)) (f 4))
  • Use the definitions of functions f and g to compute the result of the last expression. 使用函数fg的定义来计算最后一个表达式的结果。 For the example, we get ((+) ((+) ((*2) 1) ((*2) 2)) ((*2) 4)) which is ((+) ((+) (1*2) (2*2)) (4*2)) which is ((1*2) + (2*2)) + (4*2) which is (2+4)+8 = 14 . 例如,我们得到((+) ((+) ((*2) 1) ((*2) 2)) ((*2) 4)) ,即((+) ((+) (1*2) (2*2)) (4*2))((1*2) + (2*2)) + (4*2) ,即(2+4)+8 = 14

Note how we replace Leaf a unary constructor with f , a unary function, and Node , a binary constructor with g , a binary function. 注意我们如何使用f (一元函数)和带有g (二元函数)的二进制构造函数Node替换Leaf一元构造函数。 So, we always have the right number of arguments for our functions. 因此,我们总是为函数提供正确数量的参数。 More in general, the types will match just fine. 更一般地说,类型将匹配得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM