Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 23e78ee

Browse files
committed
sync with O'Reilly Atlas
1 parent f0f1608 commit 23e78ee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+2087
-124
lines changed

‎02-array-seq/lispy/LICENSE‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2010-2017 Peter Norvig
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

‎02-array-seq/lispy/README.md‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Norvig's originals and updates
2+
3+
This directory contains:
4+
5+
* `original/`:
6+
Norvig's [`lis.py`](https://github.com/norvig/pytudes/blob/c33cd6835a506a57d9fe73e3a8317d49babb13e8/py/lis.py),
7+
[`lispy.py`](https://github.com/norvig/pytudes/blob/c33cd6835a506a57d9fe73e3a8317d49babb13e8/py/lispy.py), and the `lispytest.py` custom test script for testing both;
8+
* `py3.10/`: `lis.py` with type hints, pattern matching, and minor edits—requires Python 3.10.
9+
10+
The `py3.10/` directory also has `lis_test.py` to run with
11+
[pytest](https://docs.pytest.org), including all the
12+
[`lis_tests` suite](https://github.com/norvig/pytudes/blob/60168bce8cdfacf57c92a5b2979f0b2e95367753/py/lispytest.py#L5)
13+
from `original/lispytest.py`,
14+
and additional separate tests for each expression and special form handled by `evaluate`.
15+
16+
17+
## Provenance, Copyright and License
18+
19+
`lis.py` is
20+
[published](https://github.com/norvig/pytudes/blob/c33cd6835a506a57d9fe73e3a8317d49babb13e8/py/lis.py)
21+
in the [norvig/pytudes](https://github.com/norvig/pytudes) repository on Github.
22+
The copyright holder is Peter Norvig and the code is licensed under the
23+
[MIT license](https://github.com/norvig/pytudes/blob/60168bce8cdfacf57c92a5b2979f0b2e95367753/LICENSE).
24+
25+
26+
## Changes to Norvig's code
27+
28+
I made small changes to the programs in `original/`:
29+
30+
* In `lis.py`:
31+
* The `Procedure` class accepts a list of expressions as the `body`, and `__call__` evaluates all those expressions in order, returning the value of the last. This is consistent with Scheme's `lambda` syntax and provided a useful example for pattern matching.
32+
* In the `elif` block for `'lambda'`, I added the `*` in front of the `*body` variable in the tuple unpacking to capture the expressions as a list, before calling the `Procedure` constructor.
33+
34+
* In `lispy.py` I made [changes and a pull request](https://github.com/norvig/pytudes/pull/106) to make it run on Python 3.
35+
36+
_Luciano Ramalho<br/>June 29, 2021_

‎02-array-seq/lispy/original/LICENSE‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2010-2017 Peter Norvig
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
# Source of the originals
3+
4+
* [lis.py](https://raw.githubusercontent.com/norvig/pytudes/705c0a335c1811a203e79587d7d41865cf7f41c7/py/lis.py)
5+
6+
* [lispy.py](https://raw.githubusercontent.com/norvig/pytudes/705c0a335c1811a203e79587d7d41865cf7f41c7/py/lispy.py)
7+
8+
* [lispytest.py](https://raw.githubusercontent.com/norvig/pytudes/705c0a335c1811a203e79587d7d41865cf7f41c7/py/lispytest.py)

‎02-array-seq/lispy/original/lis.py‎

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
################ Lispy: Scheme Interpreter in Python 3.3+
2+
3+
## (c) Peter Norvig, 2010-18; See http://norvig.com/lispy.html
4+
5+
################ Imports and Types
6+
7+
import math
8+
import operator as op
9+
from collections import ChainMap as Environment
10+
11+
Symbol = str # A Lisp Symbol is implemented as a Python str
12+
List = list # A Lisp List is implemented as a Python list
13+
Number = (int, float) # A Lisp Number is implemented as a Python int or float
14+
15+
class Procedure(object):
16+
"A user-defined Scheme procedure."
17+
def __init__(self, parms, body, env):
18+
self.parms, self.body, self.env = parms, body, env
19+
def __call__(self, *args):
20+
env = Environment(dict(zip(self.parms, args)), self.env)
21+
return eval(self.body, env)
22+
23+
################ Global Environment
24+
25+
def standard_env():
26+
"An environment with some Scheme standard procedures."
27+
env = {}
28+
env.update(vars(math)) # sin, cos, sqrt, pi, ...
29+
env.update({
30+
'+':op.add, '-':op.sub, '*':op.mul, '/':op.truediv,
31+
'>':op.gt, '<':op.lt, '>=':op.ge, '<=':op.le, '=':op.eq,
32+
'abs': abs,
33+
'append': op.add,
34+
'apply': lambda proc, args: proc(*args),
35+
'begin': lambda *x: x[-1],
36+
'car': lambda x: x[0],
37+
'cdr': lambda x: x[1:],
38+
'cons': lambda x,y: [x] + y,
39+
'eq?': op.is_,
40+
'equal?': op.eq,
41+
'length': len,
42+
'list': lambda *x: list(x),
43+
'list?': lambda x: isinstance(x,list),
44+
'map': lambda *args: list(map(*args)),
45+
'max': max,
46+
'min': min,
47+
'not': op.not_,
48+
'null?': lambda x: x == [],
49+
'number?': lambda x: isinstance(x, Number),
50+
'procedure?': callable,
51+
'round': round,
52+
'symbol?': lambda x: isinstance(x, Symbol),
53+
})
54+
return env
55+
56+
global_env = standard_env()
57+
58+
################ Parsing: parse, tokenize, and read_from_tokens
59+
60+
def parse(program):
61+
"Read a Scheme expression from a string."
62+
return read_from_tokens(tokenize(program))
63+
64+
def tokenize(s):
65+
"Convert a string into a list of tokens."
66+
return s.replace('(',' ( ').replace(')',' ) ').split()
67+
68+
def read_from_tokens(tokens):
69+
"Read an expression from a sequence of tokens."
70+
if len(tokens) == 0:
71+
raise SyntaxError('unexpected EOF while reading')
72+
token = tokens.pop(0)
73+
if '(' == token:
74+
L = []
75+
while tokens[0] != ')':
76+
L.append(read_from_tokens(tokens))
77+
tokens.pop(0) # pop off ')'
78+
return L
79+
elif ')' == token:
80+
raise SyntaxError('unexpected )')
81+
else:
82+
return atom(token)
83+
84+
def atom(token):
85+
"Numbers become numbers; every other token is a symbol."
86+
try: return int(token)
87+
except ValueError:
88+
try: return float(token)
89+
except ValueError:
90+
return Symbol(token)
91+
92+
################ Interaction: A REPL
93+
94+
def repl(prompt='lis.py> '):
95+
"A prompt-read-eval-print loop."
96+
while True:
97+
val = eval(parse(input(prompt)))
98+
if val is not None:
99+
print(lispstr(val))
100+
101+
def lispstr(exp):
102+
"Convert a Python object back into a Lisp-readable string."
103+
if isinstance(exp, List):
104+
return '(' + ' '.join(map(lispstr, exp)) + ')'
105+
else:
106+
return str(exp)
107+
108+
################ eval
109+
110+
def eval(x, env=global_env):
111+
"Evaluate an expression in an environment."
112+
if isinstance(x, Symbol): # variable reference
113+
return env[x]
114+
elif not isinstance(x, List): # constant literal
115+
return x
116+
elif x[0] == 'quote': # (quote exp)
117+
(_, exp) = x
118+
return exp
119+
elif x[0] == 'if': # (if test conseq alt)
120+
(_, test, conseq, alt) = x
121+
exp = (conseq if eval(test, env) else alt)
122+
return eval(exp, env)
123+
elif x[0] == 'define': # (define var exp)
124+
(_, var, exp) = x
125+
env[var] = eval(exp, env)
126+
elif x[0] == 'lambda': # (lambda (var...) body)
127+
(_, parms, body) = x
128+
return Procedure(parms, body, env)
129+
else: # (proc arg...)
130+
proc = eval(x[0], env)
131+
args = [eval(exp, env) for exp in x[1:]]
132+
return proc(*args)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /