Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

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

Search the Archive

Re: on passing arguments to a function, how to break a list into

  • To: mathgroup at smc.vnet.net
  • Subject: [mg103743] Re: on passing arguments to a function, how to break a list into
  • From: David Reiss <dbreiss at gmail.com>
  • Date: Sun, 4 Oct 2009 05:37:39 -0400 (EDT)
  • References: <ha7hvj$5qe$1@smc.vnet.net>

goo[Sequence @@ foo[]]

--David

On Oct 3, 9:01 am, "Nasser Abbasi" <n... at 12000.org> wrote:
> Suppose there is a function foo[] which returns back 2 items, say 'i' and
> 'j'. They must be returned as a list ofcourse, so we have this
>
> foo[] := Module[{i, j},
>      i = 3;
>      j = 7;
>     {i, j}
> ]
>
> Now I have another function goo[] which accepts 2 items as input, and does
> something with them, as in
>
> goo[i_, j_] := Module[{},
>          i + j
> ]
>
> Now I'd like to call foo[], and take its output, and pass it to directly to
> goo[], but I can NOT do the following:
>
>                              goo[foo[]]
>
> because there is no function goo[] which accept a list defined. I could
> solve this easily by making an intermediate step
>
> {i, j} = foo[];   (*first read into local variables*)
> goo[i, j]    (* then call goo[] *)
>
> I tried to see if it is possible to eliminate this intermediate step, as it
> could be unnecessary, and see if there is a function or a trick in
> Mathematica which takes a list and 'unwrap' it for the purpose of making
> function calls.
>
> Assuming such a function or method, lets call it  'W' for now, then I would
> just type
>
>                              goo[ W[foo[]]]
>
> Where W[] would take output from a function, and if it is a list, would
> break it down to its sublists, so  that goo[ W[foo[]] ] would be conver ted
> to  goo[ i,j ].  This W[] function would only ofcourse work in the context
> of function calls, as it does not make sense to write
>
> W[{1,2}] and expect to get just  "1,2" like this, without these being
> grouped in a List[]. So this function W is means only to work in argument
> passing.
>
> I can also solve this easily by making goo[] accepts a list, but I wanted to
> avoid having to type this:
>
> goo[arg_List] := Module[{},
>             arg[[1]] + arg[[2]]
> ]
>
> I think   writing "i+j" is more clear than writing " arg[[1]] + arg[[2]]",
> that is why I am asking this question.
>
> I wish Mathematica has named arguments?  or structs, so I could write
>
> goo[arg_Struct] := Module[{},
>             arg.i + arg.j
> ]
>
> Any ideas?
>
> Thank you,
> --Nasser
>
> __________ Information from ESET NOD32 Antivirus, version of virus signature database 4476 (20091002) __________
>
> The message was checked by ESET NOD32 Antivirus.
>
> http://www.eset.com



  • Prev by Date: Re: ma-notebooks to nb-notebooks conversion
  • Next by Date: Re: Placing images in the coordinate system?
  • Previous by thread: Re: on passing arguments to a function, how to break a list into
  • Next by thread: Re: on passing arguments to a function, how to break a list into