MathGroup Archive 2010

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

Search the Archive

Re: What does & mean?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg107239] Re: What does & mean?
  • From: Richard Fateman <fateman at cs.berkeley.edu>
  • Date: Sat, 6 Feb 2010 03:25:21 -0500 (EST)
  • References: <201002041126.GAA29847@smc.vnet.net> <hkgks4$6p9$1@smc.vnet.net> <4B6C3D17.6040401@cs.berkeley.edu> <34c814851002051316m1253d631w2be5aa21053a9e4@mail.gmail.com>

Leonid Shifrin wrote:
> Richard,
>
>
>     Leonid's confusion relates to the pseudo-definition of a
>     "function" by pattern matching using
>
>     fn[arg_]:= .......
>
>     and the definition of a true function by
>
>     fn= Function[{arg}], ......]   or the brief but obscure ...#..&
>     notation...
>     fn = ....#.... &
>
>     There is a widely held but technically bogus equivalence in the minds
>     of Mathematica users, so Leonid's not alone..
>
>     In particular, there is no pattern matching going on in the fn=
>     ... case, and the SetAttribute[fn, HoldAll] has no effect whatsoever.
>
>
> Can it be that you are not aware of the syntax of pure functions in 
> Mathematica which allows to set Hold-attributes (Function[vars,body, 
> HoldAll] for instance)?  But more to the point.
In fact, I was unaware of this feature, which I suspect was added to the 
language sometime after version 2 or 3.  I was also unaware
of the possibility of using ##  for SlotSequence.

This does not change the fact that SetAttribute[fn,HoldAll] has no 
effect on fn.

I was unaware of your book (available online free! Thanks!) which looks 
rather nice.  But there is
generally a trade-off between being "friendly" and being "accurate".  
Thus using the term "function"
is tricky.  You can use a bunch of rules to define what is conceptually 
a mathematical function ...
a pure mapping from input to output..  or you can talk about a 
"programming language function" which
is (loosely speaking) any procedure, perhaps with side effect,  that 
returns a value..
or you can talk about a Mathematica Function, or   something (....)&,   
which is a syntactic/ evaluation
convention that by-passes pattern matching.

Much of the discussion (not really in response to the original posting 
-- he just wanted to know how to
get help on the "&" symbol, I think) .. has concentrated on the 
evaluation semantics in Mathematica,
which gets in the way of many programmers. How many times do you 
evaluate a symbol?
If you type x=x+1, you  see it is about 255 times.  This causes problems 
that eventually get solved
by trying to block evaluation, sometimes, and then in some controlled 
way, Releasing it.
Using Functions is a way to help do this,  though it can be seen more 
cleanly in "functional programming"
languages. I do not know of any (non-Mathematica)  functional language with this 
"evaluate forever"
semantics, though variants of it are common in rule-based transformation 
languages.


> If you'd care to look at the relevant sections of my book you'd see 
> that I clearly state that pure and pattern-defined functions are very 
> different and used for different purposes (In fact, I had a couple of 
> examples similar to yours to illustrate this).
I looked and yes you do.

... snip....

> fn1 =
>   Function[{arg},
>    Switch[Head[Unevaluated[arg]], Print, "You want to print", Times,
>     "You want to multiply", _, "I have no idea what you want to do"],
>    HoldAll];
>
....

> To my mind,  this explanation should have  left no room for further 
> ambiguities,
.. much better example...

And thanks for mentioning your book, which I had not seen before. It 
seems to have good insights.  (I admit to not
reading its predecessors except the one by Maeder).

RJF

>


  • Prev by Date: Re: arrows disappear in exported 3D graphic
  • Next by Date: Re: What does & mean?
  • Previous by thread: Re: What does & mean?
  • Next by thread: Re: What does & mean?