I'm working through MMA Cookbook by Mangano, page 54. He shows Map
is a compact form of Fold
function. Is using Fold
function important?
myMap3[f_, l_] := Fold[Prepend[#1, f[#2]]&, {}, l]
myMap3[Sqrt, {1, 2, 3}]
(* Out: {Sqrt[3], Sqrt[2], 1} *)
I value writing better MMA code. Mastering Functional forms, then moving into Rewrite-rules. Thanks in advance.
Fold
solves problems thatMap
does not (at least not without uncomfortable contortions).Map
is perhaps more intuitive to me and certainly very convenient. It also has a nice infix form (/@
), which is handy. You would certainly want to be aware of both to write idiomatic MMA code. $\endgroup$Fold
can solve problems thatMap
cannot. I have not read Mangano's book, but to me, the more interesting comparison is betweenFold
and recursive functions. Understanding why recursion is more powerful thanFold
is valuable exercise. $\endgroup$