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

Enter

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
2
> (- 5 3)
> (+ "Hello" "World")

Each of the operations from the preious step returns a value,
that can be used to create compound expressions:

1
2
> (+ (- 3 5) 17)
> (+ "Manu" " " "was" " " "here.")

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
2
> (get x)
> (get y)

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
2
> (defun north() (set direction 0))
> (defun east() (set direction 1))

Try doing the same for west and south.
Test your results by moving arrow keys after clicking on the map.