Find Your REPL!
2018-01-09T18:46:27.418Z
The Dreamer’s Ink
So, you want to learn to build something in a new programming language? One of the most important tools you can give yourself in the context of learning is a responsive environment. In programming this has been devised a few different ways, but the simplest of them is a REPL.
What is a REPL you ask?
It is an environment which allows you to interact with code on a line by line basis. In fact, REPL is an acronym for Read-Eval-Print-Loop which is a pretty literal statement of how interactivity is achieved. It can also be called an interpreter or a shell.
- Read — Takes a line of code and assigns it to memory
- Eval — Evaluates the line of code at that memory address
- Print — Displays the result of that evaluation
- Loop — Repeats the process
Written like this it looks almost too simple, and yet the pattern matches the way we interact with the world in general, and hence gives us the extremely valuable ability to interact with our machine. With this pattern we can now act on the computer by providing it with code and the computer can act on us by providing a response that we can perceive and parse.
In Paul Graham’s classic Hackers and Painters essay one of his main points is that humans are designed to learn by doing, so closing the gap between learning and doing helps us both learn and do faster. His premise sums up as “If you can’t quickly and easily sketch a rough image, you’re not going to paint a masterpiece.” You could take that all the way to basics and say no one paints a wall in the dark. The example of the painter with her brush and canvas is an evocative one, but really the pattern abstracts to all learning. Throwing a ball, writing a play, trading stocks and even flying fighter plane can be broken down into loops that take an action and return a response. The concept of intelligent agents in AI is also predicated on the same idea, pushing on the limits of generalization and abstraction.
Originally programming had no such pattern built into its design. It was not until Lisp was designed by John McCarthy in 1958 that real-time interpretation became available as a way to interact with machines. Before that everything was done through compilers which require an entire codebase to be built from human readable instructions into machine readable ones at once before returning the result of all of the instructions as one monolithic response.
While this did provide a way to act on a machine and have it act on you in turn, the loop was so slow that it effectively killed a large part of what makes human learning effective (and fun.) It’s a problem that has haunted programming since its earliest days and contributed heavily to the perception that it is a difficult, arcane art. Without feedback even the smartest and most driven creators have trouble keeping track of their process, to say nothing of their motivation.
At this point the programming world is awash with REPL’s and related software which can help us to quickly and easily explore new code. There are now interpreters for every language. There are online REPL’s which can interpret code in the browser. There are document based interpreters which can evaluate code inside of a formatted document and display the results. Even compiled languages like Java and C can be interactive through virtual machines that provide interpreters on top of the compiler.
If we return to our definition we can uncover a healthy flexibility in the way the pattern can be implemented. Below are a few questions we can ask when looking at given interpreter to understand how it can be used by discovering how it acts.
- Read — What types of statement can it read? Where does it store them?
— For example some interpreters can only read one line of code at a time, while others like the javascript console in developer tools allow multiple lines to be entered and edited at once before sending them for evaluation. Some languages allow a statement to be redefined after being evaluated by overwriting a new definition to the same name, others do not.
- Evaluate — How does it evaluate the statement? What does it do with the result?
— Some interpreters allow a language version to be specified while others are predefined. Some will store the result in a history that can be pulled up and searched while others only show the result in one place. Some can be configured to allow external libraries to be available. Some have caches that are easy to clear while others require the entire interpreter to be restarted to clear the working environment.
- Print — What format does it print the result in? How much information does it display?
— Some will print out the return value of the function in a prettified or simplified way to encourage rapid comprehension. Some also show the memory location, the line from which it was generated or even a link to the line for easy reference.
- Loop — How fast does it bring you back to the Read step? What does it bring with you when it returns you to the Read step?
— Most will simply print the result and immediately return your cursor to the input line, but there are some interesting variations. There are now inline interpreters which work within a document and will output the results of the code to the document you have written them in and then put you on a new line in the document, allowing for documentation and code demonstration to be melded into a cohesive presentation format.
Together these different pieces of functionality combine to create very different canvases on which to learn and create. It is a blessing we should be aware of as modern developers finding the best way to build and adapt in our rapidly evolving programming landscape. As recently as 2006 Bret Victor’s frustration was palpable enough for him to produce the Magic Ink essay which pointed the way towards much of what has been built recently (online interpreters, document based interpreters) and pointed towards even tighter feedback loops that could be built to decrease the distance between our doing and our learning.
Although manipulation is the focus, good manipulation software must provide superb visualization as well. This establishes the feedback loop that is critical for all creative activity — the manipulator must see the effects of her manipulation. Thus, manipulation software design is also a significant graphic design challenge.
Happy coding and go find your REPL!