4

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?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
t-smart
  • 2,031
  • 8
  • 21

1 Answers1

5

"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.

Kuba
  • 136,707
  • 13
  • 279
  • 740