How to resolve the algorithm Jump anywhere step by step in the Raku programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Jump anywhere step by step in the Raku programming language

Table of Contents

Problem Statement

Imperative programs like to jump around, but some languages restrict these jumps. Many structured languages restrict their conditional structures and loops to local jumps within a function. Some assembly languages limit certain jumps or branches to a small range. This task is to demonstrate a local jump and a global jump and the various other types of jumps that the language supports. For the purpose of this task, the jumps need not be used for a single purpose and you have the freedom to use these jumps for different purposes. You may also defer to more specific tasks, like Exceptions or Generator. This task provides a "grab bag" for several types of jumps. There are non-local jumps across function calls, or long jumps to anywhere within a program. Anywhere means not only to the tops of functions! These jumps are not all alike. A simple goto never touches the call stack. A continuation saves the call stack, so you can continue a function call after it ends.

Use your language to demonstrate the various types of jumps that it supports. Because the possibilities vary by language, this task is not specific. You have the freedom to use these jumps for different purposes. You may also defer to more specific tasks, like Exceptions or Generator.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Jump anywhere step by step in the Raku programming language

Source code in the raku programming language

    outer-loop: loop {
        inner-loop: loop {
            # NYI # goto inner-loop if rand > 0.5; # Hard goto
            next inner-loop if rand > 0.5; # Next loop iteration
            redo inner-loop if rand > 0.5; # Re-execute block
            last outer-loop if rand > 0.5; # Exit the loop
            ENTER { say "Entered inner loop block" }
            LEAVE { say "Leaving inner loop block" }
        }
        ENTER { say "Entered outer loop block" }
        LEAVE { say "Leaving outer loop block" }
        LAST  { say "Ending outer loop" }
    }


    my @list = lazy gather for ^100 -> $i {
        if $i.is-prime {
            say "Taking prime $i";
            take $i;
        }
    }
    
    say @list[5];


    die "This is a generic, untyped exception";


    sub foo() { fail "oops" }
    my $failure = foo;
    say "Called foo";
    say "foo not true" unless $failure;
    say "foo not defined" unless $failure.defined;
    say "incremented foo" if $failure++; # exception


  sub foo($i) {
      if $i == 0 {
          die "Are you sure you want /0?";
      }
      say "Dividing by $i";
      1/$i.Num + 0; # Fighting hard to make this fail
  }
  
  for ^10 -> $n {
      say "1/$n  = " ~ foo($n);
  }
  
  CATCH {
      when ~$_ ~~ m:s/Are you sure/ { .resume; #`(yes, I'm sure) }
  }


  

You may also check:How to resolve the algorithm Look-and-say sequence step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Babbage problem step by step in the Modula-2 programming language
You may also check:How to resolve the algorithm Sierpinski triangle step by step in the Euphoria programming language
You may also check:How to resolve the algorithm Sort stability step by step in the Phix programming language
You may also check:How to resolve the algorithm Statistics/Basic step by step in the Hy programming language