Group Abstract Group Abstract

Message Boards Message Boards

Problem With Nested Pure Functions

GROUPS:
Array[Subscript[\[Lambda], #] &, {3}]
gives
{Subscript[\[Lambda], 1], Subscript[\[Lambda], 2], \
Subscript[\[Lambda], 3]}
However, if I try to generate two lists with different symbols and different lengths, the best I've come up with it
Inner[Function[{a, b},
  Array[Subscript[a, #] &, b]], {\[Lambda], \[Mu]}, {2, 3}, List]
which gives
{{Subscript[\[Lambda], 1], Subscript[\[Lambda], 2]}, {Subscript[\[Mu],
   1], Subscript[\[Mu], 2], Subscript[\[Mu], 3]}}
If I try to change Function to the # & notation, I run into a conflict in the variables
Inner[Array[Subscript[#1, #] &, #2] &, {\[Lambda], \[Mu]}, {2,
  3}, List]
gives
{{Subscript[1, 1], Subscript[2, 2]}, {Subscript[1, 1], Subscript[2,
  2], Subscript[3, 3]}}
Any suggestions?
POSTED BY: Frank Kampas
Answer
8 months ago
This is not directly addressing your question, but if I can, I try to avoid nesting pure functions. An example might be
Inner[Map,{\[Lambda],\[Mu]},Range /@{2, 3},List]/.{a_[n_] -> Subscript[a, n]}
Otherwise, it's easy to confuse the meaning of the various # symbols.
POSTED BY: Jason Grigsby
Answer
8 months ago
it is perfectly fine to nest anonymous functions. the problem is notational. you should only use the (..)& notation once or else WL gts confused. this is discussed in the WL prpgramming tutorial located at http://library.wolfram.com/infocenter/MathSource/5216/ . this is discussed on pp. 32-34 in the pdf version and p.41 shows a 'triple nested' anonymous function for a Game of Life program.
POSTED BY: Richard Gaylord
Answer
6 months ago
The following is another option which allows me to pair the symbols and listlength.
Clear["`*"];
(* Define the function that generates a list-of-lists *)
array[{s_,n_}] := Array[Subscript[s,#]&, n]
(* Call the function as often as needed with {symbol, listlength} pairs *)
array[#]& /@ {{ \[Lambda], 3},{\[Kappa], 4},{\[Delta], 5} }
POSTED BY: Isaac Abraham
Answer
8 months ago
Jason, your answer inspired me to try
Inner[Map, {Subscript[\[Lambda], #] &, Subscript[\[Mu], #] &},
Range /@ {2, 3}, List]
which also works.
Thanks
POSTED BY: Frank Kampas
Answer
8 months ago
Frank, that was the one I kept trying to come up with but I had some sort of mental block. I eventually settled on just using replacement. I'm glad you figured it out because I was getting really frustrated with myself. I like the other solutions here too.
POSTED BY: Jason Grigsby
Answer
8 months ago
Variations on a theme:
Thread /@ {Subscript[λ, Range[2]], Subscript[μ, Range[3]]}

MapThread[Thread[Subscript[##]] &, {{λ, μ}, Range /@ {2, 3}}]

MapThread[Thread[Subscript[#1, Range[#2]]] &, {{λ, μ}, {2, 3}}]

MapThread[Composition[Thread, Subscript], {{λ, μ}, Range /@ {2, 3}}]
POSTED BY: Michael Rogers
Answer
8 months ago
Thanks to everyone who replied to my question.  I'm impressed by the answers.
POSTED BY: Frank Kampas
Answer
8 months ago
it is perfectly fine to nest anonymous functions. the problem is notational. you should only use the (..)& notation once or else WL gts confused. this is discussed in the WL prpgramming tutorial located at http://library.wolfram.com/infocenter/MathSource/5216/ . this is discussed on pp. 32-34 in the pdf version and p.41 shows a 'triple nested' anonymous function for a Game of Life program.
POSTED BY: Richard Gaylord
Answer
6 months ago