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.

I know how to create a function from concrete expression like

Function[{x, y}, x^2 + y^2]

But what if I have a list of expressions list1 and I know the set of variables which can occur in this list. I want to map all expressions of list1 to curresponding functions from these variables. Like

{x^2 + y^2, z^2, x, y}

should be mapped to

{
  Function[{x,y,z}, x^2 + y^2], 
  Function[{x, y, z}, z^2], 
  Function[{x,y,z}, x], 
  Function[{x,y,z}, y]
}

I've tried

Function[{x,y,z}, #] &/@ list1

but this doesn't work. Can someone help me with solution?

share|improve this question
    
Something like g = Function[{x, y, z}, #] & /@ l1; g[[1]][a, b, c]? –  belisarius May 18 at 13:21
    
Or this way?g = Function[{x, y, z}, #] &@l1; g[a, b, c][[1]] –  belisarius May 18 at 13:22
add comment

1 Answer 1

up vote 1 down vote accepted

One way to do it is:

exprs = {x^2 + y^2, z^2, x, y};
Replace[Function[{x, y, z}, body], {Rule[body, #]} & /@ exprs, {-1}]
{
   Function[{x, y, z}, x^2 + y^2], 
   Function[{x, y, z}, z^2], 
   Function[{x, y, z}, x], 
   Function[{x, y, z}, y]
}
share|improve this answer
    
Thank you, it works! –  Artem Malykh May 18 at 20:06
add comment

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.