       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:=
lst={a,b,c};
Table[f[lst[[n]]],{n,1,Length[lst]}]

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

In:=
f/@lst

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

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

In:=
Sin/@lst

Out=
{0,1,0,-1,0}

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

In:=
Attributes[Sin]

Out=
{Listable,NumericFunction,Protected}

In:=
Sin[lst]

Out=
{0,1,0,-1,0}

Or equivalently in prefix notation

In:=
Sin@lst

Out=
{0,1,0,-1,0}

Or in postfix notation

In:=
lst//Sin

Out=
{0,1,0,-1,0}

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

In:=
Sin@@{lst}

Out=
{0,1,0,-1,0}

You can map pure functions too

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

Out=
{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