Questions on how to write code in a better or different style, using Mathematica's capabilities for coding in multiple styles.
Mathematica differs from other programming languages in that it allows users to write their code in a variety of styles.
- Procedural programming of the kind familiar to users of C or Fortran. The canonical constructs of this style are the
Forloop and it is heavily relying on saving intermediate values into variables. This is the kind of programming most novice Mathematica users resort to, and it is usually the least efficient.
- Functional programming is considered more canonically "Mathematica-style". Familiar to uses of LISP. The typical constructs in functional programming are
MapThreadand pure functions (
- Rule-based programming uses Mathematica's pattern-matching engine to match and rewrite expressions. A typical construction in this style is the replacement rule e.g.
a -> bor
a :> b, which is involved using