Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #39448 > unrolled thread

Understanding recursive code

Started byJonas Thörnvall <jonas.thornvall@gmail.com>
First post2021-02-10 01:36 -0800
Last post2021-02-10 18:01 +0000
Articles 4 — 3 participants

Back to article view | Back to comp.lang.javascript


Contents

  Understanding recursive code Jonas Thörnvall <jonas.thornvall@gmail.com> - 2021-02-10 01:36 -0800
    Re: Understanding recursive code Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-02-10 11:19 +0000
      Re: Understanding recursive code Jonas Thörnvall <jonas.thornvall@gmail.com> - 2021-02-10 05:19 -0800
        Re: Understanding recursive code John G Harris <niam@jghnorth.org.uk.invalid> - 2021-02-10 18:01 +0000

#39448 — Understanding recursive code

FromJonas Thörnvall <jonas.thornvall@gmail.com>
Date2021-02-10 01:36 -0800
SubjectUnderstanding recursive code
Message-ID<fb7855df-e736-40db-b184-8621c13f4d1bn@googlegroups.com>
I had a look at my own recursive code, and realised i am not sure i get what actually break my recursion. Could someone explain.

calcSTR="9*9"

function evalPrimary(calcSTR){
	console.log("evalPrimary");
	//var newSub=calcSTR;
	for(m=0;m<calcSTR.length;m++){
	tempChar=calcSTR.charCodeAt(m);
	    // if multiply or divide
		if(tempChar==42 || tempChar==47 ){
			operand=calcSTR.charAt(m);
			getPrimaryOps(calcSTR);
			calcSubPrimary(calcSTR);
			m++;
		}
	}
}

function getPrimaryOps(calcSTR){
	console.log("getPrimaryOps");
	//First operand
	p=m;op1="";
	while((calcSTR.charCodeAt(p-1)>47 && calcSTR.charCodeAt(p-1)<58) || calcSTR.charCodeAt(p-1)==46){
		op1=calcSTR.charAt(p-1)+op1;
		p--;
	}
	if (calcSTR.charAt(p-1)=="-") {sign="-";} else {sign="+";}
	cutStart=p;
	n=m;op2="";
	//Second operand
	if (calcSTR.charAt(n+1)!="-"){sign2="+";}
	else if (calcSTR.charAt(n+1)=="-"){sign2="-";n++;}
	while((calcSTR.charCodeAt(n+1)>47 && calcSTR.charCodeAt(n+1)<58) || calcSTR.charCodeAt(n+1)==46){
		op2=op2+calcSTR.charAt(n+1);
		n++;
	}
	cutEnd=n;
}

function calcSubPrimary(calcSTR){
	console.log("calcSubPrimary");
	if (evaluation){outSTR+="\nSubStr PRE IN to resolve "+calcSTR;}
	subResult=eval(sign+op1+operand+sign2+op2);
	console.log(outSTR);
	resSign=Math.sign(subResult);
	if (resSign==1){subResult="+"+subResult;}
	subStart=calcSTR.substring(0,cutStart-1);
	subEnd=calcSTR.substring(cutEnd+1,calcSTR.length);
	if (evaluation){outSTR+="\nSub StartStr "+subStart+"\n";}
	if (evaluation){outSTR+="Sub result "+subResult+"="+sign+op1+operand+sign2+op2+"\n";}
	if (evaluation){outSTR+="Sub EndStr "+subEnd+"\n";}
	newSub=subStart+subResult+subEnd;
	if (evaluation){outSTR+="SubStr PRE OUT "+newSub+"\n";}
	evalPrimary(newSub);
}

[toc] | [next] | [standalone]


#39449

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2021-02-10 11:19 +0000
Message-ID<87tuqkgofb.fsf@bsb.me.uk>
In reply to#39448
Jonas Thörnvall <jonas.thornvall@gmail.com> writes:

> I had a look at my own recursive code, and realised i am not sure i
> get what actually break my recursion. Could someone explain.

What you have posted is not a complete example.  First, I have to guess
what the call is that you are debugging.  I've assumed it's

  evalPrimary(calcSTR);

