4
$\begingroup$

For example, if I define a function like this:

SetAttributes[func,Listable];
func@{{1,2},{3,4}}

it gives the result like:

{{func[1], func[2]}, {func[3], func[4]}}

However, if I make a more specific define, like:

func = #*{1,1}&

then running the same code will create: {{1,2},{3,4}} instead of {{{1,1},{2,2}},{{3,3},{4,4}}}, like what it would normally do when you run {{func[1], func[2]}, {func[3], func[4]}}.

How come? What should I do if I want it to function as I wanted it to?

$\endgroup$
  • $\begingroup$ maybe use func[x_] := x {1, 1}; in defining func? $\endgroup$ – kglr Jan 22 '18 at 9:05
  • $\begingroup$ func = Function[x, x {1, 1}, Listable] does also work... $\endgroup$ – Henrik Schumacher Jan 22 '18 at 18:19
5
$\begingroup$

"what should I do if I want it to function as I wanted it to?"

see: 29169

func = Function[x, x*{1, 1}, Listable];
func @ {{1, 2}, {3, 4}}
{{{1, 1}, {2, 2}}, {{3, 3}, {4, 4}}}

And in case you don't need to keep a 'pure function' approach you can define DownValues as usual:

func // Attributes= {Listable};
funct[x_]:=x{1,1};

"how come?"

func is Listable but by giving OwnValues to it func=#*{1,1}& you make that attribute irrelevant because of a standard evaluation steps.

See: tutorial/TheStandardEvaluationProcedure

The head is evaluated before attributes 'fire'. So

  • func@{{1, 2}, {3, 4}}

  • #*{1, 1}& @ {{1, 2}, {3, 4}}

And there is no information about listability anymore.

| improve this answer | |
$\endgroup$

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.