MathGroup Archive 1998

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

Search the Archive

Re: annoying Mathematica 3.0 behavior


  • To: mathgroup@smc.vnet.net
  • Subject: [mg10778] Re: annoying Mathematica 3.0 behavior
  • From: sb@stonesoft.com (Stonewall Ballard)
  • Date: Thu, 5 Feb 1998 00:58:24 -0500
  • Organization: StoneSoft, Inc.
  • References: <6b3p4h$rqu@smc.vnet.net>

In article <6b3p4h$rqu@smc.vnet.net>, calvitti@glenn.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. I've tried definding an optional argument with
> 
>   Options[foo] = {a -> Length[lst_]}
> 
> with no luck....Obviously, I can pass a : Infinity and set it
> appropriately within the functions' body after a quick check, but it
> doesn't seem like an elegant solution.

I was unable to figure out definitively from the doc when the default
value is evaluated, but it appears to be evaluated at the time the
function foo is defined.

I would propose using a pair of functions:

foo[lst_List, a_Integer] := lst[[a]] foo[lst_List] := foo[lst,
Length[lst]]

This strikes me as "elegant".

  - Stoney

-- 
Stonewall Ballard                    StoneSoft, Inc.
sb.nospam@stonesoft.com    http://www.stonesoft.com/ Remove ".nospam"
before replying



  • Prev by Date: Solve, Plot and limits
  • Next by Date: DSolve and Erf trouble
  • Prev by thread: annoying Mathematica 3.0 behavior
  • Next by thread: help with Mathematica 3.0 'features' under NT