Message Boards Message Boards

1
|
19313 Views
|
9 Replies
|
8 Total Likes
View groups...
Share
Share this post:

Problem With Nested Pure Functions

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
9 Replies
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
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
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
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
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
Thanks to everyone who replied to my question.  I'm impressed by the answers.
POSTED BY: Frank Kampas
Posted 11 years 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
Posted 11 years 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

Hi Rich,

I have been looking for something like this! THANK YOU!!!!!!!!!!!!!

POSTED BY: Lori Johnson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract