Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Adding 2 + 2 (stack machine)

by ikegami (Pope)
on Jan 30, 2007 at 18:10 UTC ( #597421=note: print w/ replies, xml ) Need Help??


in reply to Adding 2 + 2

Stack machine!

my @stack; my $acc = 0; sub PUSH() { push(@stack, $acc); } sub POP() { pop(@stack); } sub INC() { $acc++; } sub ADD() { $acc += POP; } INC; INC; PUSH; ADD; print($acc, "\n");


Comment on Re: Adding 2 + 2 (stack machine)
Download Code
Re^2: Adding 2 + 2 (stack machine,bug fix)
by ikegami (Pope) on Apr 21, 2008 at 19:12 UTC
    The parent assumed the accumulator was initially zero. That's not likely in a real-life scenario, so this removes the assumption:
    my @stack; my $acc = rand; sub PUSH() { push(@stack, $acc); } sub POP() { pop(@stack); } sub INC() { $acc++; } sub ADD() { $acc += POP; } sub SUB() { $acc -= POP; } PUSH; SUB; INC; INC; PUSH; ADD; print("$acc\n");

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2014-12-25 21:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (163 votes), past polls