Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'Lists as arguments of functions (?obvious?)' topicStudent Support Forum > General > "Lists as arguments of functions (?obvious?)"

< Previous Comment | Next Comment >Help | Reply To Comment | Reply To Topic
Author Comment/Response
Forum Moderator
email me
10/27/98 12:18pm

>
> >
> > >
> > > Is it possible to apply a list of elements as arguments of a function without going through ''for'' and ''table''?
> > >
> > > eg: something that transforms {a,b,c} in f[a,b,c]
> > >
> > > If not is it possible to make this as a ''suffix'' form? (eg: {a,b,c} </?> f )
> > >
> > > If possible reply to mail address.
> > >
> > > Thanks
> > >
> >
> > =====
> >
> > It is not exactly clear what you are wanting to do. I will guess that you have
> > some function that evaluates for 1, 2, and 3 as f[1], f[2], and f[3] respectively, but that f[{1,2,3}] returns at least partially unevaluated. If the goal is to apply f to 1, 2, and 3, that can be done in at least two ways:
> >
> > - Use Map
> >
> > In[1]:= Map [f, {1,2,3}]
> > Out[1]= {f[1],f[2],f[3]}
> >
> > If applying f to a List is common you can use the Listable Attribute
> >
> > In[2]:= SetAttributes[f, Listable]
> >
> > Now f is automatically applied to each element of the list:
> >
> > In[3]:= f[{1,2,3}]
> > Out[3]= {f[1],f[2],f[3]}
> >
> > Tom Zeller
> > Forum Moderator.
>
> No, not like this. I have a list {1,2,3} and I want to apply it to a three argument function f. What I do is this:
>
> f[ {1,2,3}[[1]]], {1,2,3}[[2]], {1,2,3}[[3]] ]
>
> There must be an easier way...
>
>
>
>

=====

You can use Sequence and Apply:

In[36]:= Apply [Sequence, {1,2,3}]
Out[36]= Sequence[1,2,3]

In[37]:= f[x_, y_, z_] := x + y + z

In[38]:= f[1,2,3]
Out[38]= 6

In[40]:= f[Apply[Sequence,{1,2,3}]]
Out[40]= 6

However, I prefer to use Lists rather than attempt to get around them, since Lists are common forms in Mathematica. In this case, in order to deal with
{1,2,3} for which f is not yet defined, i.e.

In[57]:= f[{1,2,3}]
Out[57]= f[{1,2,3}]

I add to the definition of f so that it can handle a list.

In[58]:= f[{x_, y_, z_}]:= x + y + z

In[59]:= f[{1,2,3}]
Out[59]= 6

Now f has two definitions and it will use the one that matches the particular argument.

In[60]:= ?f
''Global`f''
f[x_, y_, z_] := x + y + z

f[{x_, y_, z_}] := x + y + z

I suggest that you look at the problem in your more recent post from this point
of view as well.

Tom Zeller
Forum Moderator


URL: ,

Subject (listing for 'Lists as arguments of functions (?obvious?)')
Author Date Posted
Lists as arguments of functions (?obvious?) Pedro Alves 10/26/98 4:32pm
Re: Lists as arguments of functions (?obvious?) Forum Modera... 10/27/98 09:13am
Re: Lists as arguments of functions (?obvious?) Pedro Alves 10/27/98 12:07pm
Re: Lists as arguments of functions (?obvious?) Forum Modera... 10/27/98 12:18pm
Re: Lists as arguments of functions (?obvious?) Aaron Honecker 11/13/98 10:18am
< Previous Comment | Next Comment >Help | Reply To Comment | Reply To Topic