Background
Visualizing λ-calculus terms
Famous lambda-juggler (and code golfer) John Tromp devised an interesting visualization of terms in the λ-calculus. In his words:
abstractions (lambdas) are represented by horizontal lines, variables by vertical lines emanating down from their binding lambda, and applications by horizontal links connecting the leftmost variables.
For example, the lambda term λf.λx.f (f (f (f x))) corresponds to the visualization:
-------------------
| | | |
-------------------
| | | | |
| | | |----
| | |----
| |----
|----
|
Read it from top to bottom:
- The first horizontal line represents the first λ.
- The four lines descending from it represent the fs in the body.
- Similarly, the second horizontal line represents the second λ, and the single new line descending from it represents the x in the body.
- The rightmost f line and the x line are connected by a horizontal line representing an application (f x).
- The next application is (f (f x)), et cetera.
Church numerals
The Church numerals are a specific sequence of terms in the λ-calculus, taking on the following pattern:
0 = λf. λx. x
1 = λf. λx. f x
2 = λf. λx. f (f x)
3 = λf. λx. f (f (f x))
...
Task
Given an input number n, print some ASCII art that visualizes the nth Church numeral. For instance, the example above is your target output when given n = 4. For n = 0, print:
---
---
|
|
Test cases
Your answer must output exactly the same text (modulo trailing newlines) as this stack snippet for all integer inputs n ≥ 0:
function visualize() {
var vin = document.getElementById('visualize-in');
var vout = document.getElementById('visualize-out');
var n = Number(vin.value);
if (n < 0) n = 0;
var line = '-'.repeat(4 * n + 3);
var bars = function(k) { return ' |'.repeat(k).substr(2); };
var img = [line, bars(n), line, bars(n + 1)];
for (var i = n; i > 0; --i) img.push(bars(i) + '----');
vout.innerHTML = img.join('\n') + '\n |';
}
<label for="visualize-in">n <input style="width:50px;" type="number" id="visualize-in" name="visualize-in" onchange="visualize()"/></label>
<pre style="background-color: #eff0f1" id="visualize-out"></pre>
This is code-golf, so the shortest code in bytes wins.
.repeat
. \$\endgroup\$