Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: block-based programming...

by fruiture (Curate)
on Apr 22, 2004 at 12:27 UTC ( #347325=note: print w/ replies, xml ) Need Help??


in reply to block-based programming...

You'll see the downsides of that approach as soon as you reach blocks F, G and H, which do the "same as .. with ..", which clearly looks like you want to have block C as a function, taking cartein arguments, being called from certain points of your main program with different parameters. I mean, you could write your block-based program and then refactor out all the redundant stuff that you find. Personally, i'd try to find the obvious things before and not write block-stuff at all.

Perhaps writing a subroutine for each block may help. While you write each sub, try to make it as flexible as possible. Finally you only need a main script, keeping the main program's state variables and calling the subs accordng to the overall logic.

You might want to think of an OO model for your data, not only refactoring to subs, but to classes and methods. The main program does then not just keep state "variables", but objects, which you might want to reuse somehwere else...

I'm sure you won't need named blocks in the end, only "if", "while" and subroutines. No redo, but tail recursion.

--
http://fruiture.de


Comment on Re: block-based programming...
Re: Re: block-based programming...
by flyingmoose (Priest) on Apr 22, 2004 at 16:24 UTC
    I agree with your post up until the point about tail recursion. Tail recursion is *easily* eliminated by reducing the recursion to a simple loop. This is much friendlier on your stack, and much more efficient! Other forms of recursion are harder to eliminate, but the refactoring of tail recursion is well known.

    That being said, tail recursion often serves as as decent way to design an algorithm originally, but it should be eliminated when you get the chance.

    In his case, assume the program is a "C" program implemented as a state machine that will run for years. Tail recursion would chew up a tremendous amount of stack and lead to program termination. Refactored as a simple while loop, the program would run forever.

    This is one (not so rare) example where the reality of how a computer works oversteps the theory of basic "computer science". What appears to be logically equivalent is not once you put it on real hardware.

    edit: darn, it looks like I'm turning into a CS professor now... run away! run away!

      This is one (not so rare) example where the reality of how a computer works oversteps the theory of basic "computer science". What appears to be logically equivalent is not once you put it on real hardware.
      What has "real hardware" to do with it? All you need is a compiler that's able to detect tail recursion, and eliminate it, instead of putting the burden on the programmer. Yes, such compilers exist. Ironically, they are more common in environments that are more concerned with the 'theory of basic "computer science"' than in environments that use 'real hardware'.

      Abigail

        So can you give me a list of languages that do/don't eliminate tail recursion automatically? Do you have that list memorized? What gcc versions? MSVC? How about java (some JVM's can, some can't). Does Fortran? What about pascal? COBOL? Python (doesn't)? Where are the efficiency tradeoffs in those various language? Is tail recursion ever faster? (never). How does Perl handle it? What about Ruby? One really can't keep this straight -- especially if you don't confine yourself to a very narrow field of languages. All the more reason to be explicit.

        This also goes back to my earlier comments about being blunt and forthright with code. Tail-recursion isn't natural in this case. In math, it's natural, but where a loop applies, use the loop. So why do it? What is the value there?

        Again, like the other argument, our neurons are wired differently and I doubt we will reach agreement.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://347325]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2014-07-11 23:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (236 votes), past polls