Functional style
Avoid iterative programming using loops like For
or Do
, use instead functional programming functions Map
, Scan
, MapThread
, Fold
, FoldList
, ... and pure functions. This makes the code cleaner and faster.
Transpose and dimensions
Something not easy to guess alone at the beginning: if you have x={1,2}
and y={3,4}
,
doing Transpose[{x,y}]
or {x,y}
ESC tr ESC in the front end will produce {{1,3},{2,4}}
(format compatible with ListPlot
). This animation helps understand why.
You can also use the second argument of Transpose
to reorder the indices of a multidimensional list.
Don't forget to regularly control the output of the lists you generate using Dimensions
.
Get familiar with shorthand syntax (@
, &
, ##
, /@
, /.
, etc.)
Programming easily
Getting help: Execute ?Map
for example for a short description of a function, or press F1 on a function name for more details and examples about it. You can solve many problems by adapting examples to your needs.
Auto-completion: Start typing the name of a function and (in Mathematica 9+) select from the pop-up auto-completion menu, or press Ctrl+k to get a list of functions which names start with what has already been entered. Once the name of the function is written completely press Ctrl+Shift+k (on Mac, Cmd+k) to get a list of its arguments.
Function templates: In Mathematica 9, after typing a function name, press Ctrl+Shift+k (on Mac, Cmd+Shift+k) and click on the desired form from the pop-up menu to insert a template with named placeholders for the arguments.
Other useful shortcuts are described in the post Using the Mathematica front-end efficiently for editing notebooks.
Use palettes in the Palettes menu especially when you're beginning.
In Mathematica 8, use the natural input capability of Wolfram Alpha, for example type "= graph 2 x + 1 between 0 and 3" without the quotes and see the command associated with the result.