{ "cells": [ { "cell_type": "markdown", "id": "828d21f7-0a3b-4024-b3d5-306ea56a3214", "metadata": {}, "source": [ "
| Java | \n", "Lisp | \n", "
|---|---|
\n",
"\n",
" | \n",
" \n",
"\n",
" | \n",
"
| \n",
"pi: 3.141592653589793\n",
" *: <built-in function mul>\n", " ...\n", " \n", "
|
" ] }, { "cell_type": "code", "execution_count": 21, "id": "5bcc6a7f-261b-4156-92d3-c771ecfcd109", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000\n", "CPU times: user 395 μs, sys: 200 μs, total: 595 μs\n", "Wall time: 521 μs\n" ] } ], "source": [ "%%time\n", "batch(\"\"\"\n", "(begin \n", " (define (factorial n) \n", " (if (<= n 1) \n", " 1 \n", " (* n (factorial (- n 1)))))\n", " (factorial 100))\"\"\")" ] }, { "cell_type": "markdown", "id": "802789ab-2f1c-4fb0-a79b-c7621a851e81", "metadata": {}, "source": [ "## More Example Lisp Programs" ] }, { "cell_type": "code", "execution_count": 22, "id": "f8728be6-2423-487f-a44a-49a50411d363", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(count 3 4)\n" ] } ], "source": [ "batch(\"\"\"\n", "(list \n", " (define (count item L) \n", " (if (null? L)\n", " 0\n", " (+ (equal? item (first L)) (count item (rest L)))))\n", " (count 0 (list 0 1 2 3 0 0))\n", " (count (quote the) (quote (the more the merrier the bigger the better))))\"\"\")" ] }, { "cell_type": "code", "execution_count": 23, "id": "af58a444-c684-481c-a74b-1ad6bb49459b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(square compose repeat 10 81 65536 2.0)\n" ] } ], "source": [ "batch(\"\"\"\n", "(list\n", " (define (square x) (* x x))\n", " (define (compose f g) (lambda (x) (f (g x))))\n", " (define (repeat f) (compose f f))\n", " ((compose round sqrt) 101)\n", " ((repeat square) 3)\n", " ((repeat (repeat square)) 2)\n", " ((repeat (repeat sqrt)) (pow 2 16)))\"\"\")" ] }, { "cell_type": "code", "execution_count": 24, "id": "5dba7073-33d3-4f39-a192-5e0ccdc42f8a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765)\n" ] } ], "source": [ "batch(\"\"\"\n", "(begin\n", " (define (fib n) \n", " (if (< n 2) \n", " 1 \n", " (+ (fib (- n 1)) (fib (- n 2)))))\n", " (define (range start stop)\n", " (if (= start stop) \n", " (quote ()) \n", " (cons start (range (+ start 1) stop))))\n", " (map fib (range 0 20)))\"\"\")" ] }, { "cell_type": "markdown", "id": "5f7fcfb8-ef93-4dfb-af8b-43030ab74eaa", "metadata": {}, "source": [ "## True Story\n", "\n", "To back up the idea that it can be helpful to know how\n", "interpreters work, here's a story. Way back in 1984 I was writing a\n", "Ph.D. thesis. This was before LaTeX, before Microsoft Word for Windows–we used\n", "[troff](https://en.wikipedia.org/wiki/Troff). Unfortunately, troff had no facility for forward references\n", "to symbolic labels: I wanted to be able to write \"As we will see on\n", "page @theorem-x\" and then write something like \"@(set theorem-x \\n%)\" in\n", "the appropriate place (the troff register \\n% holds the page number). My\n", "fellow grad student Tony DeRose felt the same need, and together we\n", "sketched out a simple Lisp program that would handle this as a preprocessor. However,\n", "it turned out that the Lisp we had at the time was good at reading\n", "Lisp expressions, but slow at reading 100 KB of characters one character at a time.\n", "\n", "From there Tony and I split paths. He reasoned that the hard part was\n", "the interpreter for expressions; he needed Lisp for that, but he knew\n", "how to write a tiny C routine\n", "for reading the characters one at a time, and how to link it into the Lisp\n", "program. I didn't know how to do that linking, but I reasoned that writing an\n", "interpreter for this trivial language (all it had was set variable,\n", "fetch variable, and string concatenate) was easy, so I wrote an\n", "interpreter in C. So, ironically, Tony wrote a Lisp program (with one small routine in C) because he was a\n", "C programmer, and I wrote a C program (that implements a hand-coded mini-interpreter) because I was a Lisp programmer.\n", "\n", "In the end, we both got our theses done (Tony, Peter).\n", "\n", "