Possible Duplicate:
Pass function or formula as function parameter

I am trying to implement a simple Plot[]-like function, with the same synopsis; my first (and unique so far) try would be something along these lines:

MyPlot[f_, {var_, xmin_, xmax_}] := ListPlot[Table[{y, f[y]}, {y, xmin, xmax, (xmax - xmin)/100}]]

however I have to call the function as follows


while I would prefer calling the function just like Plot[]


I just don't know how to strip the "[x]" when a function is used as an argument to another function. This is not only a matter of consistency with Plot[], it is also needed in order to use a multi-argument function as the argument, such as:


which is not possible as far as I know with my simple re-implementation.

  • $\begingroup$ it would be possible by using eg MultiSin[#1,#2]& as the argument. $\endgroup$
    – acl
    Nov 4, 2012 at 22:38
  • $\begingroup$ I just realized I had answered almost the same thing before: Pass function or formula as function parameter $\endgroup$
    – Jens
    Nov 5, 2012 at 0:20

3 Answers 3

SetAttributes[MyPlot, HoldAll]
MyPlot[f_[a___, var_, b___], {var_, xmin_, xmax_}] := 
      ListPlot[Table[{y, f[a, y, b]}, {y, xmin, xmax, (xmax - xmin)/100}]]

MyPlot[Sin[x], {x, 0, 1}]

Mathematica graphics

MultiSin[x_, y_] := Sin[x + y]/(Sqrt[x^2 + y^2])
MyPlot[MultiSin[x, 1], {x, 0, 1}]

Mathematica graphics

MultiMultiMultiSin[v_, x_, y_, z_] := Sin[x + y] Sin[x y z]/Sin[v]/(Sqrt[x^2 + y^2])
MyPlot[MultiMultiMultiSin[5, x, 2, 5], {x, 0, 1}]

Mathematica graphics


If you really want the function to work like Plot, it also has to accept arguments that aren't explicitly functions, but simply expressions, such as x^2.

Here's how you do it:


SyntaxInformation[MyPlot] = {"LocalVariables" -> {"Plot", {2, 2}}, 
   "ArgumentsPattern" -> {_, _, OptionsPattern[]}};
SetAttributes[MyPlot, HoldAll];

Options[MyPlot] = {};

MyPlot[f_, {x_, xmin_, xmax_}, opts : OptionsPattern[]] := 
 Module[{y, localF},
  localF = f /. x -> y;
  ListPlot[Table[{y, localF},
    {y, xmin, xmax, (xmax - xmin)/100}]

MyPlot[Sin[x], {x, 0, 1}]


What I did is to declare the HoldAll attribute so that the expression is passed along unevaluated, and moreover give some syntax information so that you get red typeface if you enter the arguments wrong. Then in the function, I localize the plot variable and call it y instead of the dummy argument x.

  • $\begingroup$ Renaming x doesn't seem to be necessary MyPlot[f_, {x_, xmin_, xmax_}, opts : OptionsPattern[]] := ListPlot[Table[{x, f}, {x, xmin, xmax, (xmax - xmin)/100}]] works just as fine. $\endgroup$ Nov 4, 2012 at 23:09
  • $\begingroup$ @Sjoerd I did this localization because it is sometimes useful, but here it's not needed. Looking for an example of where it's useful, I found that this whole question is probably a duplicate of Pass function or formula as function parameter... $\endgroup$
    – Jens
    Nov 5, 2012 at 0:17


pl[f_[x_], {x_, llim_, uplim_}] := 
 ListPlot[Table[f[x], {x, llim, uplim}]]

pl[Sin[z], {z, 5, 10}]

or better

pl[fb___, {x_, llim_, uplim_}] := ListPlot@Table[fb, {x, llim, uplim}]

which also works with

pl[Sin[x + 3], {x, 5, 10}]


pl[x^2, {x, 5, 10}]

and so on.


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