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.

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

e.g.,

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

2 Answers 2

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
1  
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

 
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.