Range Over Iterators in COBOL

Starting with version 1.23, the provided language has added support for iterators, allowing us to iterate over virtually anything!

Let’s look at the List type once more. Instead of using a method that returns all elements as a collection, iterators make this much more elegant and efficient.

Here is the full source code:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. HelloIterator.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  HEAD        POINTER.
       01  TAIL        POINTER.
       01  ELEMENT     POINTER.

       01  ELEMENT-STRUCT.
           05  NEXT     POINTER.
           05  VAL      PIC 9(04).

       PROCEDURE DIVISION.
       
       * Initialize the list
       CALL "init-list" USING BY CONTENT 10, 13, 23.

       * Print all elements using an iterator
       CALL "print-all-elements".

       * Generate and print Fibonacci sequence using an iterator
       CALL "gen-fibonacci" USING BY CONTENT 10.

       STOP RUN.

       DECLARATIVES.
       
       * Initialize the list with some values
       INIT-SECTION SECTION.
       INIT-PARA.
           CALL 'Push' USING BY REFERENCE 10.
           CALL 'Push' USING BY REFERENCE 13.
           CALL 'Push' USING BY REFERENCE 23.
           EXIT.
       END SECTION.

       * Define how to push elements to the list
       PUSH-SECTION SECTION.
       PUSH-PARA.
           LINKAGE SECTION.
           01  ELEMENT-VALUE       PIC 9(04).
           PROCEDURE DIVISION USING BY REFERENCE ELEMENT-VALUE.

           IF ELEMENT = NULL
               ALLOCATE ELEMENT TO HEAD
               SET TAIL TO ADDRESS OF ELEMENT-STRUCT
               SET ELEMENT-STRUCT::VAL TO ELEMENT-VALUE
           ELSE
               ALLOCATE ELEMENT TO TAIL::NEXT
               SET TAIL TO ADDRESS OF ELEMENT-STRUCT
               SET ELEMENT-STRUCT::VAL TO ELEMENT-VALUE
           END-IF.
           EXIT.
       END SECTION.

       * Print all elements
       PRINT-SECTION SECTION.
       PRINT-PARA.
           LINKAGE SECTION.
           PROCEDURE DIVISION.

           PERFORM VARYING ELEMENT FROM HEAD BY ADDRESS OF NEXT
           UNTIL ELEMENT = NULL
               DISPLAY ELEMENT::VAL
           END-PERFORM.
           EXIT.
       END SECTION.

       * Generate the Fibonacci sequence
       GEN-FIB-SECTION SECTION.
       GEN-FIB-PARA.
           LINKAGE SECTION.
           01  LIMIT          PIC 99.
           01  FIB-A          PIC 9(04) VALUE 1.
           01  FIB-B          PIC 9(04) VALUE 1.
           PROCEDURE DIVISION USING BY CONTENT LIMIT.

           PERFORM UNTIL FIB-A >= LIMIT
               DISPLAY FIB-A
               COMPUTE FIB-A = FIB-A + FIB-B
               COMPUTE FIB-B = FIB-A - FIB-B
           END-PERFORM.
           EXIT.
       END SECTION.

       END DECLARATIVES.

With this setup, we have defined a linked list structure and operations on it, including pushing new values and iterating over them.

Explanation:

  1. Initialization: The INIT-PARA is called to initialize the list with some values.
  2. Push Operation: The PUSH-PARA adds elements to the linked list. If the list is empty, it initializes the head and tail. Otherwise, it adds the element at the tail’s next pointer.
  3. Print All Elements: The PRINT-PARA iterates over the list and prints each element.
  4. Fibonacci Sequence Generation: The GEN-FIB-PARA generates a Fibonacci sequence up to the given limit and displays the numbers.

Execution and Iterators:

Here, iterators are simulated using loops and COBOL’s native capabilities. While COBOL does not have first-class support for iterator-like constructs, this setup demonstrates a close equivalent using structured programming techniques.

To run this program, simply compile it using a COBOL compiler and execute the compiled binary. You will see the list values and the Fibonacci sequence as output.