Range Over Iterators in Elm

Based on the provided instruction and the Go code example, here’s the corresponding Python translation presented in Markdown format suitable for Hugo:

Our first program will print the classic “hello world” message. Here’s the full source code.

class Element:
    def __init__(self, val=None):
        self.val = val
        self.next = None

class List:
    def __init__(self):
        self.head = None
        self.tail = None

    def push(self, val):
        if not self.tail:
            self.head = Element(val)
            self.tail = self.head
        else:
            self.tail.next = Element(val)
            self.tail = self.tail.next

    def all(self):
        current = self.head
        while current:
            yield current.val
            current = current.next

def gen_fib():
    a, b = 1, 1
    while True:
        yield a
        a, b = b, a + b

def main():
    lst = List()
    lst.push(10)
    lst.push(13)
    lst.push(23)

    print("List elements:")
    for e in lst.all():
        print(e)

    all_elements = list(lst.all())
    print("All elements collected:", all_elements)

    print("First few Fibonacci numbers:")
    fib_generator = gen_fib()
    for _ in range(10):
        n = next(fib_generator)
        if n >= 10:
            break
        print(n)

if __name__ == "__main__":
    main()

To run the program, save the code in a file named program.py and use python to execute it.

$ python program.py

Here’s what the execution will look like:

List elements:
10
13
23
All elements collected: [10, 13, 23]
First few Fibonacci numbers:
1
1
2
3
5
8

Now that we can run and build basic Python programs, let’s learn more about the language.