0
$\begingroup$

I tried to join a series of evaluations with nested slots as follows:

#^2 & /@ #^2 & /@ Range[10]

However, the results were somewhat different from what I expected...

{1, 4, 9, 16, 25, 36, 49, 64, 81, 100}

The following code gave me the expected results:

#^2 & /@ (#^2 & /@ Range[10])

as

{1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000}

I assumed that nested slots would be evaluated from the outside. Do we have to use ugly parentheses in this case? Is there any smart way to handle a series of evaluations with slots?

$\endgroup$
9
  • 2
    $\begingroup$ The issue is actually with operator precedence. That's why you need the parentheses--because Function (&) binds more than Map (/@). You can check out a symbol's precedence using the function Precedence. $\endgroup$
    – b3m2a1
    Commented Mar 20, 2017 at 5:30
  • 1
    $\begingroup$ Just use Range[10]^2^2 or Range[10]^4 $\endgroup$
    – Bob Hanlon
    Commented Mar 20, 2017 at 5:32
  • $\begingroup$ Or, you can compose: Composition[#^2 &, #^2 &] /@ Range[10] $\endgroup$ Commented Mar 20, 2017 at 5:35
  • $\begingroup$ I am surprised that things like f/@2 does not produce errors. $\endgroup$
    – vapor
    Commented Mar 20, 2017 at 7:07
  • $\begingroup$ @MB1965 unfortunately Precedence is useless in general, as shown in my naive answer in the linked topic :) $\endgroup$
    – Kuba
    Commented Mar 20, 2017 at 7:09

0

Browse other questions tagged or ask your own question.