Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1998
*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 1998

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

Search the Archive

Re: strange function that defines a function---how do I do it?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg15264] Re: strange function that defines a function---how do I do it?
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Wed, 30 Dec 1998 01:50:26 -0500
  • References: <75no5e$sgj@smc.vnet.net> <75q118$20o@smc.vnet.net> <7672ri$ps8$8@dragonfly.wolfram.com>
  • Sender: owner-wri-mathgroup at wolfram.com

David Reiss wrote in message <7672ri$ps8$8 at dragonfly.wolfram.com>...
............
............
>Hi Alan,
>
>Unfortunately this approach uncovers a bug or feature  concerning the
>use of a pattern in a pure function.   If we follow through with your
>example one step further
>
>In[1]:=
>Clear[makefunction];
>
>makefunction[var_, expr_] :=
>  Block[{f, SetDelayed}, f @@ (#_?NumberQ & /@ var) := expr]
>
>In[2]:= list = {x1, x2};
>
>In[3]:= makefunction[list, 6]
>
>In[4]:= ?f
>
>"Global`f"
>f[x1*_?NumberQ, x2*_?NumberQ] := 6
>
>
>If all were well we would expect the following to give 6 as the result
>
>In[5]:= f[2,3]
>
>Out[5]= f[2,3]
>
>However, this does
>
>In[6]:= f[x1 2,x2 3]
>
>Out[6]= 6
>
>What seems to be happening is the following
>
>In[7]:= (#_&)@z
>
>Out[7]= z _
>
>Note the space between the z and the _
>
>In[8]:= FullForm[%]
>
>Out[8]//FullForm=
>
>Times[z,Blank[]]
>
>So, typing the two characters # and _ into an input cell  is inperpreted
>by the frontend as the product #*_ rather  than what we want, which is
>#_.  Indeed when these two  characters are typed, the frontend
>automatically puts  a space between them in the same wayit does when a
>symbol  is preceeded by a number as in
>
>In[9]:= 2x
>
>Out[9]=
>2 x
>
>I tried initially  to use the pattern #_ in a pure  function, but ran
>into this problem, so I  used the following instead
>
>In[10]:= Pattern[#,_]&[z]
>
>Out[10]= z_
>
>
>So, do you think that this auto-formatting of #_ into # _ is a bug or
>feature?
>
>--
>David Reiss
>dreissBLOOP at bloop.earthlink.net
>To send personal email, remove the words  "bloop" and "BLOOP" from the
>email address
>


Thanks David,

Yes, I should have followed it through in this form. The solution, as
you indicate, is to use the fuller form Pattern[#,_?NumberQ]&
in place of #_?NumberQ.

In[1]:=
makefunction2[var_, expr_] :=
Block[{f, SetDelayed}, f @@ ((Pattern[#,_]?NumberQ&)/@ var):= expr]

In[2]:=
list = {x1, x2};

In[3]:=
makefunction2[list, 6]

In[4]:=
DownValues[f]//InputForm

Out[4]//InputForm=
{HoldPattern[f[(x1_)?NumberQ, (x2_)?NumberQ]] :> 6}

In[5]:=
 f[2,3]
Out[5]=
6

The parsing of #_ to Times[Blank[],Slot[1]] seems to be a feature:

#  is the  format for Slot[1].
The initial parsing of #_  is

#_  -->  Slot[1]_

which behaves like

s[1]_

    _ s[1]

FullForm[%]

    Times[Blank[],s[1]]


Allan

---------------------
Allan Hayes
Mathematica Training and Consulting
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565







  • Prev by Date: Re: strange function that defines a function---how do I do it?
  • Next by Date: MultipleListPlot Problem
  • Previous by thread: Re: strange function that defines a function---how do I do it?
  • Next by thread: Algebra on complex expressions: Collect