.net - is there any difference between reduce and reduceBack -
i'm learning f# msdn , looking , trying out reduce , reduce back, can't find difference, signature same
('t -> 't -> 't) -> 't list -> 't
and both throw same error on empty list, why there 2 of them, there should difference
others explained difference - reduce elements in different order.
for of operations can use reduce
or reduceback
, difference not matter. in more mathematical terms, if operation associative (such numeric operations, max, min or sum functions, list concatenation, etc.) 2 behave same.
an example can nicely see difference building tree, because shows how evaluation works:
type tree = | leaf of int | node of tree * tree [ n in 0 .. 3 -> leaf n] |> list.reduce (fun b -> node(a, b)) [ n in 0 .. 3 -> leaf n] |> list.reduceback (fun b -> node(a, b))
here 2 trees result (but note if flatten them, same list!)
reduce reduceback ------------------------------------- tree: /\ /\ /\ 3 0 /\ /\ 2 1 /\ 0 1 2 3 ------------------------------------- flat: 0 1 2 3 0 1 2 3
Comments
Post a Comment