MathGroup Archive 2002

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

Search the Archive

Re: Checking Programming errors; a ?

I've enhanced the code for PartialEvaluation (see my post, which I
incorrectly attaced to someone else's reply). It is somewhat more
flexible now and does more error checking. It finds the rule in
DownValues that matches f[args] (allowing for more than one rule in
DownValues); it allows the user to specify the position of the "main"
CompoundExpression; and it allows the user to specify an expression
that gets appended to the truncated CompoundExpression (and hence
evaluated and returned). The package is just a bit too large to post
here. It can be downloaded from my web site at

Nevertheless, I can give an outline of the code here (absent the error
checking stuff).

PartialEvaluation[f[args], n, expr]:

(* get the DownValues and "turn them off" *)
dv = DownValues[f];
DownValues[f] = {};

(* find the rule that matches *)
matches = Position[MatchQ[f[args], #]& /@ dv[[All, 1]], True];
match = dv[[ matches[[1, 1]] ]];

(* find the "main" CompoundExpression *)
ppos = Position[match, HoldPattern[CompoundExpression[__]]];
pos = First[Sort[ppos]];

(* extract, truncate, append to, and reinsert it *)
held = Extract[match, pos, Hold];
held = ReplacePart[held, Sequence, {1, 0}];
held = Take[held, n];
held = Join[held, Hold[expr]];
held = ReplacePart[Hold[Evaluate[held]], CompoundExpression, {1, 0}];
match = ReplacePart[match, held, pos, 1];

(* apply the modified rule and restore DownValues *)
result = f[args] /. match;
DownValues[f] = dv;


Jack Goldberg <jackgold at> wrote in message news:<alub76$nld$1 at>...
> Hi Group:
> I often run into this difficulty:  when designing a program, say as a
> module, and testing it for various inputs, I get wrong answers.  What to
> do?  I use a method that works for me but may not be the best available.
> I want to show my method and then ask a question about how it can be
> imporved.  (Oh yes, I abandoned Trace a long time ago!)
> myFunction[f_] := Module[ {L1,L2,L3},
> L1 = ... ;
> L2 = ... ;
> l3 = ... ;
> 	"final step"
> 		]
> To see what went wrong, I use (*  *) selectively as follows:
> Stage 1
> myFunction[f_] := Module[ {L1,L2,L3},
> L1 = ...  (*;
> L2 = ... ;
> L3 = ... ;
> 	"final step" *)
> 	]
> Thus I see if L1 worked as expected.  The next step is to put  (* after L2
> and see if this works.  I continue this til the bitter end and I usually
> find my errors.
> My question;   The process of moving  (*   *)  step by step through the
> program is quite tedious when the code has lots more lines.  What I would
> like is a "meta-program" which (like FoldList) does this job for me.  The
> output of this "meta-program" is the list of outputs of each line in the
> module, probably best printed as a column.
> This sounds like Trace but my problem with Trace is it is terribly
> difficult to read.  For the not-so-subtle programming I do, the only thing
> I need is what expression is returned line by line.
> Any advice?  All remarks are appreciated!
> Jack

  • Prev by Date: Re: LaTeX and Mathematica
  • Next by Date: FW: Re: empirical CDF
  • Previous by thread: Re: Checking Programming errors; a ?
  • Next by thread: RE: trying to pull numbers out of a string from a file.