Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to call a function and have that function use an argument defined in an enclosing function?


set = Table@Range@50;

posE[set_] := Select[set, EvenQ]

tform1[mult_,set_] := (#*mult) & /@ set[[posE]];

t1set = tform1[10, set]

which leads to the following message/error,

Part::pspec: Part specification posE is neither an integer nor a list of integers. >>

I would like posE to get its argument set_ from the second argument in tform1 (also named set_).

Is this possible? is there a better way to do this?

(Note that I do not want to combine the two functions into a single function).

share|improve this question
how about: Cases[set, x_?EvenQ :> 10 x]? (without knowing what you actually want to achieve) – Pinguin Dirk May 2 '13 at 7:59
up vote 5 down vote accepted

You have defined a function posE[set] but when you call it, it has no argument. You must call it with an argument. So change the function definition to

 tform1[mult_,set_] := (#*mult) & /@ set[[posE[set]]];

If what you are really after is a list that goes from 20 to 500 by 20s (which is what this code seems to do) then there is much better way to do it.

Range[20, 500, 20]
share|improve this answer
No, I was just using these functions as an example. Thanks anyway. – geordie May 2 '13 at 7:57

is it too early to add an answer to my own question?

I just found a way to do it using With:

tform1[mult_, set_, pos_] := 
  With[{posf = pos[set]}, (#*mult) & /@ set[[posf]]];

t1set = tform1[10, set, posE]

This works very similar to @bill s' answer.

share|improve this answer
You could eliminate the With and just use tform1[mult_, set_, pos_] := (#*mult) & /@ set[[pos[set]]] as per Bill s answer. – image_doctor May 2 '13 at 8:28

Your Answer


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.