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: [mg70604] Re: [mg70587] Programming style: postfix/prefix vs. functional
  • From: Murray Eisenberg <murray at math.umass.edu>
  • Date: Sat, 21 Oct 2006 05:13:39 -0400 (EDT)
  • Organization: Mathematics & Statistics, Univ. of Mass./Amherst
  • References: <200610200921.FAA11092@smc.vnet.net>
  • Reply-to: murray at math.umass.edu

I'd disagree that using prefix and postfix notation "sacrifices some 
level of readability" -- at least once is used to this additional 
notation.  In fact, they are often used for the very purpose of 
improving readability.

For example,

    f /@ expr

is often more readable than the equivalent

    Map[f, expr]

when expr is itself complicated enough, even sometimes when expr is just 
a brace-delimited list.

Likewise, for an expression expr that is the focus of attention, if you 
just want to check its numeric value,

    expr // N

is often more readable than

   N[expr]

and even if not, it's often convenient to keep typing and put the "// N" 
at the end of a line rather than go back to the start of the line, put 
"N[" there and then return to the end of the line with the closing "]".

Yet another case is composition:

   f@g[expr]

is sometimes easier to "grok" than the equivalent

   f[g[expr]]

with the nested brackets in the latter (and the use of @ so closely 
resembles the traditional function composition operator in mathematics, 
anyway).


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
> 
> 

-- 
Murray Eisenberg                     murray at math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower      phone 413 549-1020 (H)
University of Massachusetts                413 545-2859 (W)
710 North Pleasant Street            fax   413 545-1801
Amherst, MA 01003-9305


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