The following use of Level is documented, without any example.

enter image description here

Let's try some simple codes :

In[1]  Level[{a, b, c}, {1}, #^2 &]
Out[1]  a^2

In[2]  Level[{a, b, c}, {1}, #1^2 &]
Out[2]  a^2

In[3]  Level[{a, b, c}, {1}, #2^2 &]
Out[3]  b^2

In[4]  Level[{a, b, c}, {1}, #3^2 &]
Out[4]  c^2

I want a pure function like ??? in

In[4]  Level[{a, b, c}, {1}, ??? &]
Out[4]  {a^2,b^2,c^2}

Of course

In[5] Level[{a, b, c}, {1}, {#1^2,#2^2,#3^2} &]

works but
can it be done without directly referring to the length of the list(=3)
so that it works for arbitrary cases (more complex list and levelspec)?

Indirectly referring the length of a list is OK.

  • 2
    $\begingroup$ You can use {##}^2&. $\endgroup$ – Carl Woll Mar 29 at 20:59
  • $\begingroup$ Thank you. Actually I've tried ##^2& and it produced a^b^c^2. $\endgroup$ – imida k Mar 29 at 21:08
  • $\begingroup$ You nay also try: Level[{a, b, c}, {0}, #^2 &] $\endgroup$ – Daniel Huber Mar 30 at 11:27

Background: Level[{a, b}, {1}, f] is f[a, b], f[#] &[a, b] is f[a], and f[##] &[a, b] is f[a,b].

So in your case, you can use Level[{a, b, c}, {1}, {##}^2 &] or Level[{a, b, c}, {0}, #^2 &].


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.