MathGroup Archive 2003

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

Search the Archive

Re: What's legit here?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg39962] Re: [mg39940] What's legit here?
  • From: Dr Bob <drbob at bigfoot.com>
  • Date: Thu, 13 Mar 2003 03:02:52 -0500 (EST)
  • References: <200303120731.CAA19135@smc.vnet.net>
  • Reply-to: drbob at bigfoot.com
  • Sender: owner-wri-mathgroup at wolfram.com

You're breaking all the rules of functional programming, but never mind 
that for now.

Look up the syntax of the If statement, and you'll see that it has 2, 3, or 
4 arguments.  Each argument (of ANY function) is an expression.  Enclosing 
an expression in parentheses has no effect whatsoever, except that 
sometimes the parentheses are necessary for grouping, as in

a[x_] := (expr1; expr2; expr3)
a[2]

expr3

versus

b[x_] := expr1; expr2; expr3
b[2]

expr3
expr1

or an expression like

(1 - x)^2

Note that expr1; expr2; expr3 is a CompoundExpression (look it up).  This 
is how expressions are grouped together.  Evaluating {expr1; expr2; expr3} 
results in the list {expr3}, while evaluating (expr1; expr2; expr3) results 
in the value of expr3.

If your code returns failu, it is because either enalist[[result]] isn't 1 
or res2 isn't equal to result.  Only when both are true does Return[result] 
get evaluated.

Also, Return is rarely needed, and tends to lead to "spaghetti code".  Your 
code is equivalent to the following code:

ranqrs := Module[{res2}, ptab = Table[0, {i, NN}];
    If[changeq == 1, qtab = Table[Random[Integer, {1, NN - 1}], {i, NN - 
1}]];
    If[changer == 1, rtab = Table[Random[Integer, {0, NN - 1}], {i, NN - 
1}]];
    If[changes == 1, stab = Mod[Table[1, {i, NN - 1}] - rtab, NN]];
    matall; matmul; result = newres;
    If[enalist[[result]] == 1,
          newmmt; res2 = newres; If[res2 == result, result, failu],
          failu]]

Bobby

On Wed, 12 Mar 2003 02:31:36 -0500 (EST), Steve Gray <stevebg at adelphia.net> 
wrote:

> 	In the following function, most of which you can ignore,
> there is an If [ enalist ... etc. where several statements to be
> executed if the If succeeds are grouped with ( ) . This works but I
> thought the right way to group several statements together was with
> { }. The latter does not work and I find I don't know what the correct
> way is. As usual, Mathematica did not complain with the { } or the ( ). 
> The
> effect was to have the If always fail and for the function to always
> Return [ failu ].
>
> Whether  the comments get in the way, I don't know, but if someone
> would define what's legal here, I would appreciate it.
> 	Thank you.
>
>
>
> ranqrs := Module[{res2}, ptab = Table[0, {i, NN}];
> If[changeq == 1, qtab = Table[Random[Integer, {1, NN - 1}], {i, NN
> - 1}]];
> If[changer == 1, rtab = Table[Random[Integer, {0, NN - 1}], {i, NN - 
> 1}]]; If[changes == 1, stab = Mod[Table[1, {i, NN - 1}] - rtab, NN]];
> matall;                                  (* Make matrices from model.     
> *)
> matmul;                                  (* Compute matrix product.       
> *)
> result = newres;
> If [ enalist[[result]] == 1,                (* If a good value = > try 
> again; *)
> 	  ( newmmt;                              (* make a new model &
> target,   *)
> 	    res2 = newres;                       (* do another test.
> *)
> If [res2 == result, Return[result]]; (* If agree, return good result. *)
> )];
> Return[failu];                            (*If no agree or no
> interest, 0. *)
> ]
>
>



-- 
majort at cox-internet.com
Bobby R. Treat



  • Prev by Date: Re: Compile + Module => Memory Leak
  • Next by Date: RE: What's legit here?
  • Previous by thread: What's legit here?
  • Next by thread: Re: What's legit here?