What's the meaning of #0? [closed]

Here's a line of code from a handbook written by Stephen Wolfram, which turns out to be very complicated for me.

If[#1 > 2, 2 #0[#1 - #0[#1 - 2]], 1] & /@ Range[50]

The output is:

{1, 1, 2, 4, 4, 2, 4, 4, 8, 4, 4, 8, 16, 4, 2, 16, 4, 2, 4, 4, 8, 4, 4, 8, 16, 4, 8, 16, 16, 8, 4, 16, 32, 4, 4, 32, 64, 4, 2, 64, 4, 2, 4, 4, 8, 4, 4, 8, 16, 4}


I am confused about the Slot 0(#0) here, or how could I break down the code and understand it?

• Here is a dedicated Q/A on this topic. Feb 22, 2019 at 16:52
• This is discussed in the documentation for [Slot](https://reference.wolfram.com/language/ref/Slot.html). Feb 23, 2019 at 3:10

#0 refers to the function itself. This is consistent with the "0th" argument being the head of an expression.

Example:

Print[#0] &[]

(* prints Print[#0]& *)


In practice, this is useful for writing recursive functions. This is what it is used for in your example. The example could be rephrased as

f[x_] := If[x > 2, 2 f[x - f[x - 2]], 1]

• I'm going to have to remember this trick the next time I'm over on PCG Stackexchange. Feb 22, 2019 at 13:34
• @MichaelSeifert The problem is that we still need to add a stopping condition for the recursion, and with this method it needs to be done with an If[...]. That will sometimes make the code longer than the pattern matching version. Feb 22, 2019 at 13:38
• @MichaelSeifert Well, actually a Fibonacci is shorter with the pure function method: i.sstatic.net/Fg38W.png Feb 22, 2019 at 13:40