2
$\begingroup$

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.

$\endgroup$
4
  • 2
    $\begingroup$ Sure. Fold solves problems that Map 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$
    – MarcoB
    Commented May 15, 2020 at 21:36
  • 1
    $\begingroup$ As @MarcoB notes, Fold can solve problems that Map cannot. I have not read Mangano's book, but to me, the more interesting comparison is between Fold and recursive functions. Understanding why recursion is more powerful than Fold is valuable exercise. $\endgroup$ Commented May 15, 2020 at 22:17
  • $\begingroup$ Thank you, now I get it. @Shredderroy $\endgroup$
    – prog9910
    Commented May 15, 2020 at 23:35
  • 1
    $\begingroup$ Magnano's point was, "so you can understand" what is happening inside of the Map[ command, by a drawn out equivalent. I'm really enjoying trying to get inside MMA head. Just everything is so abstract & full of symbols. $\endgroup$
    – prog9910
    Commented May 16, 2020 at 3:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.