I am trying to make an Array depending on a function, and I saw that Slotis useful for my situation. There are examples described in https://reference.wolfram.com/language/ref/Array.html?view=all that I'm trying to mimic, but in a more complicated situation.

So, I wrote the following:

Array[g[Slot[1] / 10 + I*Slot[2]/10, Slot[3]*10], {10, 10, 10}]

but instead of actually evaluating the code, it just leaves the Slots unevaluated and just literally prints things like 10 Slot[3] [1, 1, 1]. This even happens with a very simple function, for instance:

g[z_, n_] := n

How can I actually get Mathematica to actually evaluate the slots?

  • $\begingroup$ If you want to learn about programming in Mathematica, I highly recommend Leonid Shifrin's amazing book. $\endgroup$ – Natas Aug 14 '20 at 6:35
  • $\begingroup$ Array[Function[g[Slot[1]/10 + I*Slot[2]/10, Slot[3]*10]], {10, 10, 10}] $\endgroup$ – cvgmt Aug 14 '20 at 12:16

As explained in Tutorial: Pure Functions (tutorial/FunctionalOperations#17469) you need to finalize the definition of a pure function using the ampersand & character.

For instance, a simplified example would be

Array[g[Slot[1]] &, 5]

In your case you can do

Array[g[Slot[1]/10 + I*Slot[2]/10, Slot[3]*10] &, {10, 10, 10}]

Note that Slots are usually written more readably as #n, i.e.

Array[g[#1/10 + I*#2/10, #3*10] &, {10, 10, 10}]
  • $\begingroup$ Thanks, missed the & symbols in the examples, and now I see when I put that in, the #n actually looks like the green italic font instead of just red font without the & (which is why I used 'Slot' in the first place) $\endgroup$ – Max Aug 14 '20 at 6:38
  • $\begingroup$ @Max notice also as mentioned by cvgmt in comments above that you can wrap with Function to use the Slots. $\endgroup$ – CA Trevillian Aug 15 '20 at 1:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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