Can Built-in function works as if it has Attributes Listable even if it doesn't have that Attributes

Consider this example:

Log[a, {b, c, d}]
(* {Log[b]/Log[a], Log[c]/Log[a], Log[d]/Log[a]} *)

ClearAttributes[Log, Listable]
Log[a, {b, c, d}]
(* Log[{b, c, d}]/Log[a] *)

Now check ReplaceAll

x /. {{x -> a}, {x -> b}}
(*{a, b}*)

How can ReplaceAll behaves as if it is Listable although it hasn't this Attributes?


Yes, they are considered pseudo-listable. Often they are implemented with something similar to

f[x_, a_List] := f[x, #]& /@ a
  • $\begingroup$ Is this considered a violation of the system and rules that MMA follows. I know it is not necessary (and some times impossible) to make all things follow same rules but this is a kind of disturbing to our organized thinking of MMA. what I am saying here is from positive perspective. $\endgroup$ Mar 12 '15 at 1:25
  • 3
    $\begingroup$ Why would it be a violation? It is just another rule. What if you want a function to be only partially Listable? For instance, if f, above, was Listable, it would be Listable in both arguments. As it is, it is only "Listable" in the second one. So, attributes can greatly simplify things, but most of the time, what they do can be accomplished from you're own code. There are exceptions, though. Study that one closely. $\endgroup$
    – rcollyer
    Mar 12 '15 at 1:43
  • $\begingroup$ Ok I see. that makes sense. thanks $\endgroup$ Mar 12 '15 at 1:59
  • $\begingroup$ @Algohi So is this question about not fully listable functions? Or are you looking for a functions that seem to be fully listable without appriopriate attribute? $\endgroup$
    – Kuba
    Mar 12 '15 at 6:09
  • $\begingroup$ @Kuba, I was surprised how does ReplaceAll and some other functions work as Listable functions although they have not this attribute. I thought no function can work one Listable bases unless they are explicitly declared Listable in their Attributes. It appears that they are working in different way. $\endgroup$ Mar 12 '15 at 14:42


ReplaceAll does not behave as a Listable head. If it did it would be broken. Consider:

SetAttributes[brokenReplaceAll, Listable]

brokenReplaceAll[{1, 2, 3}, {{2 -> "b"}, {2 -> "X"}}]

Thread::tdlen: Objects of unequal length in brokenReplaceAll[{1,2,3},{{2->b},{2->X}}] cannot be combined. >>

If it were Listable then arbitrarily nested rule lists would work and they do not:

{1, 2, 3} /. {1 -> "a", {2 -> "b", {3 -> "c"}}}
ReplaceAll::rmix: Elements of {1->a,{2->b,{3->c}}} are a mixture of lists and nonlists. >>

Instead it simply has particular handling of a list of lists of rules. This is entirely unrelated to Listable.

Internal listable behavior

There is no reason that a built-in function cannot have Listable behavior without that Attribute. The Attribute is merely a high-level abstraction of the concept. As Leonid explains the fast vector functions implement numeric behavior internally at a lower level:

ClearAttributes[Plus, Listable]

Plus[3, Range@7]
{4, 5, 6, 7, 8, 9, 10}

Note that in the example above the output of Range is packed which triggers the low level behavior.


  • 1
    $\begingroup$ As usual, Leonid get's a +1 from me as I had not seen that answer before. Oh, you can have a +1, too. :) $\endgroup$
    – rcollyer
    Mar 12 '15 at 12:43

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.