1
$\begingroup$

This question already has an answer here:

Is there a way to make the Function construct accept a list as argument ?

Writing

Function[{x,y},{x+y,x-y}]

does not work, since it accepts two arguments and not a single list.

My aim is to generate a table of functions, say

Table[Function[{x,y},{x + k y,x - k y}] , {k,1,10}]

and then to compose them in different ways. Since every such function returns a list, I would like it also to accept a list, to enable composition.

$\endgroup$

marked as duplicate by Leonid Shifrin, m_goldberg, Michael E2, user9660, MarcoB Feb 28 '16 at 15:05

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    $\begingroup$ Function[list, {list[[1]] + list[[2]], list[[1]] - list[[2]]}][{a, b}] $\endgroup$ – Quantum_Oli Feb 28 '16 at 13:21
  • 1
    $\begingroup$ Also may be useful: (8382) $\endgroup$ – Mr.Wizard Feb 28 '16 at 16:14
3
$\begingroup$

The simple way is to write

func = Table[With[{k = k}, {#[[1]] + k #[[2]], #[[1]] - k #[[2]]} &], {k, 10}];

then

Through[func[{a, b}]]

gives

{{a + b, a - b}, {a + 2 b, a - 2 b}, {a + 3 b, a - 3 b}, {a + 4 b, a - 4 b}, 
 {a + 5 b, a - 5 b}, {a + 6 b, a - 6 b}, {a + 7 b, a - 7 b}, {a + 8 b, a - 8 b}, 
 {a + 9 b, a - 9 b}, {a + 10 b, a - 10 b}}
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.