What's the meaning of #0? [on hold]
$begingroup$
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?
pure-function
$endgroup$
put on hold as off-topic by Michael E2, m_goldberg, MarcoB, J. M. is computer-less♦ Feb 26 at 8:29
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Michael E2, m_goldberg, MarcoB, J. M. is computer-less
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
$begingroup$
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?
pure-function
$endgroup$
put on hold as off-topic by Michael E2, m_goldberg, MarcoB, J. M. is computer-less♦ Feb 26 at 8:29
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Michael E2, m_goldberg, MarcoB, J. M. is computer-less
If this question can be reworded to fit the rules in the help center, please edit the question.
2
$begingroup$
Here is a dedicated Q/A on this topic.
$endgroup$
– Leonid Shifrin
Feb 22 at 16:52
2
$begingroup$
This is discussed in the documentation for[
Slot](https://reference.wolfram.com/language/ref/Slot.html)
.
$endgroup$
– Michael E2
Feb 23 at 3:10
add a comment |
$begingroup$
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?
pure-function
$endgroup$
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?
pure-function
pure-function
asked Feb 22 at 8:47
ShawnShawn
584
584
put on hold as off-topic by Michael E2, m_goldberg, MarcoB, J. M. is computer-less♦ Feb 26 at 8:29
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Michael E2, m_goldberg, MarcoB, J. M. is computer-less
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by Michael E2, m_goldberg, MarcoB, J. M. is computer-less♦ Feb 26 at 8:29
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Michael E2, m_goldberg, MarcoB, J. M. is computer-less
If this question can be reworded to fit the rules in the help center, please edit the question.
2
$begingroup$
Here is a dedicated Q/A on this topic.
$endgroup$
– Leonid Shifrin
Feb 22 at 16:52
2
$begingroup$
This is discussed in the documentation for[
Slot](https://reference.wolfram.com/language/ref/Slot.html)
.
$endgroup$
– Michael E2
Feb 23 at 3:10
add a comment |
2
$begingroup$
Here is a dedicated Q/A on this topic.
$endgroup$
– Leonid Shifrin
Feb 22 at 16:52
2
$begingroup$
This is discussed in the documentation for[
Slot](https://reference.wolfram.com/language/ref/Slot.html)
.
$endgroup$
– Michael E2
Feb 23 at 3:10
2
2
$begingroup$
Here is a dedicated Q/A on this topic.
$endgroup$
– Leonid Shifrin
Feb 22 at 16:52
$begingroup$
Here is a dedicated Q/A on this topic.
$endgroup$
– Leonid Shifrin
Feb 22 at 16:52
2
2
$begingroup$
This is discussed in the documentation for
[
Slot](https://reference.wolfram.com/language/ref/Slot.html)
.$endgroup$
– Michael E2
Feb 23 at 3:10
$begingroup$
This is discussed in the documentation for
[
Slot](https://reference.wolfram.com/language/ref/Slot.html)
.$endgroup$
– Michael E2
Feb 23 at 3:10
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
#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]
$endgroup$
3
$begingroup$
I'm going to have to remember this trick the next time I'm over on PCG Stackexchange.
$endgroup$
– Michael Seifert
Feb 22 at 13:34
$begingroup$
@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 anIf[...]
. That will sometimes make the code longer than the pattern matching version.
$endgroup$
– Szabolcs
Feb 22 at 13:38
1
$begingroup$
@MichaelSeifert Well, actually a Fibonacci is shorter with the pure function method: i.stack.imgur.com/Fg38W.png
$endgroup$
– Szabolcs
Feb 22 at 13:40
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
#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]
$endgroup$
3
$begingroup$
I'm going to have to remember this trick the next time I'm over on PCG Stackexchange.
$endgroup$
– Michael Seifert
Feb 22 at 13:34
$begingroup$
@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 anIf[...]
. That will sometimes make the code longer than the pattern matching version.
$endgroup$
– Szabolcs
Feb 22 at 13:38
1
$begingroup$
@MichaelSeifert Well, actually a Fibonacci is shorter with the pure function method: i.stack.imgur.com/Fg38W.png
$endgroup$
– Szabolcs
Feb 22 at 13:40
add a comment |
$begingroup$
#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]
$endgroup$
3
$begingroup$
I'm going to have to remember this trick the next time I'm over on PCG Stackexchange.
$endgroup$
– Michael Seifert
Feb 22 at 13:34
$begingroup$
@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 anIf[...]
. That will sometimes make the code longer than the pattern matching version.
$endgroup$
– Szabolcs
Feb 22 at 13:38
1
$begingroup$
@MichaelSeifert Well, actually a Fibonacci is shorter with the pure function method: i.stack.imgur.com/Fg38W.png
$endgroup$
– Szabolcs
Feb 22 at 13:40
add a comment |
$begingroup$
#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]
$endgroup$
#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]
answered Feb 22 at 8:52
SzabolcsSzabolcs
161k14438936
161k14438936
3
$begingroup$
I'm going to have to remember this trick the next time I'm over on PCG Stackexchange.
$endgroup$
– Michael Seifert
Feb 22 at 13:34
$begingroup$
@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 anIf[...]
. That will sometimes make the code longer than the pattern matching version.
$endgroup$
– Szabolcs
Feb 22 at 13:38
1
$begingroup$
@MichaelSeifert Well, actually a Fibonacci is shorter with the pure function method: i.stack.imgur.com/Fg38W.png
$endgroup$
– Szabolcs
Feb 22 at 13:40
add a comment |
3
$begingroup$
I'm going to have to remember this trick the next time I'm over on PCG Stackexchange.
$endgroup$
– Michael Seifert
Feb 22 at 13:34
$begingroup$
@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 anIf[...]
. That will sometimes make the code longer than the pattern matching version.
$endgroup$
– Szabolcs
Feb 22 at 13:38
1
$begingroup$
@MichaelSeifert Well, actually a Fibonacci is shorter with the pure function method: i.stack.imgur.com/Fg38W.png
$endgroup$
– Szabolcs
Feb 22 at 13:40
3
3
$begingroup$
I'm going to have to remember this trick the next time I'm over on PCG Stackexchange.
$endgroup$
– Michael Seifert
Feb 22 at 13:34
$begingroup$
I'm going to have to remember this trick the next time I'm over on PCG Stackexchange.
$endgroup$
– Michael Seifert
Feb 22 at 13:34
$begingroup$
@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.$endgroup$
– Szabolcs
Feb 22 at 13:38
$begingroup$
@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.$endgroup$
– Szabolcs
Feb 22 at 13:38
1
1
$begingroup$
@MichaelSeifert Well, actually a Fibonacci is shorter with the pure function method: i.stack.imgur.com/Fg38W.png
$endgroup$
– Szabolcs
Feb 22 at 13:40
$begingroup$
@MichaelSeifert Well, actually a Fibonacci is shorter with the pure function method: i.stack.imgur.com/Fg38W.png
$endgroup$
– Szabolcs
Feb 22 at 13:40
add a comment |
2
$begingroup$
Here is a dedicated Q/A on this topic.
$endgroup$
– Leonid Shifrin
Feb 22 at 16:52
2
$begingroup$
This is discussed in the documentation for
[
Slot](https://reference.wolfram.com/language/ref/Slot.html)
.$endgroup$
– Michael E2
Feb 23 at 3:10