I saw this question asking about the Listable. The accepted answer did a good explanation on the poor performance of user defined function with Listable. However the answer didn't clarify why the f function in following code doesn't show Listable feature.

f = #[[1]] + #[[2]]^#[[3]] &;
SetAttributes[f, Listable];
f@{{1, 2, 3}, {4, 5, 6}}

output: enter image description here I Know why the code above cannot achieve its goal, and I know how to fix it. I just don't think the above wrong code should produce the above specific output. According to my understanding of how Listable thread function arguments, it should produce the following output:

{{f[1], f[2], f[3]}, {f[4], f[5], f[6]}}

Because the list {{1, 2, 3}, {4, 5, 6}} as argument should be threaded to the innermost level, which in the end is {{f[1], f[2], f[3]}, {f[4], f[5], f[6]}} and stop with some error message since f has Part on the argument.

However, in the reality, the real output shows that actually no Listable is in effect. The List {{1, 2, 3}, {4, 5, 6}} is passed to f as one whole arguments, which can also be proved by running a Trace on it.

Someone please help me?

  • 4
    $\begingroup$ Here are a few hints: SetAttributes[] will only work if you had defined f as f[v_] := v[[1]] + v[[2]]^v[[3]]. Otherwise, an anonymous function with attributes has to be declared this way: f = Function[Null, #[[1]] + #[[2]]^#[[3]], {Listable}]. Now, try your examples again with this in mind. $\endgroup$ May 5, 2020 at 18:40
  • $\begingroup$ Wolfram surprised me again! I've recently read tons of articles on Documentation website , nowhere had I ever seen it says that SetAttributes work differently for f=Function[x, ...] and f[x_]! $\endgroup$
    – Murphy Ng
    May 6, 2020 at 7:18


Your Answer

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

Browse other questions tagged or ask your own question.