Add files via upload

This commit is contained in:
Peter Norvig 2017-11-21 22:13:27 -08:00 committed by GitHub
parent 5d0223c63e
commit 6c51ac830d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -37,7 +37,7 @@
"\n", "\n",
"# The Concordance Solution\n", "# The Concordance Solution\n",
"\n", "\n",
"I ended up with a program similar to the following, but to avoid having to explain Snobol, here I show Python syntax (except for the `'$word'` indirection):" "I ended up with a program similar to the following (translated from Snobol to Python augmented with `'$word'` indirection):"
] ]
}, },
{ {
@ -51,7 +51,7 @@
"program = \"\"\"\n", "program = \"\"\"\n",
"for i, line in enumerate(input):\n", "for i, line in enumerate(input):\n",
" for word in re.findall(r\"\\w+\", line.upper()):\n", " for word in re.findall(r\"\\w+\", line.upper()):\n",
" $word += str(i) + \", \"\n", " $word += str(i) + ', '\n",
"\"\"\"" "\"\"\""
] ]
}, },
@ -61,7 +61,7 @@
"source": [ "source": [
"That's just 3 lines, not 40 to 60! \n", "That's just 3 lines, not 40 to 60! \n",
"\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 noticed about Snobol:\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 noticed about the Snobol interpreter:\n",
"\n", "\n",
"* `$word` gets translated as `_context[word]`.\n", "* `$word` gets translated as `_context[word]`.\n",
"* It calls `exec(program, _context)`, where `_context` is a `defaultdict(str)`, so variables default to `''`.\n", "* It calls `exec(program, _context)`, where `_context` is a `defaultdict(str)`, so variables default to `''`.\n",
@ -224,7 +224,7 @@
"program = \"\"\"\n", "program = \"\"\"\n",
"for i, line in enumerate(input):\n", "for i, line in enumerate(input):\n",
" for word in re.findall(r\"\\w+\", line.upper()):\n", " for word in re.findall(r\"\\w+\", line.upper()):\n",
" $word += str(i) + \", \"\n", " $word += str(i) + ', '\n",
"del i, line, word\n", "del i, line, word\n",
"\"\"\"\n", "\"\"\"\n",
"\n", "\n",