2
$\begingroup$

I want to apply a function to a list. The Code for that is:

 Function[x,x^2]/@list
 >1,4,9

But when I want to made a function of that function, passing the argument for x^2 doesnt work:

 xsquared[formula_,list_List] := Function[x,formula]/@list
 xsquared[x^2,{1,2,3}]
 >{x^2,x^2,x^2}

Whats the problem here? I evaluate formula == x^2 to true...

Many thanks!

$\endgroup$
6
  • 1
    $\begingroup$ xsquared[formula_, list_List] := (Function @@ {x, formula}) /@ list $\endgroup$
    – Bob Hanlon
    May 2, 2023 at 22:34
  • $\begingroup$ Thanks Bob!! But why is mine not working? $\endgroup$
    – Roberta
    May 2, 2023 at 23:00
  • 1
    $\begingroup$ It is a scoping issue. You need to insure that the x in the first and second arguments to Function have the same meaning. $\endgroup$
    – Bob Hanlon
    May 2, 2023 at 23:08
  • 1
    $\begingroup$ Since you named your function xsquared, there is really no reason to pass in a "function". So, I'm assuming you have a more general problem that you're dealing with. And in the general case, it would be much better to pass in an actual Function as an argument rather than an expression that is intended to be used as a function body. $\endgroup$
    – lericr
    May 2, 2023 at 23:11
  • $\begingroup$ @lericr Well, in the end I would like to run different functions over that list. But I dont understand what you mean - x^2 is one of these actual functions... $\endgroup$
    – Roberta
    May 2, 2023 at 23:24

1 Answer 1

3
$\begingroup$

Extended comment...

Okay, so you've understood the notion of mapping a function over a list, and now you want to generalize. You want to define a function that takes as arguments a function and a list and produces as output the result of mapping that function over the list.

MapFunctionOverList[func_, list_] := func /@ list

So, to map a squaring function over the list {1,2,3} you'd do this:

MapFunctionOverList[Function[x, x^2], {1, 2, 3}]

Why don't you want to pass in an expression like x^2 and build the actual Function inside MapFunctionOverList? Because you need to know what the formal arguments to the function are. If I passed in x^2+y^2, which is the formal argument, x or y? Or we don't even need to get that complicated. z^2 is equivalent to x^2, but if MapFunctionOverList assumes that the formal argument is x, then z^2 cannot be used. And we haven't even talked about the possibility that x is already defined, which means we'll have to come up with something clever in our implementation of MapFunctionOverList to avoid that conflict.

But once we've analyzed this whole situation, MapFunctionOverList doesn't get us anything that built-in functions aren't already giving us. MapFunctionOverList is almost literally just re-implementing Map. But that's not really the point. You will definitely run into situations where you want to pass in a function as an argument to another function. The better way to do that is to pass in an actual Function or a symbol that has down-values. Don't pass in an expression with formal variables that you then turn into a function. Do that function building step outside of your main function. And of course, the main way to do that is just with Function, so again, this is already built in.

Maybe what you're after is related to this comment:

I would like to run different functions over that list

So, you have a list of functions and you want to map each of them over some given list. You can do that like this:

Through[{Map[f], Map[g], Map[h]}[{1, 2, 3}]]

{{f[1], f[2], f[3]}, {g[1], g[2], g[3]}, {h[1], h[2], h[3]}}

Now, we can pass in actual functions instead of dummy symbols:

f = Function[x, x^2];
g[y_] := 99*y;
h = #/17 &;
Through[{Map[f], Map[g], Map[h]}[{1, 2, 3}]]

{{1, 4, 9}, {99, 198, 297}, {1/17, 2/17, 3/17}}

$\endgroup$
1
  • $\begingroup$ Many thanks!!!! $\endgroup$
    – Roberta
    May 3, 2023 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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