Re: \. and #
- To: mathgroup at smc.vnet.net
- Subject: [mg17477] Re: \. and #
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Mon, 10 May 1999 01:44:21 -0400
- References: <7gdu6u$sev@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
[Apologies if this is a second copy - I sent to group and David on May 1 but it seems not to have got to the group - Allan] David Kaplan <dkaplan at uni.uiuc.edu> wrote in message news:7gdu6u$sev at smc.vnet.net... > Could someone please explain to me what \. means if there is > no replacement rule (->)? Also, what does # mean in a program or > statement such as NestList? > David, (1) \ is not used with rules (that is /. please see below) The information in the Help Browser Help > Help > Reference Guide > Listing of Named Characters (or in The Book) under \[Backslash] \[RawBackslash] \[Continuation] might help. Some combinations of symbols have to be taken together / is used to denote division, as usual In[1]:= 4/2 Out[23]= 2 / by itself is rejected as an incomplete expression. /. is used for replacement: In[2]:= {a}/.a->3 Out[2]= {3} {a}/.a->3 is a special convenience form for ReplaceAll[{a}, a->3] /. by itself is rejectes as an incomplete expression. ---------------------------------------------------------------------------- --------- (2) # is accepted as a symbols but is rarely used in isolation: it is used in constructing what I call a "slot function". A"slot function" is made from a template "body" with slots in it a #1 + b #2 #1 The full form of #1 and #2 are Slot[1] and Slot[2]. You can always see the full form by using FullForm[#1] Slot[1] To make a function we add & to get (a #1 + b #2 #1)& We have to give this at lease two inputs (to fill the slots): (a #1 + b #1 #2)&[p, q, r] This evaluates to the body with each occurrence of Slot[n] replaced with the input in the nth position (notice that r just disappears) a p+b p q [ Here is a slot function used in NestList NestList[#+a&,b, 4] {b,a+b,2 a+b,3 a+b,4 a+b} ] (2) If there are not enough inputs to fill the slots, you will be told. (a #1 + b #1 #2)&[p] Function::"slotn": "Slot number \!\(2\) in \!\(\(\(a\\ #1\) + \(b\\ #1\\ #2\)\) &\) cannot \ be filled from \!\(\((\(\(a\\ #1\) + \(b\\ #1\\ #2\)\) &)\)[P]\)." (sorry about the mess) a p+b p #2 (3) There may be evaluation of the inputs p,q,r before the slots are replaced,. Thus if p has been given a value p = P Then from (a #1 + b #1 #2)&[p, q, r] we first get (a #1 + b #1 #2)&[P, q, r] and then a P+b P q (4) And there may be evaluation after the result from the function: Expand[#1(a+ b #2)]&[p, q, r] a P+b P q (5) Functions are objects in their own right and can use by name: fn = (a #1 + b #1 #2)& fn[p,q,r] a P+b P q (6) FullForm gives the full form for any expression FullForm[a P+b P q] Plus[Times[a,P],Times[b,P,q]] FullForm[(a #1 + b #1 #2)&] Function[Plus[Times[a,Slot[1]],Times[b,Slot[1],Slot[2]]]] (7) All Mathematica expressions are built up from atoms (ab, 2 .1.9 , "pq+s",Sin ...) by a head[ elements] construction: expr0[ expr1,expr2,....] where ei are all expressions. Evaluation is often best thought of as moving the pieces around. (8) There is another, sometimes clearer and more versatile way making a function Function[{x,y}, x(a+ by)] It works in a similarly intuitive, but in a rather more complicated, way. Allan --------------------- Allan Hayes Mathematica Training and Consulting Leicester UK www.haystack.demon.co.uk hay at haystack.demon.co.uk Voice: +44 (0)116 271 4198 Fax: +44 (0)870 164 0565