Where can I find examples of good Mathematica programming practice?

Here's a collection of resources that I started on Mathgroup (a collection of Mathematica learning resources) and updated here at Stack Overflow. As this site is dedicated to Mathematica it makes more sense to maintain it here. This represents a huge amount of information; of course it's not exhaustive so feel free to improve it! Also, don't hesitate to share it and suggest other interesting links! Remember, you can always search the online Documentation Center of Mathematica, that is identical to the built-in help of the latest software version.

Links to more advanced aspects of the program that you can start to appreciate once you understand the basics are provided in separate answers (below) as this post became too large.

  • Tips and Tricks
  • Advanced evaluation, patterns and neat algorithms

Introduction

  • If you're just beginning try to have a look at these videos.
    Mathematica Basics, Elementary Programming in Mathematica
    Hands-on Start to Mathematica
    Several introductory videos by Jon McLoone
    and many other video introductions and tutorials from the official Wolfram website
  • An elementary introduction to the Wolfram language
    Fast introduction for programmers
  • Is it necessary to have a prior computational background or is it possible to learn Mathematica as a first programming language?
  • What are the most common pitfalls awaiting new users?
  • How To-s: full solutions for particular tasks from the online documentation
  • Easy-to-understand animations explaining common Mathematica functions
  • Sal Mangano's videos for using pure functions, Part and patterns
  • Introductory videos of various applications of Mathematica
  • What is the best Mathematica tutorial for young people?

Basic advices for people new to Mathematica

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.

  • Functional Programming, Functional Programming: Quick Start
  • Pure functions
    What does # mean in Mathematica?
  • Alternatives to procedural loops and iterating over lists in Mathematica
  • An example: Programming a numerical method in the functional style
  • How to understand the usage of Inner and Outer figuratively?

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.)

  • Operator Input Forms
  • when is f@g not the same as f[g]?

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.

Tutorials

  • An elementary introduction to the Wolfram language, by Stephen Wolfram
    Fast introduction for programmers
  • Fundamentals of Mathematica Programming (by Richard Gaylord, great tutorial for an overview of the logic behind Mathematica: patterns)
    Video tutorial also available
  • Introduction to Mathematica (by Thomas Hahn, another succinct overview of Mathematica)
  • Tutorial Collection by WRI (lots of extra documentation and examples, available as free PDFs, also available and up-to-date in Help > Virtual Book in Mathematica).
  • Programming Paradigms via Mathematica (A First Course)
  • Mathematica Tutorial: A New Resource for Developers
  • Wolfram's Mathematica 101
  • http://bmia.bmt.tue.nl/Software/Downloads/Campus/TrainingMathematicaEnglish.zip
    http://bmia.bmt.tue.nl/Software/Mathematica/Tutorials/index.html
  • A problem centered approach
  • A beginner's guide to Mathematica
  • http://math.sduhsd.net/MathematiClub/tutorials.htm
  • http://www.austincc.edu/mmcguff/mathematica/
  • http://www.mtholyoke.edu/courses/hnichols/phys303/
  • http://www.apam.columbia.edu/courses/ap1601y/ (Introduction to Computational Mathematics and Physics)
  • http://ftp.physics.uwa.edu.au/pub/MATH2200/2012/Lectures/ (Applied Mathematics)
    http://ftp.physics.uwa.edu.au/pub/MATH2200/2009/Lectures (path for some lectures in pdf)
  • http://en.wikibooks.org/wiki/Mathematica
  • http://www.cs.purdue.edu/homes/ayg/CS590C/www/mathematica/math.html (Basic tutorial)
  • https://stackoverflow.com/questions/4430998/mathematica-what-is-symbolic-programming (What is symbolic programming)
  • http://www.cer.ethz.ch/resec/people/tsteger/Econ_Model_Math_1.pdf
  • http://www.physics.umd.edu/enp/jjkelly (An introduction to Mathematica as well as some physics courses)
  • Do you know of any web-based university course that is entirely Mathematica based?
  • http://homepage.cem.itesm.mx/jose.luis.gomez/data/mathematica (Tutorials in Spanish)
  • Mathematica programming (some examples of the various programming paradigms that can be used in Mathematica)

FAQ

  • http://12000.org/my_notes/faq/mma_notes/MMA.htm (FAQ)
  • https://stackoverflow.com/questions/tagged/mathematica?sort=faq&pagesize=15 (FAQ on Stack Overflow)
  • https://mathematica.stackexchange.com/questions?sort=faq (FAQ on this site)
  • http://library.wolfram.com/conferences/conference98/Lichtblau/SymbolicFAQ.nb (Symbolic FAQ)

