Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2011

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

Search the Archive

Re: Mathematica daily WTF

  • To: mathgroup at smc.vnet.net
  • Subject: [mg115355] Re: Mathematica daily WTF
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Sat, 8 Jan 2011 03:41:04 -0500 (EST)

Bobby,

the problem with this approach is that if <s> contains side effects (think
i++, for instance), then
echo[] will introduce one additional evaluation of <s>, in addition to the
actual place where <s>
is in code. This may break things. Ingolf's version is free from this
problem, because he uses
pure function which evaluates the argument just once at the
parameter-passing stage. There was
a discussion on a similar topic some while ago, where I posted a solution
similar to both yours
and Ingolf's (modulo debug mode), which is not ideal either but free from
this problem:

http://groups.google.com/group/comp.soft-sys.math.mathematica/browse_thread/thread/bfd67e9122b1fdec

My function will fail in cases like the following:

Clear[a];
ShowIt[a] = 2

(which are presumably pathological uses anyway), and generally when used to
display held arguments.
There are workarounds for my version for such cases but they are more
complex. The debug mode
can be easily added to it as well.

Regards,
Leonid



On Fri, Jan 7, 2011 at 12:11 PM, DrMajorBob <btreat1 at austin.rr.com> wrote:

> I'd try something like:
>
> Clear[echo]
> Attributes[echo] = {HoldAll};
> echo[s_, place_] /; debug :=
>  Print[ToString[Unevaluated[s]], " = ", s, " at ", place]
>
> With a module like this:
>
> debug = True;
> bookSave[num_]:=Module[{book,dir},
>
> (* some code *)
> echo[dir, 1];
> echo[book, 2];
> echo[num, 3];
>
> (* other code *)
>
> ]
>
> The printed output was:
>
> dir$3287 = /Users/keleen/TS/Wings/ at 1
>
> book$3287 = Book1 at 2
>
> 1 = 1 at 3
>
> It's annoying that "num" prints as its value, rather than its name.
>
> Unevaluated is still a mystery.
>
> Bobby
>
> On Thu, 06 Jan 2011 01:01:37 -0600, Ingolf Dahl <ingolf.dahl at telia.com>
> wrote:
>
> > Andrzej,
> > Since you ask for "single examples", I would like to hear your opinion
> > about
> > my "Debugging code snippet"
> >
> > ((If[debug, Print["Place1 ", #]]; #) &)@
> >
> > I have started with the functional identity operator # &@, and then added
> > the global variable debug, which if set to True prints out the value of
> > the
> > variable as side effect. The code snippet can be used to monitor the
> > values
> > of sub expressions during debugging, without disturbing the flow of the
> > program, and it does not necessarily need to be removed afterwards.
> > Best regards
> >
> > Ingolf Dahl
> > ingolf.dahl at telia.com
> >
> > This code snippet can be inserted into d
> >> -----Original Message-----
> >> From: Andrzej Kozlowski [mailto:akoz at mimuw.edu.pl]
> >> Sent: den 3 januari 2011 09:58
> >> To: mathgroup at smc.vnet.net
> >> Subject: [mg115174] Re: Mathematica daily WTF
> >>
> >> I don't want to get involved in what is likely to turn out a
> >> "linguistic"
> > dispute, but I think
> >> your ideas about what constitutes "functional" and "procedural" are
> > misconceived and do
> >> not correspond to what other's mean by these terms. While there is some
> > difference between
> >> the meaning of "function" in mathematics and in programming, both
> >> concepts
> > originate from
> >> the same source. To quote Thompson's "Haskel. The craft of functional
> > programming": "A
> >> function is something that we can picture as a box with some inputs and
> >> an
> > output:..."
> >> followed by a picture which is exactly the same that I used to draw in
> >> my
> > lectures on
> >> introductory set theory and analysis for many years before I heard of
> > functional
> >> programming. In this sense functions are ubiquitous in mathematics and
> > science. "Procedural
> >> programming", on the other hand, is programming by "change of state" or
> > "side-effects", and
> >> as the latter expression suggests, is less natural for the human mind
> >> even
> > if it could be
> >> claimed to c!
> >> orrespond more closely to what goes on at "machine level". In any case,
> >> I
> > cannot think of
> >> any scientific or mathematical problems that can be more naturally
> > formulated in terms of
> >> "side-effects" than in terms of "functions". Perhaps they exist and I my
> > bias is due to several
> >> decades of doing mathematics but I seriously can't think of a single
> > example. Can you
> >> provide one?
> >>
> >> Andrzej Kozlowski
> >>
> >>
> >> On 2 Jan 2011, at 10:55, AES wrote:
> >>
> >> > In article <ifmrvv$pim$1 at smc.vnet.net>,
> >> > Andrzej Kozlowski <akoz at mimuw.edu.pl> wrote:
> >> >
> >> >>
> >> >> anyway, it does not matter as far as the point I was making is
> > concerned,
> >> >> which is that the C-like structure of Mathematica procedural programs
> > is
> >> >> helpful to people (somewhat) familiar with C or Fortran.
> >> >>
> >> >
> >> > I'd argue it is also extremely helpful to people who _think_
> >> physically,
> >> > or if you like procedurally, and who are primarily focused on solving
> >> > problems that have an inherently procedural character.
> >> >
> >> > The successive steps (lines, cells, expressions) in a procedural
> >> program
> >> > will very often state or mimic or reproduce what happens as a function
> >> > of time in a dynamic system, or as a function of distance as a wave
> >> > propagates, or mimic the flow of control in a complex system, or . . .
> >> >
> >> > As such, they simplify the process of _coding_ these programs; they
> >> > _document_ and make readable what the program is doing, step by step;
> >> > they make it easy to _insert later refinements_ inside the procedure
> >> > (e.g., tests for current values or for exceptional cases at points
> >> > within the procedure).
> >> >
> >> > All of these things are much more valuable to some of us in our use of
> >> > Mathematica than the speed at which the code executes, or the brevity
> >> > with which it can be typed.  And none of this is to argue that many
> >> > basic functions within the language (things like Fourier transforms,
> >> > finding matrix eigensolutions, many others) should not be provided and
> >> > used as pre-coded non-procedural routines within larger programs.
> >> >
> >> > I make a lot of use of self-programmed Modules[] in my own
> >> programming.
> >> > The active or working part of the completed program, where numerical
> >> > results get asked fror and results displayed, can be quite briefly
> >> > written, mostly just setting input variables, then calling these
> >> > modules.  But these modules themselves are heavily procedurally coded
> >> > internally, and I think that makes a lot of sense.
> >> >
> >
> >
>
>
> --
> DrMajorBob at yahoo.com
>
>

--0016e64c16e08b49d004993f6556
Content-Type: text/html; charset="ISO-8859-1"
Content-Transfer-Encoding: quoted-printable
X-Sun-Content-Length: 8958

Bobby, <br><br>the problem with this approach is that if &lt;s&gt; contains=
 side effects (think i++, for instance), then <br>echo[] will introduce one=
 additional evaluation of &lt;s&gt;, in addition to the actual place where =
&lt;s&gt; <br>

is in code. This may break things. Ingolf&#39;s version is free from this p=
roblem, because he uses<br>pure function which evaluates the argument just =
once at the parameter-passing stage. There was<br>a discussion on a similar=
 topic some while ago, where I posted a solution similar to both yours <br>

and Ingolf&#39;s (modulo debug mode), which is not ideal either but free fr=
om this problem:<br><br><a href="http://groups.google.com/group/comp.soft=
-sys.math.mathematica/browse_thread/thread/bfd67e9122b1fdec" target="_bla=
nk">http://groups.google.com/group/comp.soft-sys.math.mathematica/browse_th=
read/thread/bfd67e9122b1fdec</a><br>

<br>My function will fail in cases like the following:<br><br>Clear[a];<br>=
ShowIt[a] = 2<br><br>(which are presumably pathological uses anyway), and=
 generally when used to display held arguments. <br>There are workarounds f=
or my version for such cases but they are more complex. The debug mode <br>
can be easily added to it as well.<br><br>Regards,<br>Leonid<br><br><br><br=
><div class="gmail_quote">On Fri, Jan 7, 2011 at 12:11 PM, DrMajorBob <sp=
an dir="ltr">&lt;<a href="mailto:btreat1 at austin.rr.com" target="_blan=
k">btreat1 at austin.rr.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; borde=
r-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
I&#39;d try something like:<br>
<br>
Clear[echo]<br>
Attributes[echo] = {HoldAll};<br>
echo[s_, place_] /; debug :=<br>
  Print[ToString[Unevaluated[s]], &quot; = &quot;, s, &quot; at &quot;,=
 place]<br>
<br>
With a module like this:<br>
<br>
debug = True;<br>
bookSave[num_]:=Module[{book,dir},<br>
<br>
(* some code *)<br>
echo[dir, 1];<br>
echo[book, 2];<br>
echo[num, 3];<br>
<br>
(* other code *)<br>
<br>
]<br>
<br>
The printed output was:<br>
<br>
dir$3287 = /Users/keleen/TS/Wings/ at 1<br>
<br>
book$3287 = Book1 at 2<br>
<br>
1 = 1 at 3<br>
<br>
It&#39;s annoying that &quot;num&quot; prints as its value, rather than its=
 name.<br>
<br>
Unevaluated is still a mystery.<br>
<br>
Bobby<br>
<br>
On Thu, 06 Jan 2011 01:01:37 -0600, Ingolf Dahl &lt;<a href="mailto:ingol=
f.dahl at telia.com" target="_blank">ingolf.dahl at telia.com</a>&gt;<br>
<div>wrote:<br>
<br>
&gt; Andrzej,<br>
&gt; Since you ask for &quot;single examples&quot;, I would like to hear yo=
ur opinion<br>
&gt; about<br>
&gt; my &quot;Debugging code snippet&quot;<br>
&gt;<br>
&gt; ((If[debug, Print[&quot;Place1 &quot;, #]]; #) &amp;)@<br>
&gt;<br>
&gt; I have started with the functional identity operator # &amp;@, and the=
n added<br>
&gt; the global variable debug, which if set to True prints out the value o=
f<br>
&gt; the<br>
&gt; variable as side effect. The code snippet can be used to monitor the<b=
r>
&gt; values<br>
&gt; of sub expressions during debugging, without disturbing the flow of th=
e<br>
&gt; program, and it does not necessarily need to be removed afterwards.<br=
>
&gt; Best regards<br>
&gt;<br>
&gt; Ingolf Dahl<br>
&gt; <a href="mailto:ingolf.dahl at telia.com" target="_blank">ingolf.dahl=
@telia.com</a><br>
&gt;<br>
&gt; This code snippet can be inserted into d<br>
&gt;&gt; -----Original Message-----<br>
&gt;&gt; From: Andrzej Kozlowski [mailto:<a href="mailto:akoz at mimuw.edu.p=
l" target="_blank">akoz at mimuw.edu.pl</a>]<br>
&gt;&gt; Sent: den 3 januari 2011 09:58<br>
&gt;&gt; To: <a href="mailto:mathgroup at smc.vnet.net" target="_blank">ma=
thgroup at smc.vnet.net</a><br>
</div><div><div></div><div>&gt;&gt; Subject: [mg115174] Re: Mathematica dai=
ly WTF<br>
&gt;&gt;<br>
&gt;&gt; I don&#39;t want to get involved in what is likely to turn out a<b=
r>
&gt;&gt; &quot;linguistic&quot;<br>
&gt; dispute, but I think<br>
&gt;&gt; your ideas about what constitutes &quot;functional&quot; and &quot=
;procedural&quot; are<br>
&gt; misconceived and do<br>
&gt;&gt; not correspond to what other&#39;s mean by these terms. While ther=
e is some<br>
&gt; difference between<br>
&gt;&gt; the meaning of &quot;function&quot; in mathematics and in programm=
ing, both<br>
&gt;&gt; concepts<br>
&gt; originate from<br>
&gt;&gt; the same source. To quote Thompson&#39;s &quot;Haskel. The craft o=
f functional<br>
&gt; programming&quot;: &quot;A<br>
&gt;&gt; function is something that we can picture as a box with some input=
s and<br>
&gt;&gt; an<br>
&gt; output:...&quot;<br>
&gt;&gt; followed by a picture which is exactly the same that I used to dra=
w in<br>
&gt;&gt; my<br>
&gt; lectures on<br>
&gt;&gt; introductory set theory and analysis for many years before I heard=
 of<br>
&gt; functional<br>
&gt;&gt; programming. In this sense functions are ubiquitous in mathematics=
 and<br>
&gt; science. &quot;Procedural<br>
&gt;&gt; programming&quot;, on the other hand, is programming by &quot;chan=
ge of state&quot; or<br>
&gt; &quot;side-effects&quot;, and<br>
&gt;&gt; as the latter expression suggests, is less natural for the human m=
ind<br>
&gt;&gt; even<br>
&gt; if it could be<br>
&gt;&gt; claimed to c!<br>
&gt;&gt; orrespond more closely to what goes on at &quot;machine level&quot=
;. In any case,<br>
&gt;&gt; I<br>
&gt; cannot think of<br>
&gt;&gt; any scientific or mathematical problems that can be more naturally=
<br>
&gt; formulated in terms of<br>
&gt;&gt; &quot;side-effects&quot; than in terms of &quot;functions&quot;. P=
erhaps they exist and I my<br>
&gt; bias is due to several<br>
&gt;&gt; decades of doing mathematics but I seriously can&#39;t think of a =
single<br>
&gt; example. Can you<br>
&gt;&gt; provide one?<br>
&gt;&gt;<br>
&gt;&gt; Andrzej Kozlowski<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; On 2 Jan 2011, at 10:55, AES wrote:<br>
&gt;&gt;<br>
&gt;&gt; &gt; In article &lt;ifmrvv$pim$<a href="mailto:1 at smc.vnet.net" t=
arget="_blank">1 at smc.vnet.net</a>&gt;,<br>
&gt;&gt; &gt; Andrzej Kozlowski &lt;<a href="mailto:akoz at mimuw.edu.pl" ta=
rget="_blank">akoz at mimuw.edu.pl</a>&gt; wrote:<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; anyway, it does not matter as far as the point I was maki=
ng is<br>
&gt; concerned,<br>
&gt;&gt; &gt;&gt; which is that the C-like structure of Mathematica procedu=
ral programs<br>
&gt; is<br>
&gt;&gt; &gt;&gt; helpful to people (somewhat) familiar with C or Fortran.<=
br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; I&#39;d argue it is also extremely helpful to people who _thi=
nk_<br>
&gt;&gt; physically,<br>
&gt;&gt; &gt; or if you like procedurally, and who are primarily focused on=
 solving<br>
&gt;&gt; &gt; problems that have an inherently procedural character.<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; The successive steps (lines, cells, expressions) in a procedu=
ral<br>
&gt;&gt; program<br>
&gt;&gt; &gt; will very often state or mimic or reproduce what happens as a=
 function<br>
&gt;&gt; &gt; of time in a dynamic system, or as a function of distance as =
a wave<br>
&gt;&gt; &gt; propagates, or mimic the flow of control in a complex system,=
 or . . .<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; As such, they simplify the process of _coding_ these programs=
; they<br>
&gt;&gt; &gt; _document_ and make readable what the program is doing, step =
by step;<br>
&gt;&gt; &gt; they make it easy to _insert later refinements_ inside the pr=
ocedure<br>
&gt;&gt; &gt; (e.g., tests for current values or for exceptional cases at p=
oints<br>
&gt;&gt; &gt; within the procedure).<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; All of these things are much more valuable to some of us in o=
ur use of<br>
&gt;&gt; &gt; Mathematica than the speed at which the code executes, or the=
 brevity<br>
&gt;&gt; &gt; with which it can be typed.  And none of this is to argue t=
hat many<br>
&gt;&gt; &gt; basic functions within the language (things like Fourier tran=
sforms,<br>
&gt;&gt; &gt; finding matrix eigensolutions, many others) should not be pro=
vided and<br>
&gt;&gt; &gt; used as pre-coded non-procedural routines within larger progr=
ams.<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; I make a lot of use of self-programmed Modules[] in my own<br=
>
&gt;&gt; programming.<br>
&gt;&gt; &gt; The active or working part of the completed program, where nu=
merical<br>
&gt;&gt; &gt; results get asked fror and results displayed, can be quite br=
iefly<br>
&gt;&gt; &gt; written, mostly just setting input variables, then calling th=
ese<br>
&gt;&gt; &gt; modules.  But these modules themselves are heavily procedur=
ally coded<br>
&gt;&gt; &gt; internally, and I think that makes a lot of sense.<br>
&gt;&gt; &gt;<br>
&gt;<br>
&gt;<br>
<br>
<br>
</div></div><font color="#888888">--<br>
<a href="mailto:DrMajorBob at yahoo.com" target="_blank">DrMajorBob at yahoo.=
com</a><br>
<br>
</font></blockquote></div><br>

--0016e64c16e08b49d004993f6556--


  • Prev by Date: Re: Mathematica daily WTF
  • Next by Date: Re: I'm puzzled by drastic truncation of y-axis in DateListLogPlot
  • Previous by thread: Re: Mathematica daily WTF
  • Next by thread: How to change the directory for the docs?