2
$\begingroup$

I have a function F[i_Integer,B_List,l_list] which spits out a list {B',l'} where B' and l' are of the same dimension as B and l, respectively. Now I want to get F[j,B',l'] and I then want to use the output of that again to get F[k,B'',l''], etc. (note that the integer argument changes at each step). What is the best way to do this? I'd be grateful for any help.

$\endgroup$
1
  • $\begingroup$ I guess I can define my function as F[{B_List,l_List},i_Integer] and then do Fold[F,{B,l},{i,j,k}] but is defining a function which takes as an argument a list of lists a bad practice? $\endgroup$
    – amator2357
    Nov 18, 2020 at 15:57

1 Answer 1

3
$\begingroup$
F[i_Integer, B_List, l_List] := {B - i, l + i}

B = Array[b, 3]; l = Array[L, 4];
SeedRandom[123]; {i, j, k} = RandomInteger[{-5, 5}, 3]

(* {2, -1, 5} *)

Fold[F[#2, Sequence @@ #1] &, {B, l}, {i, j, k}]

(* {{-6 + b[1], -6 + b[2], -6 + b[3]}, {6 + L[1], 6 + L[2], 6 + L[3], 
  6 + L[4]}} *)
$\endgroup$
1
  • $\begingroup$ This is very nice, thanks! $\endgroup$
    – amator2357
    Nov 18, 2020 at 16:40

Your Answer

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

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