Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1998

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

Search the Archive

Re: help with Mathematica 3.0 'features' under NT

  • To:
  • Subject: [mg10788] Re: help with Mathematica 3.0 'features' under NT
  • From: Paul Abbott <>
  • Date: Thu, 5 Feb 1998 00:58:32 -0500
  • Organization: University of Western Australia
  • References: <6b3rkc$> 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


Paul Abbott                                   Phone: +61-8-9380-2734
Department of Physics                           Fax: +61-8-9380-1014
The University of Western Australia            Nedlands WA  6907  AUSTRALIA                   

            God IS a weakly left-handed dice player

  • Prev by Date: Re: PDEs & Mathematica.
  • Next by Date: Re: ColorFunction and/or ColorOutput - mkdk
  • Prev by thread: help with Mathematica 3.0 'features' under NT
  • Next by thread: 3D problem solution