Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Not accepting function as parameter

  • To: mathgroup at smc.vnet.net
  • Subject: [mg71694] Re: Not accepting function as parameter
  • From: "Philipp" <Philipp.M.O at gmail.com>
  • Date: Sun, 26 Nov 2006 05:49:34 -0500 (EST)
  • References: <ekbmj4$f87$1@smc.vnet.net>

Unfortunately, Sin is not a function

In[]:=   Head[Sin]
Out[]=   Symbol

You have the following options:
1. Keep your definition of happy and pass Sin as a function, i.e.,

In[]:=   happy[Sin[#] &, 1, 21] // N
Out[]=   0.0199987

2. Redefine your function to accept both Heads
In[]:=   veryhappy[f : (_Symbol | _Function), a_Integer, b_Integer] :=
           Module[{width = (b - a)/1000}, f[width]];

Now,

In[]:=   veryhappy[Sin, 1, 21] // N
         veryhappy[Sin[#] &, 1, 21] // N
         veryhappy[{Sin}, 1, 21]

give

Out[]=   0.0199987
         0.0199987
         veryhappy[{Sin}, 1, 21]

Cheers,

Philipp.

wooks wrote:
> This is a piece of experimental code. The function happy does not
> evaluate whenever I pass f as a parameter as in the example below.
>
> Clear[happy]
> happy[ f_Function, a_Integer, b_Integer] := Module[{width = (b -
> a)/1000},
>        f[width]];
> 
> happy[ Sin, 1, 21]
> 
> I'd be grateful for help.


  • Prev by Date: Re: Plotting a function -
  • Next by Date: Re: Re: Method option to Solve
  • Previous by thread: Not accepting function as parameter
  • Next by thread: Re: Not accepting function as parameter