What's the meaning of #0?












9












$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?










share|improve this question







New contributor




Shawn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    Here is a dedicated Q/A on this topic.
    $endgroup$
    – Leonid Shifrin
    9 hours ago
















9












$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?










share|improve this question







New contributor




Shawn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    Here is a dedicated Q/A on this topic.
    $endgroup$
    – Leonid Shifrin
    9 hours ago














9












9








9


2



$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?










share|improve this question







New contributor




Shawn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$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






share|improve this question







New contributor




Shawn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Shawn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Shawn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 17 hours ago









ShawnShawn

482




482




New contributor




Shawn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Shawn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Shawn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • $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




$begingroup$
Here is a dedicated Q/A on this topic.
$endgroup$
– Leonid Shifrin
9 hours ago










1 Answer
1






active

oldest

votes


















10












$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]





share|improve this answer









$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 an If[...]. 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











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.










draft saved

draft discarded


















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









10












$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]





share|improve this answer









$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 an If[...]. 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
















10












$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]





share|improve this answer









$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 an If[...]. 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














10












10








10





$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]





share|improve this answer









$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]






share|improve this answer












share|improve this answer



share|improve this answer










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 an If[...]. 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




    $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






  • 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










Shawn is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to label and detect the document text images

Vallis Paradisi

Tabula Rosettana