Second, the code references undefined variables.  For example,
'evaluation' is used in conditional tests but is never set.  That's not
the only case of using undefined variables, but it's the most obvious.

Third, the almost exclusive use of global rather than local variables
makes it almost impossible for me to reason about what the code is
doing.  Every function call could, until I can prove otherwise, change
almost every variable referenced in the calling function.  I last
debugged such a program when I stopped being a research assistant
helping non-programmers using FORTRAN IV.  That was in 1979.  I don't
want to start again.  It's a grim job.

I know you will want to just plough on, hammering the code into shape,
but if you were a student of mine I would insist that you start again
with a well thought out high-level plan which can be written in code
without a single global variable.

<snip code>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#39451

FromJonas Thörnvall <jonas.thornvall@gmail.com>
Date2021-02-10 05:19 -0800
Message-ID<35a54f50-8c75-49dc-be35-42ca2ae576fen@googlegroups.com>
In reply to#39449
onsdag 10 februari 2021 kl. 12:20:00 UTC+1 skrev Ben Bacarisse:
> Jonas Thörnvall <jonas.t...@gmail.com> writes: 
> 
> > I had a look at my own recursive code, and realised i am not sure i 
> > get what actually break my recursion. Could someone explain.
> What you have posted is not a complete example. First, I have to guess 
> what the call is that you are debugging. I've assumed it's 
> 
> evalPrimary(calcSTR); 
> 
> Second, the code references undefined variables. For example, 
> 'evaluation' is used in conditional tests but is never set. That's not 
> the only case of using undefined variables, but it's the most obvious. 
> 
> Third, the almost exclusive use of global rather than local variables 
> makes it almost impossible for me to reason about what the code is 
> doing. Every function call could, until I can prove otherwise, change 
> almost every variable referenced in the calling function. I last 
> debugged such a program when I stopped being a research assistant 
> helping non-programmers using FORTRAN IV. That was in 1979. I don't 
> want to start again. It's a grim job. 
> 
> I know you will want to just plough on, hammering the code into shape, 
> but if you were a student of mine I would insist that you start again 
> with a well thought out high-level plan which can be written in code 
> without a single global variable. 
> 
> <snip code> 
> -- 
> Ben.
Well i see i am so "really" close, but yet so far away because the main recursion is split up into two legs A and B. And depending upon the string configuration either A done, B done or A and B done.
And that make me lost about what string to pass to B. 

Is it something that fell out from A or is it the "sub original" string.
Just doing things sequential there is no way to know.

Maybe there should be 3 cases for the substring recursion, but that require you to check the sub string before you start to break it down.
And it would become.
If A and B operands in substring
else If A operands in substring
else If B operands in substring.

But it creates double reading/checking of string, and make what is going on even more complex.










[toc] | [prev] | [next] | [standalone]


#39453

FromJohn G Harris <niam@jghnorth.org.uk.invalid>
Date2021-02-10 18:01 +0000
Message-ID<2s782gt5hnvpt2ff6gfp9t80dpsqn5i31a@4ax.com>
In reply to#39451
On Wed, 10 Feb 2021 05:19:34 -0800 (PST), Jonas Thörnvall
<jonas.thornvall@gmail.com> wrote:

>onsdag 10 februari 2021 kl. 12:20:00 UTC+1 skrev Ben Bacarisse:
>> Jonas Thörnvall <jonas.t...@gmail.com> writes: 
>> 
>> > I had a look at my own recursive code, and realised i am not sure i 
>> > get what actually break my recursion. Could someone explain.
  <snip>

Here's an example of recursion. It's the usual function to calculate 
n factorial. Notice that each call of the function has its own "n".

<html>
<head>
  <title> A recursive function </title>  
<script>
  function factorial(n)
    {
      if (n == 0)
        return 1;
      else
        return n * factorial(n-1);
    }  
</script>  
</head>

<body>
<p> Recursion </p>

<script>
  alert( factorial(5) );
</script>

</body>
</html>

  John

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web