Teaching programming concepts with a new language: P-Lisp
I once co-wrote a Lisp interpreter that ran entirely in the browser, as part of an exercise in allowing folks to appreciate programmatic control in an easy, syntactically-forgiving environment.
The following is a tutorial for the original interpreter, parts of which have been lost to the ravages of obsolescence.
Demo of browser REPL for P-Lisp
Script for student
In this tutorial, you will come to learn the basics of programming constructs, covering creating and manipulating numbers, text, and computer graphics using a custom programming language, âP-Lispâ
By learning to write basic âfunctionsâ â groups of programming instructions â
youâll learn how different abstractions of instructions can be used for high
and low-level instructions to computers.
Start by entering a simple number at the prompt, and observe that
the value is printed back to the console. This doesnât do much to the computer,
it doesnât store â5â in anywhere permanent; it simply shows you the interactivity at
your control today.
1 | > 5 |
The other type of data commonly used is text, which you can enter
within quotation marks to let the computer understand it as raw text, which is data, and not as âcodeâ or instructions for it to try doing anything with:
1 | > "The quick brown fox" |
Next, try printing some text using the âprintâ command.
Commands are issued within parentheses, like so:
1 | > (print "Hello world") |
Note that the word âprintâ is separate from the words âHello Worldâ in how the computer
expects to treat them. print and () are code, while âHello Worldâ is data.
Basic arithmetic can be applied in interesting ways on numbers as well as on text:
1 | > (- 5 3) |
Each of the operations from the preious step returns a value,
that can be used to create compound expressions:
1 | > (+ (- 3 5) 17) |
You can create intermediate values and save it to memory for reference at a future point.
Hereâs how you can get that value:
1 | > (set name "Manu") |
Once saved to memory, it will last as long as your program does.
Try retrieving the value you saved using the âgetâ command:
1 | > (get name) |
Likewise, you can find the coordinates of the dot on the map by
retrieving the values âxâ and âyâ:
1 | > (get x) |
To move the line of motion for the dot, try setting its âxâ value to
an arbitrary coordinate:
1 | > (set x 200) |
Likewise, you can set the direction of movement by setting the variable âdirectionâ.
{0,1,2,3} map to {âGo northâ, âGo eastâ, âGo southâ, âGo westâ} for this program
1 | > (set direction 2) |
Finally, we will try to define âfunctionsâ or mini-programs to do this for us using the âdefunâ method
1 | > (defun north() (set direction 0)) |
Try doing the same for west and south.
Test your results by moving arrow keys after clicking on the map.