How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the E programming language

Published on 12 May 2024 09:40 PM
#E

How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the E programming language

Table of Contents

Problem Statement

Loop over multiple arrays   (or lists or tuples or whatever they're called in your language)   and display the   i th   element of each. Use your language's   "for each"   loop if it has one, otherwise iterate through the collection in order with some other loop.

For this example, loop over the arrays: to produce the output:

If possible, also describe what happens when the arrays are of different lengths.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the E programming language

Source code in the e programming language

def a1 := ["a","b","c"]
def a2 := ["A","B","C"]
def a3 := ["1","2","3"]

for i => v1 in a1 {
    println(v1, a2[i], a3[i])
}

for [v1, v2, v3] in zip(a1, a2, a3) {
    println(v1, v2, v3)
}

def zip {
  to run(l1, l2) {
    def zipped {
      to iterate(f) {
        for i in int >= 0 {
          f(i, [l1.fetch(i, fn { return }),
                l2.fetch(i, fn { return })])
        }
      }
    }
    return zipped
  }

  match [`run`, lists] {
    def zipped {
      to iterate(f) {
        for i in int >= 0 {
          var tuple := []
          for l in lists {
            tuple with= l.fetch(i, fn { return })
          }
          f(i, tuple)
        }
      }
    }
    zipped
  }
}

  

You may also check:How to resolve the algorithm Search a list step by step in the E programming language
You may also check:How to resolve the algorithm Loops/Do-while step by step in the E programming language
You may also check:How to resolve the algorithm Greatest subsequential sum step by step in the E programming language
You may also check:How to resolve the algorithm Copy a string step by step in the E programming language
You may also check:How to resolve the algorithm Real constants and functions step by step in the E programming language