Talking With A LISP

Introduced B to the basics of the programming language LISP (or Lisp, as the young whipper-snappers call it). LISP has two main advantages over other programming languages: its syntax is much simpler, and it doesn’t have any expressive limits. B. is most chuffed at the idea that she’s learning the most powerful programming language ever invented. And we did it entirely on paper, without needing to touch the computer.

Plan of attack was this:

  • Started with a simple mathematical expression: 3 + 4 = ___. She answered that quite happily: 7.
  • Digressed (already!) to talk about algebra. She’s managed to pick up (gods know where) the idea of variables, such as x and y, and their use as a place holder like the ___ in the expression above.
  • Asked her to find what x was in x = 3 + 4. No problem.
  • Asked her to find what x was in 7 = 3 x 4. (Note: that’s an x, not a ×. On reflection I should have used a less confusing variable for this step.) With a little hinting she hit upon the idea that variables didn’t have to be things (ie operands, like 3 and 4): they could also be actions (ie operators, like + or – or ×). The light went on!
  • Introduced her to LISP syntax: the equivalent of the above expression would be (+ 3 4) – note the parentheses and the operator-first order.
  • Asked her without explanation to evaluate (+ 1 2 3). She answered correctly (6). Explain that the syntax allows more than two operands.
  • Talked about the difference between evaluating an expression like (+ 7 2) to get an answer (9) and seeing it as a list of three items (a plus sign, a seven and a two).
  • Explained that everything in LISP works like that: a list whose first item tells you what to do with the rest of the items. So, for example, (format t “Hello”) will, when evaluated, print the word “Hello” on the screen. (Digression: she guessed that the t stands for “text”, which is pretty clever but, sadly, wrong; it actually means “true” and is the convention in the format command for outputting the result to the screen instead of a file, but I didn’t go into detail.
  • Asked her to evaluate (+ 3 (- 4 1)). Explained that on the face of it, it was silly: how do you add a number to a list? Pointed out that she can evaluate each item in the “plus list”, including the “minus list”. Showed her how to cross out the (- 4 1) and replace it with 3; from there she worked out the rest and gave the answer: 6.
  • Gave her a very complicated expression, introducing * as the multiplication operator: (+ (- (* 7 1) (- 4 2)) (* 5 2)). Concentration was flagging a little, and the asterisk threw her at first, but she evaluate the (- 4 2) as 2 and the (* 7 1) as 7, meaning that the first operand was 5 (that’s 7 minus 2) and the second was 10 (that’s 5 times 2) so the result was 15 (that’s 5 plus 10). Was justifiably pleased with herself.
  • Gave her a teaser for next time: the defun operator for defining functions. Was very pleased with the idea of defining functions and using what she already knows of algebra.

So: the basics are: the duality of code and data (ie an expression can “mean” its contents or its value, depending on what you decide to do with it); the concept of evaluating a list to get a result, and the very minimal LISP syntax (given a list, use the first item to decide what to do with the rest) and a bit of a view of types (I showed her symbols, like +, and defun, as well as numbers and text). And all without using a computer!

Next time I’ll show her how to write her own functions, and maybe touch on the list operators car, cdr, cons and list as a teaser for lesson 3…

This entry was posted in Uncategorized. Bookmark the permalink.