From 8c8f0cef1a2b3d4d2725f29574461e02ca3fd534 Mon Sep 17 00:00:00 2001 From: Peter Norvig Date: Wed, 8 Jul 2026 10:29:31 -0700 Subject: [PATCH] Add files via upload --- ipynb/Snobol.ipynb | 120 +++++++++++++++++++++++---------------------- 1 file changed, 61 insertions(+), 59 deletions(-) diff --git a/ipynb/Snobol.ipynb b/ipynb/Snobol.ipynb index f89255f..d655326 100644 --- a/ipynb/Snobol.ipynb +++ b/ipynb/Snobol.ipynb @@ -4,7 +4,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "
Peter Norvig, Nov. 2017
\n", + "
Peter Norvig
Nov. 2017
\n", "\n", "# Bad Grade, Good Experience\n", "\n", @@ -12,11 +12,11 @@ "\n", "> *As a student, did you ever get a bad grade on a programming assignment?* \n", "\n", - "I've forgotten most of my assignments, but there is one I do remember. It was something like this:\n", + "There is one that I do remember. It was in an introductory programming class where they wanted the students to get experience in a different language. It went something like this:\n", "\n", "# The Concordance Assignment\n", "\n", - "> *Using the [`Snobol`](http://www.snobol4.org/) language, read lines of text from the standard input and print a **concordance**, which is an alphabetized list of words in the text, with the line number(s) where each word appears. Words with different capitalization (like \"A\" and \"a\") should be merged into one entry.*\n", + "> *Using the [Snobol](http://www.snobol4.org/) language, read lines of text from the standard input and print a **concordance**, which is an alphabetized list of words in the text, with the line number(s) where each word appears. Words with different capitalization (like \"A\" and \"a\") should be merged into one entry.*\n", "\n", "After studying Snobol a bit, I realized that the expected solution was along these lines:\n", "\n", @@ -28,24 +28,23 @@ "\n", "That would be around 40 to 60 lines of code; an easy task. But I noticed three interesting things about Snobol:\n", "\n", - "* '`$`' is an *indirection* operator, so if the variable `'word'` has the value `\"A\"`, then `'$word = x'` is the same as `'A = x'`.\n", - "* Uninitialized variables are treated as the empty string, so `'A = A + \"text\"'` works even if we haven't seen `'A'` before.\n", - "* When the program ends, the Snobol interpreter \n", + "- '`$`' is an *indirection* operator, so if the variable `'word'` has the value `\"A\"`, then `'$word = x'` is the same as `'A = x'`.\n", + "- Uninitialized variables are treated as the empty string, and coercion to string (e.g. from int) is automatic.\n", + " - (For example, `A = A + 1 + \", \"'` sets `A` to `\"1, \"` if `A` was initially undefined.)\n", + "- When the program ends, the Snobol interpreter \n", "prints out each variable (in sorted order), with its value, as a debugging aid.\n", "\n", "That means I could use `$` to do away with the hash table and array data structures, eliminating steps 1, 3, 4, and 5, and just do step 2! \n", "\n", "# The Concordance Solution\n", "\n", - "I ended up with a program similar to the following (translated from Snobol to Python, but with `'$word'` indirection):" + "I ended up with a program similar to the following (translated from Snobol to Python, but using `'$word'` indirection):" ] }, { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "program = \"\"\"\n", @@ -61,12 +60,13 @@ "source": [ "That's just 3 lines, not 40 to 60! \n", "\n", - "To test the program, I'll write a mock Snobol/Python interpreter, which at heart is just a call to the Python interpreter, `exec(program)`, except that it handles the three things I mentioned about the Snobol interpreter, plus one more:\n", + "# The Snobol/Python Interpreter\n", + "\n", + "To test the program, I'll write a mock Snobol/Python interpreter, which calls the Python interpreter, `exec(program, _globals)`, except that it handles the three things I mentioned about the Snobol interpreter:\n", "\n", "1. `$word` gets translated as `_globals[word]`.\n", - "2. The interpreter calls `exec(program, _globals)`, where `_globals` is a `defaultdict` that makes variables default to the empty string.\n", - "3. After the `exec` completes, the user-defined variables (but not the built-in ones) are printed.\n", - "4. Concatenating a string with an integer coerces the `int` to `str` automatically. I'll handle that with a `Str` class.\n" + "2. Undefined variables default to an empty `Str` (a class that coerces for concatenation).\n", + "4. After the `exec` completes, the user-defined variables (but not the built-in ones) are printed.\n" ] }, { @@ -79,23 +79,25 @@ "import re\n", "\n", "def snobol(program, data=''):\n", - " \"\"\"A Python interpreter with four Snobol-ish features:\n", - " 1. $word indirection; 2. variables default to empty string; \n", - " 3. post-mortem dump; 4. automatic coercing to string\"\"\"\n", - " program = re.sub(r'\\$(\\w+)', r'_globals[\\1]', program) # 1. \n", - " _globals = defaultdict(Str, vars(__builtins__)) # 4., 2.\n", - " _globals.update(re=re, input=data.splitlines(), _globals=_globals)\n", + " \"\"\"A Python interpreter with three Snobol-ish features:\n", + " • $word indirection; \n", + " • variables default to empty string and concatenation coerces to string\n", + " • post-mortem dump of all user-defined variables\"\"\"\n", + " program = re.sub(r'\\$(\\w+)', r'_globals[\\1]', program) # $ is indirection\n", + " _globals = defaultdict(Str) # default var is empty Str\n", + " _globals.update(vars(__builtins__), _globals=_globals, # load builtin functions\n", + " input=data.splitlines(), re=re)\n", " builtins = set(_globals) | {'__builtins__'}\n", " try:\n", " exec(program, _globals)\n", - " finally:\n", - " print('-' * 79) # 3. \n", + " finally: # post-mortem dump\n", + " print('-' * 79) \n", " for name in sorted(_globals):\n", " if name not in builtins:\n", " print('{:10} = {}'.format(name, _globals[name]))\n", " \n", "class Str(str):\n", - " \"String class with automatic coercion for +\"\n", + " \"\"\"String subclass with automatic coercion for concatenation with +\"\"\"\n", " def __add__(self, other): return Str(str(self) + str(other))\n", " def __radd__(self, other): return Str(str(other) + str(self))\n" ] @@ -104,7 +106,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Now we can run the program on some data:" + "Now we can define some data and run the program on it:" ] }, { @@ -113,7 +115,7 @@ "metadata": {}, "outputs": [], "source": [ - "data = \"\"\"\n", + "data = \"\"\"\\\n", "There she was just a-walkin' down the street, \n", "Singin' \"Do wah diddy diddy dum diddy do\"\n", "Snappin' her fingers and shufflin' her feet, \n", @@ -135,35 +137,35 @@ "output_type": "stream", "text": [ "-------------------------------------------------------------------------------\n", - "A = 1, \n", - "AND = 3, 8, \n", - "DIDDY = 2, 2, 2, 4, 4, 4, \n", - "DO = 2, 2, 4, 4, \n", - "DOWN = 1, \n", - "DUM = 2, 4, \n", - "FEET = 3, \n", - "FINE = 6, 6, 7, \n", - "FINGERS = 3, \n", - "GOOD = 5, 5, 7, \n", - "HER = 3, 3, \n", - "I = 8, \n", - "JUST = 1, \n", - "LOOKED = 5, 5, 6, 6, 7, 7, \n", - "LOST = 8, \n", - "MIND = 8, \n", - "MY = 8, \n", - "NEARLY = 8, \n", - "SHE = 1, 5, 6, 7, 7, \n", - "SHUFFLIN = 3, \n", - "SINGIN = 2, 4, \n", - "SNAPPIN = 3, \n", - "STREET = 1, \n", - "THE = 1, \n", - "THERE = 1, \n", - "WAH = 2, 4, \n", - "WALKIN = 1, \n", - "WAS = 1, \n", - "i = 8\n", + "A = 0, \n", + "AND = 2, 7, \n", + "DIDDY = 1, 1, 1, 3, 3, 3, \n", + "DO = 1, 1, 3, 3, \n", + "DOWN = 0, \n", + "DUM = 1, 3, \n", + "FEET = 2, \n", + "FINE = 5, 5, 6, \n", + "FINGERS = 2, \n", + "GOOD = 4, 4, 6, \n", + "HER = 2, 2, \n", + "I = 7, \n", + "JUST = 0, \n", + "LOOKED = 4, 4, 5, 5, 6, 6, \n", + "LOST = 7, \n", + "MIND = 7, \n", + "MY = 7, \n", + "NEARLY = 7, \n", + "SHE = 0, 4, 5, 6, 6, \n", + "SHUFFLIN = 2, \n", + "SINGIN = 1, 3, \n", + "SNAPPIN = 2, \n", + "STREET = 0, \n", + "THE = 0, \n", + "THERE = 0, \n", + "WAH = 1, 3, \n", + "WALKIN = 0, \n", + "WAS = 0, \n", + "i = 7\n", "line = And I nearly lost my mind\n", "word = MIND\n" ] @@ -223,7 +225,7 @@ ], "source": [ "program = \"\"\"\n", - "for i, line in enumerate(input):\n", + "for i, line in enumerate(input, 1):\n", " for word in re.findall(\"[A-Z]+\", line.upper()):\n", " $word = $word + i + \", \"\n", "del i, line, word\n", @@ -247,7 +249,7 @@ "# TFW you flunk AI\n", "\n", "Here's another example that I had completely forgotten about until 2016, when I was cleaning out a filing cabinet and came across my old college transcript. It turns out that *I flunked an AI course!* (Or at least, didn't complete it.) This course was offered by Prof. Richard Millward in the Cognitive Science program. I certainly remember a lot of influential material from this class: we read David Marr, we read Winston's just-published *Psychology of Computer Vision*, we read a chapter from Duda and Hart which was then only a few years old. The things I learned in that course have stuck with me for decades, but one thing that didn't stick is that, according to my transcript, I never completed the course! I'm not sure what happened. I did an independent study with Ulf Grenander that semester; my best guess\n", - "is that when I started doing the independent study that would have put me over some limit, and so I had to drop the AI course. \n", + "is that when I started doing the independent study that would have put me over some limit for number of credits taken, and so I had to drop the AI course. \n", "\n", "So in both the concordance program and the Cognitive Science AI class, I had a great experience and I learned a lot, even if it wasn't well-reflected in official credit. The moral is: look for the good experiences, and don't worry about the official credit.\n" ] @@ -255,7 +257,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -269,9 +271,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.3" + "version": "3.13.9" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 }