Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

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

Search the Archive

Re: Pattern: x_List with conditions on elements

  • To: mathgroup at smc.vnet.net
  • Subject: [mg110924] Re: Pattern: x_List with conditions on elements
  • From: "Nasser M. Abbasi" <nma at 12000.org>
  • Date: Mon, 12 Jul 2010 01:03:37 -0400 (EDT)
  • References: <i13tqo$7h7$1@smc.vnet.net> <i15qqr$ktt$1@smc.vnet.net> <i1c5s1$ccu$1@smc.vnet.net>
  • Reply-to: nma at 12000.org

On 7/11/2010 3:19 AM, telefunkenvf14 wrote:

 >
> How much does Mathematica really benefit by narrowing types of inputs?
> (besides the benefit of overloading symbols) For example, does it help
> above and beyond simply declaring, say,
> SetAttributes[MyFunction]={NumericFunction}?
>
> -RG
>

It is not Mathematica which benefits, it is your program which will.

If you know that a function you are writing will only accept input which 
must be numeric, then better to define the function as such.

This will make your program more reliable, because if somewhere else you 
made a mistake calling the function with the wrong argument, i.e. 
something other than a numeric argument, then you'll catch that right 
away, at the call time, not some time later down the road.

If in addition, you know something about the range of values the input 
must be within, it is better to also add this as well.  Something like

foo[x_?(NumberQ[#]&&Positive[#]&)]:=.....

much better than just writing

foo[x_]:=.....

Since Mathematica does not have data types, the above seems to me to be 
one way around this that I learned.

In languages with type definition, one can define a type to be numeric 
and positive, and then declare variables of that type, then the compiler 
will check that caller argument matches the type of called parameter. 
Learning how to use correct type definition to match the problem at 
hand, can lead to programs with much less errors.

--Nasser






  • Prev by Date: strange bahavior of ArcSinh
  • Next by Date: Re: multivariate interpolation
  • Previous by thread: Re: Pattern: x_List with conditions on elements
  • Next by thread: Re: Pattern: x_List with conditions on elements