MathGroup Archive 2005

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

Search the Archive

Re: pure functions vs. functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg57446] Re: [mg57415] pure functions vs. functions
  • From: "David Park" <djmp at earthlink.net>
  • Date: Sat, 28 May 2005 05:39:10 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

MR,

First, pure functions are very useful in Mathematica and it's great that you
are looking at them.

Pure functions are usually used where we want to carry out some operation on
expressions without actually going to the bother of defining a named
function. Maybe it is something you want to do on the fly and you are only
going to use it once.

Here is a function that takes the square of an expression.

f[x_]:=x^2

We use it...

f[2a]
4*a^2

But maybe we only wanted to do this once. So why bother defining f. (Of
course, in this case we could just use (2a)^2 but I'm trying to use a simple
example where f might actually be more complicated.) We could just use...

#^2 &[2a]
4*a^2

The pure function is just a substitute for 'f'. Instead of using a name of a
function we simply describe the action we want from the function and don't
bother with assigning a name.

Here is an example where we might want to use pure functions. We are going
to show how a simple linear equation is solved step-by-step for x. (Copy the
code and paste it into a Mathematica notebook to evaluate.)

Print["Equation to solve for x"]
a x + b == c
Print["Subtract b from each side"]
# - b & /@ %%
Print["Divide each side by a"]
#/a & /@ %%

We can do the entire derivation in one cell. The interspersed Print
statements annotate the steps. %% refers to the second previous output
(jumping over the Print statements). Each operation is defined by a pure
function, which is mapped to each side of the equation. It would be
cumbersome to define functions to subtract b from an expression, or to
divide an expression by a because we would never use them again.

The Function statement is just a longer method of writing a pure function.
Sometimes I find Function useful because it is a little more explicit and it
is easier to follow if I'm trying to write nested pure functions.

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





From: Marcin Rak [mailto:umrakmm at cc.umanitoba.ca]
To: mathgroup at smc.vnet.net


Hey everyone, I had a beginner question:  what exactly is the difference
between a pure function and just a function?
ie. the difference between declaring functions using
f[arg1_,...,argn_] := "some expression making use of the arguments"

and explicilty calling Function[{arg1_,...,argn_}, "same expression
making use of the arguments"]

They have different heads!!

Thanks
MR






  • Prev by Date: How To Override FrontEnd/init.m Settings?
  • Next by Date: Re: a dangerous feature of Module[]?
  • Previous by thread: Re: pure functions vs. functions
  • Next by thread: Printing - mathematica cropping the right hand side... :(