Here is a short tutorial to get you familiar with “h” and with Herbert.
First, make sure that your system is able to run Herbert with all of its functionality. See help for more information on minimum systems requirements and required security settings.
Next, click here to run a practice version of Herbert.
You can now follow along in the tutorial by both writing programs yourself, and checking them against the examples given, or by copying and pasting the examples from this page into Herbert to see how they work.
In this tutorial we'll learn how to program in "h" by writing a program that makes Herbert go around in a square. We'll start out with a simple solution and then show how procedures and parameters can be used to simplify and/or generalize the solution.
Our first task is to program Herbert to go around in a square 5 units per edge, and then end in the same position and facing the same direction in which he started.
The tutorial consists of a single level (level 0) which is free of buttons and obstructions. At any time during this tutorial, feel free to adjust Herbert's speed by using the scroll bar on the bottom of the Herbert window. To watch a program more carefully, you may halt it at any time by pressing the Halt (Pause) toolbar button. You can also place a vertical bar "|" (a breakpoint) at the point in the program where you want it to stop. Once the program is halted, you can start stepping through the program one command at a time ("Step" in the "Run" menu or on the toolbar), resume execution ("Resume"), or reset Herbert to his starting position ("Reset").
Type the following into the program text area:
sssssrsssssrsssssrsssssr
Now choose "Go" from the "Run" menu. Note that "s" makes Herbert go forward one unit, while "r" makes him turn one-quarter turn to the right. As you might guess, "l" (the letter "ell") would make him turn one-quarter turn to the left. These three commands are the only ones Herbert understands.
Now reset Herbert by pressing the stop button and try:
a:sssssr
aaaa
Here you've defined a procedure called "a", which does "sssssr" whenever called. By calling it four times in a row, this is effectively the same as the first example, but it's a smaller program; 11 bytes versus 24. Any single lowercase letter is legal as a procedure name (except for "s", "r", and "l" which already mean something to Herbert).
Even simpler is:
a:sssssra
a
Here "a" does "sssssr" as before, and then calls itself (which then does "sssssr" and calls itself ...). When a procedure calls itself, this is called "recursion". This example is an infinite recursion; Herbert doesn't stop when he's done with the square. There is no problem with this in itself, since Herbert has an infinite stack, but you can stop him if you want by choosing "Halt" on the "Run" menu. You may wish to limit the depth of the recursion in the program itself if, for instance, you wanted him to do something else after he's gone around in a square. To do this you must use a parameter:
a(A):sssssra(A-1)
a(4)
Parameters are always single uppercase letters. When a procedure with parameters is called, you must supply "arguments" for each parameter (here the argument for "A" is initially 4, then 3, then 2, etc.). The rule with arguments in "h" is that if any numeric argument in a procedure call evaluates to 0 or less, the call is not made.
You can also use a second parameter to vary the size of the square.
a(A,B):f(B)ra(A-1,B)
f(A):sf(A-1)
a(4,5)
Now change the "5" to "8" and see what happens. Study the procedure "f" here (we've used the letter "f" to stand for "forward"). This procedure is a very important one and will appear time and time again in your solutions.
Parameters can be supplied with "procedural" arguments as well as numeric ones. try:
a(A,B,C):f(B)Ca(A-1,B,C)
f(A):sf(A-1)
a(4,5,r)
Then, try changing the "r" to an "l" (ell).
That's all there is to it! For more practice with "h", try the following variations of our square program. What does each one do?
a(A,B,C):f(B)Ca(A-1,B,C)
b(A):a(4,5,r)lb(A-1)
f(A):sf(A-1)
b(4)
a(A,B,C):f(B)Ca(A-1,B,C)
b(A):a(4,A,r)b(A-1)
f(A):sf(A-1)
b(10)
a(A,B,C):f(B)Ca(A-1,B,C)
b(A):a(2,11-A,r)b(A-1)
f(A):sf(A-1)
b(10)
a(A,B,C):f(B)Ca(A-1,B,C)
f(A):sf(A-1)
a(4,5,rslsr)