Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

consider this example:

list = {a, b, c};
Table[{Sin[i], Cos[j]}, {i, 1, 3}, {j, list[[i ;;]]}]

(*{{{Sin[1], Cos[a]}, {Sin[1], Cos[b]}, {Sin[1], Cos[c]}}, {{Sin[2], 
   Cos[b]}, {Sin[2], Cos[c]}}, {{Sin[3], Cos[c]}}}*)

for the following code:

{Sin[#], Cos[#]} & /@ list[[# ;;]] & /@ {1, 2, 3}

we can get same result if the slot inside Sin (Sin[#]) takes the values from the outer range ({1,2,3})

so in general, is it possible to specify which range that slot takes from?

share|improve this question
1  
I read this question twice and I still don't understand it. Edit: Okay, thanks to Verbeia's answer I think I understand. I do find the phrasing of the question confusing. –  Mr.Wizard Aug 5 at 23:21
    
@Mr.Wizard, Sorry for not being clear. –  Algohi Aug 6 at 0:04

3 Answers 3

up vote 7 down vote accepted

My first suggestion would be to localize the outer Slot, like this:

With[{indx = #}, {Sin[indx], Cos[#]} & /@ list[[indx ;;]]] & /@  Range[3]

(* {{{Sin[1], Cos[a]}, {Sin[1], Cos[b]}, {Sin[1], Cos[c]}}, {{Sin[2], 
   Cos[b]}, {Sin[2], Cos[c]}}, {{Sin[3], Cos[c]}}} *)

You could also rework the process to get the full set of tuples of {Sin[1],Sin[2],Sin[3]} and {Cos[a],Cos[b],Cos[c]} and keep the ones you want, using something like:

MapThread[#1[[#2 ;;]] &, {Outer[{Sin[#1], Cos[#2]} &, Range[3], list],
   Range[3]}]
share|improve this answer

You can nest Functions to accomplish what I believe you want to:

f = i \[Function] {Sin[i], Cos[#]} & /@ list[[i ;;]];

Array[f, 3] // Column
{{Sin[1],Cos[a]},{Sin[1],Cos[b]},{Sin[1],Cos[c]}}
{{Sin[2],Cos[b]},{Sin[2],Cos[c]}}
{{Sin[3],Cos[c]}}

This looks nicer in the Notebook:

enter image description here

Other methods include:

# /. i_ :> ({Sin[i], Cos[#]} & /@ list[[i ;;]]) & /@ {1, 2, 3}

With[{i = #}, {Sin[i], Cos[#]} & /@ list[[i ;;]]] & /@ {1, 2, 3}  (* Verbeia's *)

Nevertheless I feel that the Table construct is at least as clear as any of these.

share|improve this answer

is it possible to specify which range that slot takes from?

The short answer is "no" (not counting solutions with named arguments since they aren't Slots) but if you really, really want to do it you can write it like I've done below, then #1 refers to the inner Map and #2 refers to the outer Map:

Map[
 Map[
   {Sin[#], Cos[#2]} & @@ # &,
   Thread@{#, list[[# ;;]]}
   ] &,
 {1, 2, 3}
 ]

This technique can be used for more levels I think, so that if you had four Map inside each other you could use #1, #2, #3 and #4 to refer to the different levels. But it isn't worth the trouble to start thinking about it.

share|improve this answer
2  
Nice. consider this also, {Sin[#1], Cos[#2]} & @@@ Thread[{#, list[[# ;;]]}] & /@ Range[3] –  Algohi Aug 6 at 0:50
    
@Algohi Yep, that works too. –  Pickett Aug 6 at 0:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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