MathGroup Archive 2006

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

Search the Archive

Re: Programming style: postfix/prefix vs. functional

  • To: mathgroup at smc.vnet.net
  • Subject: [mg70597] Re: Programming style: postfix/prefix vs. functional
  • From: dimmechan at yahoo.com
  • Date: Sat, 21 Oct 2006 05:13:28 -0400 (EDT)
  • References: <eha5kn$b82$1@smc.vnet.net>

For a newcomer, as you said you are, I would
suggest to use the complete name of the higher order
 built-in functions Map, Apply, MapAll.

You should also use the /. and //. notations.

Also you can write your code containg Map, Apply etc
and converting to InputForm to get it with compact notation.
This can be done by selecting the Cells, and then press
simultaneously Shift+Ctrl+I.

Some examples follow:

Here is a user defined function that mofifies slightly the Plot
function in order to deal with periodic discontinuus functions like
Tan[x].

plotDisc[f_, {x_, a_, b_, c_}, opts___] := Show[Map[Plot[f, {x, Part[#,
1], Part[#, 2]}, opts,DisplayFunction -> Identity] &,
Partition[Range[a, b, c],2, 1]], DisplayFunction -> $DisplayFunction]

Here is an example of application

plotDisc[Tan[x], {x, 0, 8Pi, Pi/2}, PlotStyle -> Blue, Frame -> True,
Axes -> False]
(*plot to be displayed*)

Here is the same function after it was converted to InputForm

plotDisc[f_, {x_, a_, b_, c_}, opts___] := Show[(Plot[f, {x, #1[[1]],
#1[[2]]}, opts, DisplayFunction -> Identity] & ) /@ Partition[Range[a,
b, c], 2, 1],
DisplayFunction -> $DisplayFunction]

Here is another user defined function that demonstrates the used
ploints by the
Plot algorithm and as well draw vertical lines connected them to
horizontal axis.

plotAlgor[f_, {x_, a_, b_}, opts___] := Show[ff = Plot[
    f, {x, a, b}, opts, DisplayFunction -> Identity], Map[
        Graphics[{{Red,
           Line[{{First[#], 0}, #}]}, {Green, PointSize[0.018],
Point[#]}}]
&, Cases[ff, {z_?
    NumberQ, w_?NumberQ}, Infinity]], DisplayFunction ->
$DisplayFunction]

Here is an example of application

plotAlgor[x, {x, 0, 2Pi}, PlotStyle -> Blue];
plotAlgor[Sin[x], {x, 0, 2Pi}, PlotStyle -> Blue];
(*plots to be displayed*)

and here is the same function converted in InputForm

plotAlgor[f_, {x_, a_, b_}, opts___] := Show[ff = Plot[f, {x, a, b},
opts, DisplayFunction -> Identity],
   (Graphics[{{Red, Line[{{First[#1], 0}, #1}]}, {Green,
PointSize[0.018], Point[#1]}}] & ) /@
    Cases[ff, {(z_)?NumberQ, (w_)?NumberQ}, Infinity], DisplayFunction
-> $DisplayFunction]

See also the following

ReplaceAll[log[a b c d],log[x_ y_]\[Rule]log[x]+log[y]]
log[a]+log[b c d]

The same expression in InputForm

log[a*b*c*d] /. log[(x_)*(y_)] -> log[x] + log[y]
log[a] + log[b*c*d]

ReplaceRepeated[log[a b c d],log[x_ y_]\[Rule]log[x]+log[y]]
log[a]+log[b]+log[c]+log[d]

The same expression in InputForm

log[a*b*c*d] //. log[(x_)*(y_)] -> log[x] + log[y]
log[a] + log[b] + log[c] + log[d]

Following Ted Esrek's advice (see
http://www.verbeia.com/mathematica/tips/Tricks.html)
I suggest that users in your position get a copy of The Mathematica
Book and read sections 1.0 through 1.10,  sections 2.1 through 2.4 (260
pages of light reading).

Here are also some other links for introduction to Mathematica
programming

http://library.wolfram.com/infocenter/TechNotes/Mathematica/
http://library.wolfram.com/infocenter/MathSource/5216/

Lastlly I highly recomend you two books

Introduction to Programming with Mathematica, Third Edition
(by Wellin, Gaylord and Kamin)

Mastering Mathematica: Programming Methods and Applications, Second
Edition
(by John W. Gray)

Regards
Dimitris

Will Robertson wrote:
> Hello,
>
> As a newcomer to Mathematica, I'm a little unsure on what "good style"
> would be in this programming language. I notice that several functions
> have prefix and postfix notations such as //. for ReplaceRepeated, /@
> for Map, and so on.
>
> Clearly using these forms makes the code more compact, but sacrifices
> some level of readability. Are there guidelines or suggestions that
> have built up over the years of whether these are "good" or "bad" to
> use?
>
> If it's simply personal preference, what do you like to use?
> --
> Many thanks,
> Will Robertson


  • Prev by Date: Re: damped oscilations data fit
  • Next by Date: Re: Programming style: postfix/prefix vs. functional
  • Previous by thread: Re: Programming style: postfix/prefix vs. functional
  • Next by thread: RE: Programming style: postfix/prefix vs. functional