2
$\begingroup$

General question

For a function f[n, a], where a is a list and n is a whole number, what is the correct syntax for

f[n - 5, f[n - 4, f[n - 3, f[n - 2, f[n - 1, f[n, a]]]]]]

?

Update

All I want to do is

fg[n_, w_] := f1@ff[n, w]
fg[n - 2, fg[n - 1, fg[n, list]]]

as shown above, which works, but I am having to manually write out n-3,n-2,n-1,...

Have tried

fg[n_][w_] := f1@ff[n, w]
NestList[fg[n], list, 3]

but doesn't quite work. I think it needs a #-1 in there somewhere, that is applied to the n, but not sure on syntax.

$\endgroup$
11
  • 1
    $\begingroup$ Wait ... do you need help with NestList[] or with that code? $\endgroup$ Nov 18 '14 at 20:57
  • $\begingroup$ @martin I'm also having trouble understanding what you're trying to do here.. $\endgroup$
    – Kris
    Nov 18 '14 at 21:00
  • $\begingroup$ @belisarius It is just the last line I need help with - the fg[n - 2, fg[n - 1, fg[n, list]]] bit $\endgroup$
    – martin
    Nov 18 '14 at 21:05
  • $\begingroup$ @Kris please see update $\endgroup$
    – martin
    Nov 18 '14 at 21:07
  • 1
    $\begingroup$ Nest[fg[#[[1]] - 1, fg @@ ##] &, {n, list}, 2] $\endgroup$ Nov 18 '14 at 21:13
1
$\begingroup$
Last @ Nest[{ #[[1]] - 1,  f1[ ff[ #[[1]], #[[2]] ] ] } &,
            {n, list}, 
            3]
f1[ff[-2 + n, f1[ff[-1 + n, f1[ff[n, list]]]]]]
$\endgroup$
2
  • $\begingroup$ Great - thank you - I was really struggling there! $\endgroup$
    – martin
    Nov 18 '14 at 22:03
  • $\begingroup$ ok changed as advised :) $\endgroup$
    – martin
    Nov 18 '14 at 22:13
4
$\begingroup$

Could it be that you're looking for the function Fold rather than Nest?

Fold[f[n - #2, #1] &, a, Range[5]]
f[-5 + n, f[-4 + n, f[-3 + n, f[-2 + n, f[-1 + n, a]]]]]
$\endgroup$
2
  • 1
    $\begingroup$ +1, maybe better Fold[g[#2, #] &, a, Range[n, n - 2, -1]]. $\endgroup$
    – Kuba
    Nov 19 '14 at 12:09
  • $\begingroup$ @Kuma Thanks, I agree that a one-liner is nicer. I updated the answer $\endgroup$
    – Kris
    Nov 19 '14 at 13:52

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.