Re: help with Mathematica 3.0 'features' under NT
- To: mathgroup@smc.vnet.net
- Subject: [mg10788] Re: help with Mathematica 3.0 'features' under NT
- From: Paul Abbott <paul@physics.uwa.edu.au>
- Date: Thu, 5 Feb 1998 00:58:32 -0500
- Organization: University of Western Australia
- References: <6b3rkc$s0l@smc.vnet.net>
calvitti@gulam.ces.cwru.edu wrote: > 2) Is there an elegant way for a default (or optional) argument to a > function to take a value which depends on another argument to the same > function if it's not explicitly passed? e.g. in "foo" I would like to > have "a" default to Length[lst]. > > foo[lst_List, a_Integer : Length[lst_]] := lst[[a]] > > but foo[{5,6,7,8}] returns "List", which implies that it's defaulting > "a" to 0. How about the following? In[1]:= foo[lst_List, a_Integer] := lst[[a]] In[2]:= foo[lst_List] := lst[[Length[lst]]] In[3]:= foo[{5,6,7,8}] Out[3]= 8 Cheers, Paul ____________________________________________________________________ Paul Abbott Phone: +61-8-9380-2734 Department of Physics Fax: +61-8-9380-1014 The University of Western Australia Nedlands WA 6907 mailto:paul@physics.uwa.edu.au AUSTRALIA http://www.pd.uwa.edu.au/~paul God IS a weakly left-handed dice player ____________________________________________________________________