# Passing list to NestWhileList

I'm trying to implement the secant root finding method

using NestWhileList as follows:

NestWhileList[{#2, #2 - f[#2] (#2 - #1)/(f[#2] - f[#1])} &, {x0,x1},
eps < Abs@@Differences[#] &, 1]


Where {x0, x1} is a list of numbers, and f[x] is a polynomial.This does not work ("Slot number 2 cannot be filled"). This is somewhat confusing as the documentation for NestWhileList says it

[NestWhileList] generates a list of the results of applying f repeatedly...

I take this to mean that Apply is being used. If I use apply myself:

{#2, #2 - f[#2] (#2 - #1)/(f[#2] - f[#1])} & @@ {x0, x1}


It produces no error. The only way I can get the code to work is this hideous mess using Part:

NestWhileList[{#[[2]], #[[2]] - f[#[[2]]] (#[[2]] - #[[1]])/(f[#[[2]]] - f[#
[[1]]])}&, {x0,x1},eps < Abs@@Differences[#] &, 1]


So my question is two-fold, 1) why does Apply by itself work differently than when NestWhileList applies the function, and, 2) is there a way for me to have cleaner code while still using slots (instead of writing a Function)?

Thank you.

## 1 Answer

"Applying" might be a bit misguiding. (since NestWhileList only passes one argument to the function, there's no reason to assume that it needs to Apply the function.

A simple fix that doesn't involve Part is to simply wrap your function in Apply:

NestWhileList[Apply[{#2, #2 - f[#2] (#2 - #1)/(f[#2] - f[#1])} &], {x0,x1},
eps < Abs@@Differences[#] &, 1]


This is the operator form, which basically does the following:

Apply[f][{x,y,...}] --> Apply[f,{x,y,...}] --> f[x,y,...]