6
$\begingroup$

Are multiple hashtags allowed in NestList usage? If yes, how to implement it?

Example,

 f[{x_, y_}] := {x + y, x - y};
 NestList[f, {100, 75}, 5]

My attempt,

 NestList[{#1+#2,#1-#2} &,{100,75},5]

It does not work.

$\endgroup$

4 Answers 4

10
$\begingroup$

Use Apply.

NestList[Apply[{#1 + #2, #1 - #2} &], {100, 75}, 5]
$\endgroup$
8
$\begingroup$

The argument being supplied to the function is a list. It's a single argument with head List. So, #2 cannot be bound to anything. You can either use Apply (per cvgmt's answer) or use Part:

NestList[{#[[1]] + #[[2]], #[[1]] - #[[2]]} &, {100, 75}, 5]
$\endgroup$
6
$\begingroup$
NestList[{{1, 1}, {1, -1}}.# &, {100, 75}, 5]
$\endgroup$
6
$\begingroup$

Use Function and Apply:

NestList[Apply[Function[{x, y}, {x + y, x - y}]], {100, 75}, 5]
(*{{100, 75}, {175, 25}, {200, 150}, {350, 50}, {400, 300}, {700, 100}}*)

Or following your attempt:

f := Function[{x, y}, {x + y, x - y}]
NestList[Apply[f], {100, 75}, 5]
(*{{100, 75}, {175, 25}, {200, 150}, {350, 50}, {400, 300}, {700, 100}}*)

Or you can define your function as a solution rule and use the resource function SolutionRulesToFunctions as follows:

ReplaceAll[ResourceFunction["SolutionRulesToFunctions"][f[{x, y}] -> {x + y, x - y}]][NestList[Apply[f], {100, 75}, 5]]
(*{{100, 75}, {175, 25}, {200, 150}, {350, 50}, {400, 300}, {700, 100}}*)
$\endgroup$

Your Answer

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

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