1
$\begingroup$

Let's say I want to define a function indexed by an natural number $g$, which is given by $$f_g(x)=g+x$$

I could (I guess naively) write the following code to define it up until $g=19$

For[g = 1, g < 20, g++, f[g] = g + # &]

But, for some reason, this doesn't work. If you try to evaluate, let's say

f[3]
f[3][10]

you see that the use of # delays the replacement of $g$ until you give the function an argument. $f_3(10)$ above returns $30$ for example, since that's the value of $g$ at that point in the code.

Now, I am aware I could just define a two-variable function (I think that's actually doable in the original problem I'm working on too) but I would still like to understand what is going on here and how I can get around this without actually avoiding the problem altogether.

Thank you all.

$\endgroup$
2
  • $\begingroup$ Looking at your first equation: f3(10) should be 3+10=13? But later on you want it evaluated as 30? $\endgroup$
    – Syed
    Apr 3, 2022 at 16:43
  • $\begingroup$ I didn't want it evaluated as $30$, but it does so anyway. I wanted $13$, which is not what happens with the above code. This specific question got solved in the answer below, but the solution doesn't quite work for my original code, so I asked a new question here $\endgroup$ Apr 3, 2022 at 16:56

2 Answers 2

3
$\begingroup$

It's somewhat interesting what exactly gets held here (as in Hold). The anonymous function holds its arguments unevaluated, so:

For[g = 1, g < 20, g++, f[g] = g + # &]

Assigns g + # & to f[1] through f[19], in exactly the form.

You could change this to:

For[g = 1, g < 20, g++, f[g] = Evaluate[g + #] &]

And it will subsequently work, as f[1] becomes 1 + # &, f[2] becomes 2 + # & and so on. Note that operator precedence around Evaluate and & can be somewhat non-intuitive. For example, (Evaluate[g] + #) & gives a somewhat unexpected form which holds the Evaluate term in its entirety. I suspect this may be explained somewhere, but I've not personally encountered where.

More generically, you may also want to consider one of the following:

f[g_] := g + # &
f[g_?IntegerQ] := g + # &
$\endgroup$
2
  • $\begingroup$ This exactly what I was looking for, some way to "force" the evaluation to take precedence, but I couldn't figure it out myself. Thank you! $\endgroup$ Apr 3, 2022 at 16:21
  • $\begingroup$ Never mind, it still doesn't work for my original problem. Evaluate just stays there and doesn't do anything. I've asked a new question here with exactly the problem that I'm facing. $\endgroup$ Apr 3, 2022 at 16:55
2
$\begingroup$

Let me know if this is what you intended?

enter image description here

$\endgroup$
3
  • $\begingroup$ Yes that works too. And thank you for that neat "indexed argument" notation, which I did not know :) $\endgroup$ Apr 3, 2022 at 17:09
  • 1
    $\begingroup$ Nice and new to me too. Is there any hidden problem with this as I read that people try to avoid subscript whenever you can? $\endgroup$
    – hana
    Apr 3, 2022 at 18:13
  • $\begingroup$ Subscript[f, 1][x] // Head returns Plus. And should it not have returned f1? $\endgroup$
    – Syed
    Apr 3, 2022 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.