Question: [This question](https://mathematica.stackexchange.com/questions/2440/effective-matrix-power-like-algorithm) mentions "`x := x =` trickery".

What does defining a function as `f[x_] := f[x] = ...` do and what is it good for?

Answer: 
It is a simple way to implement [Memoization](https://en.wikipedia.org/wiki/Memoization). The trick is that if you define a function as

```
f[x_]:=f[x]=ExpensiveFunctionOf[x]

```

then when you for the first time call e.g. `f[3]`, it will evaluate as

```
f[3]=ExpensiveFunctionOf[3]

```

which will evalulate the expensive function, and *assign the result to `f[3]`* (in addition to giving it back, of course). So if e.g. `ExpensiveFunctionOf[3]` evaluates to 7, it will assign `f[3]=7`.

The next time you call `f[3]`, it will find that newly created definition of `f[3]` and use that in preference to the general rule. This means it will return the value 7 *without* calculating `ExpensiveFunctionOf[3]` a second time.
[Memoization](http://reference.wolfram.com/mathematica/tutorial/FunctionsThatRememberValuesTheyHaveFound.html) is perhaps the most common application, but it is not the *meaning* of that construct.

More generally it is a construct for a function that redefines itself. This has many uses beyond memoization. Consider this function:

```
f[y_] := (f[y] = Sequence[]; y)

```

It is used to remove duplicates in a list. When the function is first called with a particular argument (expression) it redefines itself, only for that argument, to `Sequence[]`. The next time it is applied to that expression it therefore resolves to `Sequence[]` (because the more specific definition has priority) and the expression is effectively removed. (Of course it must be [reinitialized](https://mathematica.stackexchange.com/a/2423/121) between lists unless you want to remove duplicates globally.)

```
f /@ {3, 5, 2, 3, 2, 4, 3}

```

> 
> 
> ```
> {3, 5, 2, 4}
> 
> ```
> 
> 

You can also write a function that changes its behavior after the first use.  

Note the different pattern name to avoid conflict.

```
g[x_] := (g[y_] := y - 1; x + 1)

g /@ {1, 2, 3}

```

> 
> 
> ```
> {2, 1, 2}
> 
> ```
> 
> 

This can even be nested deeper:

```
g[x_] := (g[y_] := (g[z_] := z + 4; y - 1); x + 1)

g /@ {1, 2, 3}

```

> 
> 
> ```
> {2, 1, 7}
> 
> ```
> 
> 

---

Syntax for memoization
----------------------

When using this construct for memoization it is often nice to use a named pattern for the entire left-hand-side, e.g.:

```
mem : f[x_] := mem = . . .

```

This is equivalent to `f[x_] := f[x] = . . .`. In this simple case it is not an improvement, but when the definition becomes longer it has the advantages of being:

* more concise
* less prone to error as there is less code to retype
* a convenient label for purpose of the construct

If one adopts this convention seeing `mem :` will immediately let one know this is a memoized function.

As a contrived example:

```
mem : combinations[set_Integer?Positive, choose_Integer?Positive] := 
  mem = set!/(choose! (set - choose)!)

```

Memoization is not the only application of a pattern that matches the entire left-hand-side and by using consistent pattern names for each application one can impart valuable metadata about the nature of the code to follow.

Reference:

* [How does a construct like `i : func[arg\_] := i = an expression using arg` work?](https://mathematica.stackexchange.com/q/119035/121)