Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #2028 > unrolled thread
| Started by | Michael Mandelberg <mmandelberg@comcast.net> |
|---|---|
| First post | 2011-05-02 10:52 +0000 |
| Last post | 2011-05-03 09:46 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Please Help Eliminate My Ignorance Michael Mandelberg <mmandelberg@comcast.net> - 2011-05-02 10:52 +0000
Re: Please Help Eliminate My Ignorance "Sjoerd C. de Vries" <sjoerd.c.devries@gmail.com> - 2011-05-03 09:46 +0000
| From | Michael Mandelberg <mmandelberg@comcast.net> |
|---|---|
| Date | 2011-05-02 10:52 +0000 |
| Subject | Please Help Eliminate My Ignorance |
| Message-ID | <ipm2cl$56f$1@smc.vnet.net> |
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]]]. >>
[toc] | [next] | [standalone]
| From | "Sjoerd C. de Vries" <sjoerd.c.devries@gmail.com> |
|---|---|
| Date | 2011-05-03 09:46 +0000 |
| Message-ID | <ipoitu$g68$1@smc.vnet.net> |
| In reply to | #2028 |
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 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]]]. >>
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web