Re: To apply a function to a List

• To: mathgroup at smc.vnet.net
• Subject: [mg65615] Re: To apply a function to a List
• From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
• Date: Tue, 11 Apr 2006 04:04:23 -0400 (EDT)
• Organization: The Open University, Milton Keynes, UK
• References: <e1cusv\$enp\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Francisco Javier wrote:
> Dear all,
>
> Let f, l be a function and a list respectively. How can we do
>
> Table[f[list[[n]]],{n,1, Length[list]}]
>
> in another way?
>
> Thanks.
>
In general, the built-in function *Map* (or /@) does what you are
looking for:

In[1]:=
lst={a,b,c};
Table[f[lst[[n]]],{n,1,Length[lst]}]

Out[2]=
{f[a],f[b],f[c]}

In[3]:=
f/@lst

Out[3]=
{f[a],f[b],f[c]}

In[4]:=
lst={0,Pi/2,Pi,3Pi/2,2Pi};

In[5]:=
Sin/@lst

Out[5]=
{0,1,0,-1,0}

Since the *Sin* function has the attribute *Listable*, it is
automatically mapped

In[6]:=
Attributes[Sin]

Out[6]=
{Listable,NumericFunction,Protected}

In[7]:=
Sin[lst]

Out[7]=
{0,1,0,-1,0}

Or equivalently in prefix notation

In[8]:=
Sin@lst

Out[8]=
{0,1,0,-1,0}

Or in postfix notation

In[9]:=
lst//Sin

Out[9]=
{0,1,0,-1,0}

You can also do fancy things with the *Apply* built-in function

In[10]:=
Sin@@{lst}

Out[10]=
{0,1,0,-1,0}

You can map pure functions too

In[11]:=
Sin[#]+Cos[#]&/@lst

Out[11]=
{1,1,-1,-1,1}

Hope this helps,
Jean-Marc

```

• Prev by Date: Re: Re: Axes with arrowheads !?
• Next by Date: Re: Problem with the Hannan Rissanen procedure
• Previous by thread: Re: To apply a function to a List
• Next by thread: For Loop problem