Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

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

Search the Archive

Re: pure functions vs. functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg57584] Re: [mg57415] pure functions vs. functions
  • From: "Marcin Rak" <umrakmm at cc.umanitoba.ca>
  • Date: Wed, 1 Jun 2005 06:03:58 -0400 (EDT)
  • References: <NDBBJGNHKLMPLILOIPPOCEFKEIAA.djmp@earthlink.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Thanks for the thurough description of when one would go about using the
pure function.
I've never programmed in a language of this sort - I'm used to procedural
and object orianted but Mathematica (which seems to simply be a whole bunch
of statements nested one within the other is something completely
different).

Perhaps you could help me understand also the structure of programs in
Mathematica, by answering whether the following is true:
1) Each line of input in Mathematica is simply at most a whole bunch of
nested commands where the output of the inner is used as the input of the
outer?

2) The only way to program proceduraly in Mathematica is to employ the
entire notebook?

Thanks again
MR
----- Original Message -----
From: "David Park" <djmp at earthlink.net>
To: mathgroup at smc.vnet.net
Subject: [mg57584] Re: [mg57415] pure functions vs. functions


> 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: Re: Re: making an animated picture from many pictures
  • Next by Date: Interrupt[] without confirmation
  • Previous by thread: Re: Re: Re: making an animated picture from many pictures
  • Next by thread: Re: Re: pure functions vs. functions