What's the meaning of #0?
$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
New contributor
$endgroup$
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
New contributor
$endgroup$
$begingroup$
Here is a dedicated Q/A on this topic.
$endgroup$
– Leonid Shifrin
9 hours ago
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
New contributor
$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
New contributor
New contributor
New contributor
asked 17 hours ago
ShawnShawn
482
482
New contributor
New contributor
$begingroup$
Here is a dedicated Q/A on this topic.
$endgroup$
– Leonid Shifrin
9 hours ago
add a comment |
$begingroup$
Here is a dedicated Q/A on this topic.
$endgroup$
– Leonid Shifrin
9 hours ago
$begingroup$
Here is a dedicated Q/A on this topic.
$endgroup$
– Leonid Shifrin
9 hours ago
$begingroup$
Here is a dedicated Q/A on this topic.
$endgroup$
– Leonid Shifrin
9 hours ago
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
12 hours ago
$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
12 hours ago
1
$begingroup$
@MichaelSeifert Well, actually a Fibonacci is shorter with the pure function method: i.stack.imgur.com/Fg38W.png
$endgroup$
– Szabolcs
12 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "387"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Shawn is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f191999%2fwhats-the-meaning-of-0%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
12 hours ago
$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
12 hours ago
1
$begingroup$
@MichaelSeifert Well, actually a Fibonacci is shorter with the pure function method: i.stack.imgur.com/Fg38W.png
$endgroup$
– Szabolcs
12 hours ago
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
12 hours ago
$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
12 hours ago
1
$begingroup$
@MichaelSeifert Well, actually a Fibonacci is shorter with the pure function method: i.stack.imgur.com/Fg38W.png
$endgroup$
– Szabolcs
12 hours ago
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 17 hours ago
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
12 hours ago
$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
12 hours ago
1
$begingroup$
@MichaelSeifert Well, actually a Fibonacci is shorter with the pure function method: i.stack.imgur.com/Fg38W.png
$endgroup$
– Szabolcs
12 hours ago
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
12 hours ago
$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
12 hours ago
1
$begingroup$
@MichaelSeifert Well, actually a Fibonacci is shorter with the pure function method: i.stack.imgur.com/Fg38W.png
$endgroup$
– Szabolcs
12 hours ago
3
3
$begingroup$
I'm going to have to remember this trick the next time I'm over on PCG Stackexchange.
$endgroup$
– Michael Seifert
12 hours ago
$begingroup$
I'm going to have to remember this trick the next time I'm over on PCG Stackexchange.
$endgroup$
– Michael Seifert
12 hours ago
$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
12 hours ago
$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
12 hours ago
1
1
$begingroup$
@MichaelSeifert Well, actually a Fibonacci is shorter with the pure function method: i.stack.imgur.com/Fg38W.png
$endgroup$
– Szabolcs
12 hours ago
$begingroup$
@MichaelSeifert Well, actually a Fibonacci is shorter with the pure function method: i.stack.imgur.com/Fg38W.png
$endgroup$
– Szabolcs
12 hours ago
add a comment |
Shawn is a new contributor. Be nice, and check out our Code of Conduct.
Shawn is a new contributor. Be nice, and check out our Code of Conduct.
Shawn is a new contributor. Be nice, and check out our Code of Conduct.
Shawn is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f191999%2fwhats-the-meaning-of-0%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
Here is a dedicated Q/A on this topic.
$endgroup$
– Leonid Shifrin
9 hours ago