Books

  • Stephen Wolfram's The Mathematica Book (online, version 5.2), available for free
  • Mathematica programming: an advanced introduction (online) by Leonid Shifrin, available for free
  • Tutorial Collection by WRI (lots of extra documentation and examples, available as free pdfs, also available and up-to-date in Help > Virtual Book in Mathematica).
  • Mathematica Cookbook by Sal Mangano (O'Reilly, 2010)
  • Mathematica in Action by Stan Wagon (Springer, 2010)
  • Mathematica: A Problem-Centered Approach by Roozbeh Hazrat (Springer, 2010)
  • Mathematica Navigator by Heikki Ruskeepaa (Academic Press, 2009)
  • The Mathematica GuideBooks (for Programming, Numerics, Graphics, Symbolics) by Michael Trott (Springer, 2004-2005)
  • An introduction to programming with Mathematica by Paul R. Wellin, Richard J. Gaylord and Samuel N. Kamin (Cambridge University Press, 2005); contains an example of Domain Specific Language (DSL) creation.
  • Mastering Mathematica by John W. Gray (Academic Press, 1997)
  • Programming in Mathematica by Roman Maeder (Addison-Wesley Professional, 1997)
  • Programming with Mathematica®: An Introduction by Paul Wellin (Cambridge University Press, 2013)
  • Power Programming With Mathematica: The Kernel, by David B. Wagner (Mcgraw-Hill, 1997), out of print but scanned copy available here.
  • http://blog.wolfram.com/2014/01/10/read-up-on-mathematica-in-many-subjects

Wolfram Websites

Learn

  • http://www.wolfram.com/broadcast/
  • http://www.wolfram.com/training/courses (Online video courses, most are free)
    http://www.wolfram.com/training/special-event/ (Links to videos of past conferences)
  • Slides of seminars
  • http://www.youtube.com/user/WolframResearch
  • An elementary introduction to the Wolfram language
    Fast introduction for programmers
    Data drop quick reference

Examples

  • http://demonstrations.wolfram.com
  • How To-s
  • http://www.wolfram.com/mathematica/new-in-8
    http://www.wolfram.com/mathematica/new-in-9
    http://www.wolfram.com/mathematica/new-in-10/
    http://www.wolfram.com/mathematica/new-in-11/
    http://www.wolfram.com/training/special-event/new-in-mathematica-10/
  • A plot gallery for Mathematica 9
  • http://www.wolfram.com/language/

Resources

  • http://www.wolfram.com/mathematica/resources
  • http://library.wolfram.com/ (Great amount of resources here)
  • http://support.wolfram.com/kb/topic/mathematica (Knowledge base)
  • http://www.mathematica-journal.com
  • Help
    Help > Virtual Book
  • http://www.wolfram.com/support/learn/
  • http://www.wolfram.com/books/
  • http://reference.wolfram.com

Blogs

  • http://community.wolfram.com
  • http://blog.wolfram.com
  • http://blog.wolframalpha.com
  • http://blog.stephenwolfram.com
  • http://twitter.com/#!/mathematicatip

Other related sites

  • http://www.mathematica25.com
    SMP
    http://blog.stephenwolfram.com/2013/06/there-was-a-time-before-mathematica http://blog.stephenwolfram.com/data/uploads/2013/06/SMPHandbook.pdf
  • http://www.wolframalpha.com
  • Wolfram Science: the official site of Stephen Wolfram's New Kind of Science
    NKS forum
    Lecture notes from NKS summer schools
    Programs from the notes
    Demonstrations
  • http://computerbasedmath.org/
  • http://education.wolfram.com (Some interactive basic math courses, useful for curious young people)
  • http://www.wolfram.com/webresources.html (other Mathematica related sites)

Virtual conferences

  • http://www.wolfram.com/events/virtual-conference/spring-2013
  • http://www.wolfram.com/events/virtual-conference/2012
  • http://www.wolfram.com/events/virtual-conference/2011

Mathematica one-liner competition

  • http://www.wolfram.com/events/techconf2010/competition.html
  • http://www.wolfram.com/events/technology-conference/2011/one-liners.html
  • http://www.wolfram.com/training/special-event/mathematica-experts-live-one-liner-competition-2012

Wolfram technology conferences

  • http://www.wolfram.com/events/technology-conference/2016
  • 2015, http://www.wolfram.com/events/technology-conference/2015
  • 2014, http://www.wolfram.com/events/technology-conference/2014
  • 2013, http://www.wolfram.com/events/technology-conference/2013
  • 2012, http://www.wolfram.com/events/technology-conference/2012
  • 2011, http://www.wolfram.com/events/technology-conference/2011
  • 2010, http://www.wolfram.com/events/techconf2010

  • 2009, 2007, 2006, 2005, 2004, 2003, 2001, 1999, 1998, 1997, 1994, 1992

  • http://library.wolfram.com/infocenter/Conferences/


My original post above became too big so I'm splitting it into two posts. The tips and tricks section that I used to keep track of interesting new posts mostly on this site was big so here it is.

Note the tags at the end of each question on this site in order to read related questions.

I'm further dividing this post. See my third answer on Advanced evaluation, patterns, and neat algorithms below.

Tips and tricks

Top posts

  • What are the most common pitfalls awaiting new users?
    Programming paradigm change
  • Can one identify the design patterns of Mathematica?
    Mathematica style guide?
  • General strategies to write big code in Mathematica?
    Granular versus terse coding
  • What is in your Mathematica Toolbag

General tricks

  • A New Mathematica Programming Style: Functional-Procedural Fusion
  • http://www.verbeia.com/mathematica/tips/Tricks.html (Ted Ersek's tricks)
    http://library.wolfram.com/infocenter/MathSource/4557/ (Ted Ersek's tricks as notebooks)
  • http://szhorvat.net/pelican/pages/mathematica.html (Szabolcs' tricks)
  • http://www.verbeia.com/mathematica/tips/tips.html (Verbeia's tricks)
  • http://www.nhn.ou.edu/~morrison/Mathematica/ (Tip sheets)
  • As pointed out by Mr. Wizard, have a look at the available packages in your install directory for getting new ideas!
  • Tricks of the trade in the Mathematica Journal
  • How do I designate arguments in a nested map?
  • Levels: how do they work?
    Why Mathematica chooses bracket for function arguments over parenthesis?
    Why do Mathematica list indices start at 1?
  • Elegant operations on matrix rows and columns
    http://reference.wolfram.com/mathematica/tutorial/LinearAlgebraMatrixAndTensorOperations.html

Tips for writing faster code

  • Performance tuning in Mathematica?
  • http://blog.wolfram.com/2011/12/07/10-tips-for-writing-fast-mathematica-code
  • Programming for speed
  • Benchmarking expressions (timeAvg function)
    Measuring time complexity of a method
    Profiling from Mathematica
  • http://www.dbaileyconsultancy.co.uk/m_performance/m_performance.html
  • http://library.wolfram.com/infocenter/Conferences/7005/ (Principles of efficient MM programs, tips for writing fast code)
  • http://library.wolfram.com/infocenter/Conferences/320/ (Generally Efficient Numerics Programming in Mathematica)
  • Writing Efficient Mathematica Code
  • Dispatch tables
    Using Mathematica's Dispatch
    Efficiently extracting an array subset given a separate array
    How to speed up calculations with large number of replacement rules? (Experimental`OptimizeExpression)
  • Alternatives to procedural loops and iterating over lists in Mathematica
    Are there any cases when For[] loops are reasonable?
    Alternatives to While Loops?
    Is Table the only functional way to construct nested loops in Mathematica?
    How to transform Do loop to more efficient codes?
    Replace For-loop with functional code
    Equivalent Nested Loop Structure
    "do ... while" loop equivalent in Mathematica
  • Evaluate while external command is being run

  • Use Internal`Bag as a linked list (see links in Data Structures paragraph below)

Reap and Sow

  • Collecting Expressions during Evaluation
    Reap and Sow can be used with StepMonitor
  • SelectEquivalents (my personal favorite Mathematica function, basically it allows to define classes of equivalence and apply a function on the elements of each class, using Reap and Sow)
    How would you do a PivotTable in Mathematica?
  • Reap and Sow can also be used for debugging, see for example how it's used in ShowIt
  • How to collect result continuously (interruptible calculation) when running parallel calculations?

Compile

  • How to compile effectively?
    How well does Mathematica code exported to C compare to code directly written for C? (Note the function Experimental`OptimizeExpression)
    Has this implementation of FDM touched the speed limit of Mathematica?
  • Note the instruction (<< CompiledFunctionTools`; StringFreeQ[CompilePrint[cf], "MainEvaluate"]) to quickly verify that a compiled function doesn't need calls to external functions, available as from v8.
  • http://reference.wolfram.com/mathematica/Compile/tutorial/Overview.html
  • http://library.wolfram.com/infocenter/Conferences/7968/ (Effective Use of the Mathematica Compiler and Code Generation)
    What kinds of uses of SymbolicC (introduced in Mathematica 8) do you foresee? Are there any neat examples already?
  • List of compilable functions
    Why is there a huge performance gap using Map with more than 100 List entries
  • Shaving the last 50 ms off NMinimize (Advanced tricks for a very fast Nelder-Mead optimizer)
    Minimization by Nelder-Mead
    Using Apply inside Compile
    Removing calls to MainEvalute when using inlined compiled closures
  • JIT compilation
    Implementing a function which generalizes the merging step in merge sort
    A fast, robust DropWhile
  • Retaining and reusing a one-to-one mapping from a sort
  • Is it possible to use Compile on a function with optional arguments?
  • Compiling more functions
    How can I compile this function
    Compiling more functions that don't call MainEvaluate
    How to define a complicated function inside the body of Compile?
  • Does Mathematica have a "MATLAB Builder" equivalent?
    http://reference.wolfram.com/mathematica/CCodeGenerator/tutorial/CodeGeneration.html
    http://reference.wolfram.com/mathematica/CCodeGenerator/ref/LibraryGenerate.html
  • Third argument of Compile
  • CompiledFunctionCall vs. LibraryFunction
  • Compiling LinearSolve[] or creating a compilable procedural version of it

Using links to other languages

  • System interfaces and deployment
  • Faster alternatives for DayOfWeek (Java)
    Working with Java hashmaps from Mathematica
  • WSTP
    Convert C program using mathlink to WSTP
    Handling error in WSTP program C/C++
    A MathLink tutorial
    Mathlink Mode article
  • LTemplate
    How to simplify writing LibraryLink code?
    Drawing Clifford Attractors in Mathematica
  • Minimal effort method for integrating C++ functions into Mathematica
    Converting other C++ classes to MTensor in LibraryLink
    How can I use shared libraries in LibraryLink code and ensure Mathematica will find them?
    How to understand shared passing of a MTensor in LibraryLink?
    Using Boost with CreateLibrary
    Write C/C++ programs calling Mathematica functions
    Example program for calling Mathematica from C
    Returning multiple results from a LibraryLink function
    Calling Mathematica from Visual Studio - example
    Which Distributions can be Compiled using RandomVariate
    Librarylink made easier to use with C++11
    Calling Mathematica from Visual Studio - example
  • How to use C language functions in Mathematica?
  • Mathematica Functions from VBA
  • Linking FORTRAN with Librarylink
  • MATLink, link between Mathematica and Matlab
    Is it possible to export the equations from Mathematica to MATLAB?
  • Calling IronPython code from Mathematica
  • JLink
    JLink classes' reference
  • ExternalEvaluate for Python fails with Import::nopythonevals

Packed arrays

  • http://library.wolfram.com/infocenter/TechNotes/391/ (Packed Arrays)
  • What is a Mathematica packed array? (instruction for being warned when a packed array is unpacked)
    Mathematica memory management for large arrays
  • Avoid mixing integers with real numbers in a list, which would unpack the list and slow computations. For example use 1. instead of 1 if you have to enter manually an entry in a list of reals.
  • Isn't it possible to Pack a SparseArray?

Debugging

  • A debug utility to print or extract intermediate data from a program (ShowIt, a useful variant of Print)
  • The clearest way to represent Mathematica's evaluation sequence
    How do I evaluate only one step of an expression?
    Is it possible to construct a fullform trace function
  • Debugging memory leaks
    Profiling memory usage in Mathematica
    Preventing Mathematica from eating up all your memory (not a bug but the info can be useful)
    How to un-eat memory?
  • Using the Mathematica debugger, and other interesting possibilities
    Debugging Mathematica Code
  • DebugTrace
  • What are the best practices / most common / idiomatic ways to report errors in Mathematica?
    How to program a F::argx message?
    How to abort on any message generated?
    Best practices in error reporting
    How to leave function unevaluated programmatically?
    Setting up diagnostic error messages in large Mathematica projects, Debugging a $RecursionLimit error?
    Dealing with errors and resuming the code after one
    Interrupting package evaluation, handling error
    Prepend Information to Warning Messages
    Need help with exception handling
    Error checking and trapping techniques with Throw and Catch
    Exception/Error handling using returns codes
    How do I get Mathematica to return a function call unevaluated?
    Resource management in Mathematica
  • Wolfram Workbench has an integrated debugger and profiler. See here for posts about it on this site: https://mathematica.stackexchange.com/questions/tagged/workbench
    Inspecting non-variable state from a breakpoint in Workbench
    workbench: how to see values of global variables during debugging
  • Mathematica Debuggability
  • Automatically generating a dependency graph of an arbitrary Mathematica function?
    Is there an analogue of the Variables command for general expressions?
  • Using Java log4j in Mathematica

Data Structures

  • Data structures and efficient algorithms, from Daniel Lichtblau
    Kd-list, Kd-tree (applied to Gröbner bases)
  • Expr Bag
    Internal`Bag inside Compile
    Efficiently collecting results inside a compiled function
    Most efficient way of splitting a file into length-prefixed blocks
  • Linked lists
    Mathematica “linked lists” and performance
    Merge-sort
    Variant of the cutting-stock problem in Mathematica
    The answer of Leonid is a reference on the topic: Can one identify the design patterns of Mathematica?
    Searching linked lists that contain lists?
  • Efficient circular buffer?
  • Efficient priority queues?
  • BTree
    Using Memoization with a Mutable Object
    How to speed up Minimum Spanning Tree algorithm?
    How to make Huffman coding by using tree representation
    What is the fastest way to maintain a large set of expressions?
    Generating an ordered list of pairs of elements from ordered lists

Objects

  • How to create strong types?
    How to implement FittedModel like objects
    How can one define a custom data object?
  • Struct equivalent in Mathematica?
    Implementing a dictionary data structure
    Is there HashTable structure in Wolfram Mathematica?
    Is there a method to enumerate the keys/values of System`Utilities`HashTable
    A usage example in the answer of Oleksandr R. or in the answer to the post
    How can you give a Module a context and have its local variables and Modules belong to that context?
    What's fastest way of defining 10^5 down values?
    Return a Part by Reference? (For accessing hierarchical / structured data)
  • Once more on object orientation in Mathematica: does it have to be so hard?
    Builder pattern for complex objects construction
    How can I implement object oriented programming in Mathematica?
  • MTools, an open-source package for OOP from the author of this answer
  • OOP applied to manipulate
  • Question about designing a particular data structure
  • Code Readability and Object-Oriented Code
  • Polymorphism and the pattern-matcher
  • Inheritance in Mathematica using pattern matching on UpValues
  • How to implement the Observer design pattern?
  • http://www.objectica.net/Documentation6/guide/ReferenceGuide.html (non free, but still can give some inspiration)

Traversal of an expression

  • How to perform a depth-first preorder traversal of an expression?
  • How to perform a breadth-first traversal of an expression?
  • Determining all possible traversals of a tree
  • Uses for MapAll (//@)
    myHold examples with MapAll (1,2)
  • Can a Trie be implemented efficiently?
  • Concatenate strings from different levels of list

Graphs

  • GraphUtilities
  • How to play with Facebook data inside Mathematica?

Optional Values

  • Functions with Options
  • What are the benefits of switching from Rule and /. to OptionsPattern[] and OptionValue in a large application?
  • How to avoid collision between optional arguments and options
  • Can a function be made to accept a variable amount of inputs?
  • Dealing with nested options
  • Evaluation of OptionValue[]
  • Custom functions by delegating options in a specific way and using core functions (withOptionConfiguration)
  • Specifying optional arguments with variables
  • Is there any way to define pure functions with optional arguments?
    Is it possible to separately call the default value of a variable which isn't at the end of the sequence?
  • Why doesn't Mathematica use uniform criteria for validating Options?
    How to check the validity of an option value
  • How to inherit options by scoping?
  • How to make a function with its own options as well as passing options to other functions
  • Using a held option
  • Writing functions with "Method" options (Interesting answer on parsing a grammar of options)

Accessing data in different ways

  • Cases[data,Colon[key,_]] vs. Cases[data,key:_] toward XPath, XQuery
  • Emulating R data frame getters with UpValues
    Creating a R DataFrame like construct in Mathematica
    How to achieve Set+Part like behaviour in custom Set function?
  • The gap between MapAt and ReplacePart for 2D data tables with headers
  • Data Table Manipulation in Mathematica
    Data Table Manipulation in Mathematica: Step 2
    How can a big table be treated as a database?
  • Path queries for tree-structured data
  • Splitting up delimited data in lists
    Partitioning with varying partition size (dynamicPartition)
    Splitting a list by specifying section headers
  • Accessing list elements by name

Association/Dataset

  • How to make use of Associations?
    Normal // Association // Dataset workaround for some user query operators
    Can't get Append to append a record (association) to a dataset
    How can I save a Manipulate's control settings to a dataset?
    V10's Operator Forms - what are they good for?
    What is the natural way of using Dataset to get a FittedModel?
    Dataset: get number of levels and elements
    Is there a comprehensive list of functions with operator forms?
    Converting structured data to a dataset
    How can I add a column into a existing Dataset?
    GroupBy several keys while keeping the Dataset as a Table (List of Associations)
    Learning Resources for Data Science in the Wolfram Language (many usage examples of List/Association/Dataset)
    SQL-like computation on Dataset: Aggregate function
    Is there a faster way to Map an Association?
    Difference between Association and Dispatch
    Changing Values in an Association using Map (Answer on Immutability)
    Filtering Query Nested Data (Dataset and Association)
    Is there an idiomatic way to determine whether an `Association` is a subset of another?
    Visualizing Type System Operations
    Struggling to understand Query
    Reshaping associations, generalization of GroupBy

Rules and replacement (the backbone of Mathematica, more advanced)

  • Where definitions are stored
  • What is the distinction between DownValues, UpValues, SubValues, and OwnValues?

Getting ideas from Lisp

  • Structure and interpretation of computer programs, Common Lisp The Language, On Lisp, Practical Common Lisp ...
  • repl.it has an online interpreter for Scheme with some examples.
  • Is there a Mathematica/Lisp link?
  • Other functional languages : Scala, Haskell, Erlang, Clojure, Caml, F#, Maude ...
  • FAQ on functional programming

Memoization/Caching

  • The answer of Leonid is a reference on the topic: Can one identify the design patterns of Mathematica?
  • What does the construct f[x_] := f[x] = ... mean?
  • Avoid repeated calls to Interpolation (Interesting answers and links about caching values and expressions)
  • Function that caches when it returns unevaluated
  • Why is there no PositionFunction in Mathematica?
  • Built-in Mathematica data: are they cached? how to speed up the loading?
  • Memoization with pure functions?

Undocumented features

  • What are some useful, undocumented Mathematica functions?
    Documentation of the frequently-used functions in the context of Internal` and Compile`
    Items known by CurrentValue
  • How can one find undocumented options or option values in Mathematica?
  • What is the complete list of valid Front End Tokens?
  • How do you convert a string containing a number in C scientific notation to a Mathematica number?
  • What is the complete list of valid FrontEnd Packet types?
  • How can I get the unchanged Box form of an arbitrary expression?
  • How can I manipulate TemporalData?
  • How to work with Experimental`NumericalFunction?
  • Full documentation for AppearanceElements
  • How to create internally optimized expression for computing with high WorkingPrecision?
  • What are all the named Mathematica styles?
  • How to create expanding summary boxes like the ones displayed for InterpolatingFunction, NearestFunction, etc.?
  • Incompatible Changes since Mathematica Version 7?
  • Monte Carlo integration with random numbers generated from a Gaussian distribution

IDEs and packages

  • How to install the Wolfram Workbench plugin into Eclipse Kepler or Neon?
    Wolfram Workbench
    Managing formatted usage messages in Wolfram Workbench
    Creating cross-version compatible documentation with Workbench
    Renaming projects in Wolfram Workbench
    Recommended settings for git when using with Mathematica projects?
    How to setup Team Development for a Mathematica project?
  • Open-source IntelliJ IDEA plugin to support Mathematica development
  • How to distribute Mathematica packages as paclets?
    How to properly handle mutual imports of multiple packages? What's the difference between these two ways of specifying dependent packages?
    Second argument of BeginPackage with nested package loading
    Creating Mathematica packages
    What are recommended guidelines for developing packages?
    How to unload automatically loaded packages?
    How can I dynamically get a file whenever it is saved in Workbench?
    How to work with Application Project files in Wolfram Workbench?
    What is a "Paclet"?

Mathematica functions and environment

  • How to specify Mathematica as a programming language?
    http://www.wolfram.com/mathematica/quick-revision-history.html
    Where can I access documentation for old versions of Mathematica?
    http://12000.org/my_notes/compare_mathematica/index.htm
    How to get complete Documentation Center graph of guide pages?
    Automated testing for compatibility with older Mathematica versions
    Integrating notebooks to Mathematica's documentation center (Using Wolfram Workbench is easier for building a help but the answer is still interesting)
    List of dangerous functions
    What are all the named Mathematica styles?
  • How does Plot work?
    How to implement the sample-point process like the built-ins of Mathematica?
    How to examine the structure of Graphics objects
    Symbolic derivatives are being calculated numerically
    Embed Code Into Image
  • How does Mathematica calculate integrals?
  • Is it possible to define a new PlotTheme?
    How to get grid lines at all (log) tick marks in Version 10?
  • How can I set up a versioning system within a notebook?
    Is there a notebook autosave frequency configuration?
    Are there suitable versioning systems for Mathematica notebooks?
  • How to find a specific error message?
  • The Joy of Tagging: Manipulating and Mining Notebooks in Mathematica
    How to programmatically mark and then select a subset of input cells?
  • Programmatic formatting for Mathematica code - possible?
    Syntax highlighting for your own functions
    Customizing syntax highlighting for private cell styles
    Special Mathematica Cell to work with R code
  • Is it possible to improve Mathematica's auto-completion feature?
  • Creating a notebook that has cells of a certain style password protected
    Advanced typesetting
    Resources for beautiful Mathematica Stylesheets
    Some tutorials on formatting notebook for exporting to pdf
    What options and settings can be used to create print quality typeset documents with Mathematica?
  • Adding a label to an expression result
    How to fold output cells by default?
  • Automatic e-mail/text message when a calculation has finished?
    Keep MathKernel Running In Background - Speed Up Execution Time
  • Simpler input for the new unit support
  • Quality of random numbers
  • Will Mathematica install and run on Raspberry Pi computer?
  • Scheduling Mathematica scripts to run from a command line
  • How to keep $FrontEnd options from resetting?
  • How do I add new menuitems to menus?
  • Mathematica color schemes for the colorblind
    Is there an easy way to use Matteo Niccoli's perceptual color maps for 2D plots in Mathematica?
  • What's inside InterpolatingFunction[{{1., 4.}}, <>]?
  • Are there any tutorials on version 10 notebook templating and report generation? Are there missing docs?
  • Handling Kernel start. What is a full initialization stack and what place the Autoload has there?

Third part of my collection of links, dedicated to advanced evaluation, pattern matching and neat algorithms (which is quite arbitrary but contains a lot of interesting Q&As in my opinion).

Advanced evaluation of expressions

  • Non standard evaluation allows to work on the symbols of an expression before they get evaluated.

    Here's how I represent myself Mathematica's evaluation: consider a function/tree f[a,b]. Without any particular attribute for f the leafs/arguments will be evaluated before the parent f. With f having a HoldAll attribute you don't evaluate the leafs but go directly in the evaluation of f. a and b will be evaluated as soon as they are used in a function that doesn't hold again their evaluation. For example

    f[a_,b_]:= a Hold[b]; 
    f[2^2,2^2] (* returns 4 Hold[4] *)
    
    SetAttributes[f, HoldAll];  
    f[2^2,2^2] (* returns 4 Hold[2^2] *)
    

    You can also see with the above example that SetDelayed (:=) acts like a With, ie it can insert a value inside a held expression.

    http://reference.wolfram.com/mathematica/tutorial/NonStandardEvaluation.html
    Preventing evaluation of Mathematica expressions
    Hold any argument
    How can I hold UpValues but evaluate other expressions?
    Symbolic computations with already assigned variables
    How to pass a symbol name to a function with any of the Hold attributes?
    Pure function with attributes of arbitrary number of arguments: Is it possible?

  • Mathematica Language Structure
  • Mathematica Internals: A Tutorial
    The evaluation process
    http://reference.wolfram.com/mathematica/tutorial/Evaluation.html
    http://reference.wolfram.com/mathematica/tutorial/TheStandardEvaluationProcedure.html
    List manipulation to build a functional expression
    How does Mathematica determine that an evaluation should be terminated?
    What are all the "magic" Symbols in the Mathematica language?
  • Some notes on internal implementation
    The Internals of the Wolfram System
    Algorithm used by IsomorphicGraphQ
  • What is the complete sequence of evaluations/transformations from submitting a cell to actual evaluation?
    \$PreRead, \$Pre, \$Post, \$PrePrint
  • Robby Villegas, Working with Unevaluated Expressions (Advanced resource about the evaluation process)
    Update a function avoiding infinite recursion (Villegas-Gayley technique)
    Understanding Villegas-Gayley
  • What are the use cases for different scoping constructs?
    A speed comparison between Module, Block and With (Still, Module is what users use most of the time)
    Enforcing correct variable bindings and avoiding renamings for conflicting variables in nested scoping constructs
    What does Internal`InheritedBlock do?
    How safe is the use of Block and Internal`InheritedBlock
    What is the purpose of Internal`LocalizedBlock?
    What are the scoping rules for function parameters shadowing System` symbols?
    Constructing symbol definitions for With
    Using With to scope over pure functions
    Module variable scoping in Scheduled Tasks?

    Enforcing correct variable bindings and avoiding renamings for conflicting variables in nested scoping constructs
    StringReplace, ReplaceAll and Rule interact in a bizarre way

  • What are some advanced uses for Block?
    How to set Block local variables by code?
    Safely nesting RuleDelayed
  • Metaprogramming in Mathematica
  • Can one identify the design patterns of Mathematica?
  • Replacement inside held expression
    Injecting a sequence of expressions into a held expression
    Is it possible to replicate the "magic" of OptionPatterns[] with DynamicModule's local variables?
    How does MakeBoxes handle an n-ary operator?
    How to inject an evaluated expression into a held expression? (Trott-Strzebonski in-place evaluation trick, RuleCondition)
    How to pass a list of arguments into HoldAll
    Returning an unevaluated expression with values substituted in
    Passing a joined list of symbols for Module or Block to treat as its own local symbols
    Replacing parts of a held expression with held parts of another expression
  • Expression parsing examples
    Converting StringJoin to StringForm inside Hold
    How to write a function to remove comments from a .m source file preserving formatting such as line wrapping reasonably?
    How to write a function-defining function which stores the function arguments in a stack?
    Is there a Mathematica API for the functions.wolfram site?
    Function that counts the number of arguments of other functions
    Using MatchQ (or other means) to parse an expression using sums of COS or SIN correctly Automatically generating a dependency graph of an arbitrary Mathematica function?

    Programmatic formatting for Mathematica code - possible?

  • Functional style using lazy lists?
    Tally repeated evaluation of function
    File-backed lists/variables for handling large data (Lazy evaluation of streams)
    Lazy lists of Tuples and Subsets
  • How to use pattern matching to assign values to Subscript[f,x_]?
  • Implementing a safe ValueQ that does not evaluate its argument
  • Convert an expression to a Function
    Nested definition: How can I define a function with a passed-in expression?
  • How to avoid nested With[]?
    Comparing LetL and Module efficiency
  • Do people actually use UpValues?
    Upvalues, TagSet and UpSet, what's the difference, when should a use each?
    How to implement dual numbers in Mathematica?
    Make mathematica treat $e_i^2$ as numeric
    How to differentiate formally?
  • Currying with Mathematica
    Going full functional (Haskell style)
  • How to avoid returning a Null if there is no "else" condition in an If construct (Vanishing function)
  • SetAttributes[f,Flat]: Why the order dependence?
    The Flat Attribute, Unevaluated and the Evaluation Process
    Orderless pattern matching
  • Spelunking
    What is the most convenient way to read definitions of in-memory symbols when we don't have the source files? (Spelunking tools)
    How to see which arguments are passed into a function
  • How does Return work?
    What can I use as the second argument to Return in my own functions?
    FoldWhile and FoldWhileList
  • Tail call optimization in Mathematica?
    What tools can help in realizing tail recursion?
  • Elegant manipulation of the variables list
    How to name a variable with the value of another variable?
  • Block attributes of Equal
  • How to find the name of the current function?
  • Does pass-by-value affect the performance of function calls?
  • Comparing Mathematica expressions like diff
  • What is the fastest way to get a list of subexpressions and their positions?
  • Resource management in Mathematica
  • How to pass a list of arguments into HoldAll
    Dynamic Programming with delayed evaluation
  • Multiplying expressions within a list of pure functions
    Using Through to evaluate complex expressions
  • How to make a function like Set, but with a Block construct for the pattern names
    Scoping in assigning a derivative
  • Getting a usable expression tree
  • Call Functions From File Without Modifying Context (Sandbox)
  • Functions vs. patterns
    when is f@g not the same as f[g]?
    When should I use Apply (or Function) and when @@ (or &)?
  • Constructing functions with variable number of output arguments
    Alternative to overloading Set
  • What are the cool kids talking about when they use ##&[]?
    How to avoid returning a Null if there is no "else" condition in an If construct
  • How to create symbols from strings and set values for them?
  • What's the difference between Inactive and HoldForm?
  • Is it possible to reference "self" from a pure function?
  • How can one manually change the rule ordering
  • Permanently extending the behaviour of functions (like decorators)

Pattern matching

A pattern is a way to quickly describe the structure of expressions and do computations on them, using /. or Cases for example.

  • Some examples
    Much more elaborate: a Texas Hold'em package by Sal Mangano
  • Rules, pattern and functions, one of the chapter of Leonid Shifrin's book.
    Demistifying rules
  • Where in the documentation can I find a list of function argument types?
    How can I type-check the arguments of a Mathematica function?
  • Advice for Mathematica as Mathematician's Aid
  • Using ?NumericQ to Affect Order of Evaluation
    Is it possible to use the LevenbergMarquardt algorithm for fitting a black-box residual function?
  • Convert boolean test function to pattern?
  • http://library.wolfram.com/infocenter/Conferences/6999/ (Inside the Mathematica pattern matcher)
  • Is there an open source implementation of Mathematica-the-language? (Some interesting links to papers about pattern matching, 1,2,3,4)
    MockMMA
    Mathador
    Mathematica as a normal programming language
    Mathematica for Computer Scientists
  • Mathematica Destructuring (Mr. Wizard also gives many links to interesting answers illustrating the same point)
  • How to match a pattern with a pattern?
    How to generally match, unify and merge patterns?
    Pattern matching a pattern with patterns
  • Semantica, a package for using semantic patterns (f[(2 n_)] for example)
  • How do I perform string matching and replacements?
    Working with string patterns
    Counting the number of instances of one sub-string within a given string within a lower- and upper-bound gap of a second sub-string
  • Placement of Condition /; expressions
    Using a PatternTest versus a Condition for pattern matching
    Use of StringExpression as argument
    Quick way to use conditioned patterns when defining multi-argument function?
  • Assessing argument type in set delayed function definitions
    How to Combine Pattern Constraints and Default Values for Function Arguments
  • ForEach in Mathematica
  • Why does the name of a pattern affect the result of a transformation rule?
  • Conflict with variables in pure functions
  • How to match expressions with a repeating pattern
  • Is it possible to specify a context-sensitive, "depth-agnostic" rewrite rule?
  • Change variables in differential expressions
  • Transforming XML
    Extract information from HTML using Mathematica
    How to manipulate web pages on Mathematica?
    Extract information from HTML using CSS selectors?
  • Replacements/Substitutions in Mathematica (\$Assumptions)
  • Position function not always returning an answer even with no apparent problems
  • How can I ensure that I am constructing patterns in the most efficient way possible?
  • How can I find these patterns' signatures?
  • replacement rules from a pattern and a matching expression
  • Position of a pattern-matched part of an expression
    Select cases from a list (ReplaceList)
  • Patternmatching sets
  • Using patterns in pure functions
  • The difference between 0. and 0
  • “Strange” behavior of Rule
  • f[arg1, arg2,...,argN] vs. f[{arg1, arg2,...,argN}]
    A function that accepts a pair or a list of pairs
    Alternatives pattern in a function definition
    How to distinguish between lists and values?
    Vanishing patterns
  • Get a "step-by-step" evaluation in Mathematica
    Interactively inspecting parts of an object
  • How to do Cases with multiple related patterns?
  • How to match a cyclically repeating sequence?
  • How to define a recursive pattern?
    Convert recursive RegularExpression to StringExpression?
    Pattern match for nested Association
  • How to enlarge Mathematica's knowledge about certain functions?
  • Calling Table with custom iterator

Neat algorithms

  • Mathematica Minecraft
    How to create word clouds?
    How can I use Mathematica's graph functions to cheat at Boggle?
    Performance tuning for game solving (peg solitaire | senku)
    Factorisation diagrams
    Happy 2K prime question
    Simulating Theatre puzzle
    How can I generate this "domain coloring" plot?
    Tiling a square
    Generating visually pleasing circle packs
    Circuit drawing in Mathematica
    Detecting patterns of black and white stones on a 2D board
    How to build a bvh (a motion capture file format) player in Mathematica?
  • How to draw Fractal images of iteration functions on the Riemann sphere?
    Generating a Sierpinski carpet
    Speeding up this fractal-generating code
  • Explain a Mathematica winning one-liner
  • How can this confetti code be improved to include shadows and gravity?
    How to create animated snowfall?
  • xkcd-style graphs
    xkcdConvert routines perform slower in Mathematica 9
    http://blog.wolfram.com/2012/10/05/automating-xkcd-diagrams-transforming-serious-to-funny/
    Sketch-type graphics with transparency and dashed hidden lines?
  • How to improve the performance of solutions to Project Euler (#39)?
    How to find palindromic numbers (Project Euler #4)?
  • Image processing
    Help find a bright object on Mars!
    How can I find Waldo?
    QR Code in shopping cart handle
    Playing with Matrix falling code in Mathematica
    Image segmentation and object separation in 3D using Mathematica
    Artistic image vectorization
    DumpsterDoofus's captivating generative art
  • How to express trigonometric equation in terms of of given trigonometric function?
    How do I introduce a new variable in a trigonometric equation?
    Checking if two trigonometric expressions are equal
  • Efficiently generating n-D Gaussian random fields
    Distribution of random points in 3D space to simulate the Crab Nebula
  • How to improve this code for solving the "Mr.S and Mr.P" puzzle?
    Efficient code for the Ten True Sentences puzzle
    Mathematica Implementations of the Random Forest algorithm
    Soft-Match String Comparison
    Gram-Schmidt Process for Polynomials
    How to determine the center and radius of a circle given some points in 3D?
    Counting multiplications (complexity function)
    Insert $+$, $-$, $\times$, $/$, $($, $)$ into $123456789$ to make it equal to $100$
    Higher order SVD
    How to check if a 2D point is in a polygon?
    Find eigen energies of time-independent Schrödinger equation
    Solving a time-dependent Schrödinger equation
    Efficient backtracking with Mathematica
    Determine frequency of oscillations
    Alternative ways to implement a triangular recursion
    Implementation of smoothing splines function
  • Position
    Efficiently finding the positions of a large list of targets in another, even larger list
    Looking for a way to insert multiple elements into multiple positions simultaneously in a list
    Efficient way of identifying the indices of first occurrences
  • Duplicates
    Deleting quasi-duplicates from large list efficiently
    Delete duplicate elements from a list
    Ordering function with recognition of duplicates
    How to get list of duplicates when using DeleteDuplicates?
    How to efficiently find positions of duplicates?
    Removing elements from a list which appear in another list
  • Map/Thread
    Scan vs. Map vs. Apply
    Map a function across a list conditionally
    Map-Thread-Through-Apply a list of functions onto a list of (lists of) values
    Thread over list in different levels Thread over a nested list top to bottom until non-list elements are found How can I make threading more flexible?
    How to make MapAt work with Span?
  • Flatten
    "Unflattening" a list
    Transpose uneven lists
  • List manipulations
    Finding a subsequence in a list
    How to select minimal subsets?
    Optimising 2D binning code
    Efficiently extracting an array subset given a separate array
    Quick multiple selections from a list
    How to Derive Tuples Without Replacement
    Need help coding/creating a recursive list (FoldList)
    Find subsequences of consecutive integers inside a list
    Instruct a Table to only evaluate until a condition is fulfilled
    How do I replace a missing value in a column with the value immediately above throughout a table?
    Sort+Union on a list
    How to generalize and speed up this program?
    How do I obtain an intersection of two or more list of lists conditioned on the first element of each sub-list?
    Vlookup function as Excel in Mathematica
    Partitioning a list of numbers the Mathematica way
    How to pick increasing numbers from the list
    How to replace an element in a list based on the value of the next element?
    How to partition a list in a specific way
    Nest , Fold ... is there an extension for more than 2 arguments?
    List comprehension in Mathematica (similar to Python)
    Select a repeated element in a list
    Fast method for combining two lists
    Discrete Convolution
    Shuffle product of two lists
    Is there a function which instantly tells you whether an element is part of a list?
    Sort data after specific ordering (ascending/descending) in multiple columns
    Total by a criteria