On this page:
7.1 What is This Assignment’s Purpose?
7.2 Reading
7.3 Task
7 Control
7.1 What is This Assignment’s Purpose?

Modern languages now have interesting forms of control that go beyond traditional call-and-return, such as pausing the computation and being able to resume it. They make programs much more readable, elegant, and even performant.

Yet you may or may not have been exposed to these features before, and even if you have been, you may not have thought about them systematically. We want you to learn enough about these to compare and contrast them, so you have them in your toolbox when you program in the future.

7.2 Reading

The modern control toolkit includes generators, iterators, streams, asynchronous functions, goroutines, and more.

To understand the ideas that motivate them, please carefully read this article by Russ Cox, one of the members of the Go programming language team. You do not need to read the whole article. Please read through the section entitled “Coroutines, Threads, and Generators”. You can stop at “Why Coroutines in Go?”, but you are of course welcome to read on if you’re interested!

7.3 Task

Consider the working example in Cox’s article, which is to compare two trees for equality by ignoring their tree structure and only examining the value sequence.

Before we proceed, you might wonder why one can’t just flatten the two trees and compare them. You certainly can, and the result would be equivalent to what his implementations provide. However: that implementation would both (a) produce a data structure proportional to the number of leaves, whereas this produces no intermediate data structure; and (b) have to traverse the entire tree even if they are not equal, whereas this short-circuits the moment it finds a difference. Thus, this approach saves both space and time.

Your task is to implement this approach using asynchronous functions in JavaScript. If you believe it’s impossible to do, please tell us your argument why.

You may use any Web resources you wish (within the rules set down in the syllabus). Be sure to credit them appropriately. But beware that many Web sites are written by authors who are underinformed or even wrong, so don’t just take them at their word! Trust but verify.