MathGroup Archive 2006

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

Search the Archive

Re: To apply a function to a List

  • To: mathgroup at smc.vnet.net
  • Subject: [mg65638] Re: [mg65597] To apply a function to a List
  • From: "David Park" <djmp at earthlink.net>
  • Date: Tue, 11 Apr 2006 04:04:52 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Francisco,

Let

testlist = Range[5]
{1, 2, 3, 4, 5}

Then you can either Map f onto the list (see Map in Help)

f /@ testlist
{f[1], f[2], f[3], f[4], f[5]}

or you can set the Attributes of f to Listable (see Attributes in Help) Do
it before defining f if f has a definition.

Attributes[f] = Listable;

f[testlist]
f[1], f[2], f[3], f[4], f[5]}

f[x_] := x^2
f[testlist]
{1, 4, 9, 16, 25}

Here is another function, g, defined as a pure function (look up Function in
Help). It takes the square and subtracts 1.

g = #^2 - 1 &;

g /@ testlist
{0, 3, 8, 15, 24}

In general, you will find the 'functional' programming constructs very
useful.

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/

From: Francisco Javier [mailto:pacoga at ctv.es]
To: mathgroup at smc.vnet.net

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.

--
----
Francisco Javier García Capitán
http://garciacapitan.auna.com



  • Prev by Date: Re: Help with Identities
  • Next by Date: Re: For Loop problem
  • Previous by thread: Re: To apply a function to a List
  • Next by thread: Re: To apply a function to a List