example-code-2e/18-with-match/lispy/py3.9
2021-10-12 22:37:28 -03:00
..
examples_test.py sync from Atlas 2021-10-12 22:37:28 -03:00
lis_test.py sync from Atlas 2021-09-20 10:37:26 -03:00
lis.py sync from Atlas 2021-10-12 22:37:28 -03:00
README.md moved lispy from 02 to 18 2021-09-15 22:48:08 -03:00

Changes from the original

While adapting Peter Norvigs lis.py for use in Fluent Python, Second Edition, I made a few changes for didactic reasons.

Luciano Ramalho

Major changes

  • Make the lambda form accept more than one expression as the body. This is consistent with Scheme syntax, and provides a useful example for the book. To implement this:
    • In Procedure.__call__: evaluate self.body as a list of expressions, instead of a single expression. Return the value of the last expression.
    • In evaluate(): when processing lambda, unpack expression into (_, parms, *body), to accept a list of expressions as the body.
  • Remove the global_env global dict. It is only used as a default value for the env parameter in evaluate(), but it is unsafe to use mutable data structures as parameter default values. To implement this:
    • In repl(): create local variable global_env and pass it as the env paramater of evaluate().
    • In evaluate(), remove global_env default value for env.
  • Rewrite the custom test script lispytest.py as lis_test.py: a standard pytest test suite including new test cases, preserving all Norvigs test cases for lis.py but removing the test cases for the features implemented only in lispy.py.

Minor changes

Cosmetic changes to make the code look more familiar to Python programmers, the audience of Fluent Python.

  • Rename eval() to evaluate(), to avoid confusion with Pythons eval built-in function.
  • Refer to the list class as list instead of aliasing as List, to avoid confusion with typing.List which is often imported as List.
  • Import collections.ChainMap as ChainMap instead of Environment.