Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #2061
| From | "Sjoerd C. de Vries" <sjoerd.c.devries@gmail.com> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: Please Help Eliminate My Ignorance |
| Date | 2011-05-03 09:48 +0000 |
| Organization | Steven M. Christensen and Associates, Inc and MathTensor, Inc. |
| Message-ID | <ipoj0m$g8r$1@smc.vnet.net> (permalink) |
As you can see in tutorial/OperatorInputForms (documentation center) SetDelayed (:=) has a higher precedence than CompundExpression (;). So test[p_] := x = 1; Append[p, p[[4]]]; is interpreted as (test[p_] := x = 1); Append[p, p[[4]]]; Therefore the definition includes only the first part. If you want to include the second as well you need to use parenthesis: test[p_] := (x = 1; Append[p, p[[4]]]) Cheers -- Sjoerd Mathematica questions also answered at Stackoverflow: http://stackoverflow.com/questions/tagged/mathematica > -----Original Message----- > From: Michael Mandelberg [mailto:mmandelberg@comcast.net] > Sent: Tuesday, 3 May 2011 05:24 > To: 'Sjoerd C. de Vries' > Subject: RE: Please Help Eliminate My Ignorance > > Thanks. So how do I write a function that does both things? > > Michael > > -----Original Message----- > From: Sjoerd C. de Vries [mailto:sjoerd.c.devries@gmail.com] > Sent: Monday, May 02, 2011 4:29 PM > To: Michael Mandelberg > Subject: Re: Please Help Eliminate My Ignorance > > Hi Michael, > > In 1. because of SetDelayed (:=) the Part ([[ ]]) statement is not executed > directly. It lays dormant until test is called. In 2. Append and the Part contained > in it are executed immediately. Here the problem with p being not a List that > can be indexed directly leads to an error message. If you call test in 1. with a > argument that is not a list you get the same error message. > > > Cheers -- Sjoerd > > Mathematica questions also answered at Stackoverflow: > http://stackoverflow.com/questions/tagged/mathematica > > On May 2, 12:52 pm, Michael Mandelberg <mmandelb...@comcast.net> > wrote: > > Why does Mathematica like the first, but not the second? > > > > 1. > > Clear[x, testp, p]; > > test[p_] := Append[p, p[[4]]]; > > > > 2. > > Clear[x, testp, p]; > > test[p_] := x = 1; Append[p, p[[4]]]; > > > > Part::partd: Part specification p[[4]] is longer than depth of object. > > > > > > > > Append::normal: Nonatomic expression expected at position 1 in > > Append[p,p[[4]]]. >> > >
Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread
Re: Please Help Eliminate My Ignorance "Sjoerd C. de Vries" <sjoerd.c.devries@gmail.com> - 2011-05-03 09:48 +0000
csiph-web