How does foldr work?

后端 未结 10 2049
别跟我提以往
别跟我提以往 2020-12-04 06:37

Can anybody explain how does foldr work?

Take these examples:

Prelude> foldr (-) 54 [10, 11]
53
Prelude> foldr (\\x y -> (x+y)/2) 54 [12, 4,         


        
10条回答
  •  Happy的楠姐
    2020-12-04 07:26

    foldr means fold from the right, so foldr (-) 0 [1, 2, 3] produces (1 - (2 - (3 - 0))). In comparison foldl produces (((0 - 1) - 2) - 3).

    When the operators are not commutative foldl and foldr will get different results.

    In your case, the first example expands to (10 - (11 - 54)) which gives 53.

提交回复
热议问题