├── .gitignore
├── LICENSE
├── README.md
├── calculator
├── README.md
├── calc.py
├── calc_test.py
└── mypy.ini
├── conftest.py
├── mylis
├── README.md
├── mylis_1
│ ├── examples_test.py
│ ├── exceptions.py
│ ├── lis.py
│ ├── lis_test.py
│ ├── meta_test.py
│ ├── mylis.py
│ ├── mylis_test.py
│ ├── mypy.ini
│ └── requirements.txt
├── mylis_1_closure
│ ├── examples_test.py
│ ├── exceptions.py
│ ├── lis.py
│ ├── lis_test.py
│ ├── meta_test.py
│ ├── mylis.py
│ ├── mylis_test.py
│ ├── mypy.ini
│ └── requirements.txt
├── mylis_2
│ ├── .gitignore
│ ├── closures_demo_test.py
│ ├── examples
│ │ ├── count-change.scm
│ │ ├── fact-iter.scm
│ │ ├── fact.scm
│ │ ├── fibo-n.scm
│ │ ├── fibo-rec.scm
│ │ ├── fibo-seq.scm
│ │ ├── member.scm
│ │ ├── newton.scm
│ │ ├── prime.scm
│ │ └── sumints.scm
│ ├── examples_test.py
│ ├── exceptions.py
│ ├── exercises
│ │ ├── README.md
│ │ ├── ackermann.py
│ │ └── fibonacci.py
│ ├── lis.py
│ ├── lis_test.py
│ ├── metacircular.rkt
│ ├── metacircular.scm
│ ├── metacircular_test.py
│ ├── metacircular_test.rkt
│ ├── mylis.py
│ ├── mylis_test.py
│ ├── mypy.ini
│ └── requirements.txt
└── mylis_3
│ ├── README.md
│ ├── __init__.py
│ ├── environ.py
│ ├── environ_test.py
│ ├── evaluator.py
│ ├── evaluator_test.py
│ ├── examples
│ ├── fact-iter.scm
│ └── fact.scm
│ ├── mylis.py
│ ├── mylis_test.py
│ ├── mypy.ini
│ ├── mytypes.py
│ ├── parser.py
│ ├── parser_test.py
│ └── requirements.txt
├── original
├── LICENSE
├── README.md
├── norvig
│ ├── lis.py
│ ├── lispy.py
│ ├── lispytest.py
│ └── py2.7
│ │ ├── README.md
│ │ └── lis.py
├── py3.10
│ ├── README.md
│ ├── examples_test.py
│ ├── lis.py
│ ├── lis_test.py
│ ├── meta_test.py
│ └── mypy.ini
└── py3.9
│ ├── README.md
│ ├── lis.py
│ ├── lis_test.py
│ └── mypy.ini
├── pytest.ini
├── references
├── LiSP-2ndEdition-2006Dec11
│ ├── ChangeLog
│ ├── Makefile
│ ├── README
│ ├── bigloo
│ │ ├── book.bgl
│ │ ├── hack.bgl
│ │ ├── others
│ │ │ ├── ReadMe
│ │ │ ├── compat.ss
│ │ │ ├── expand.bb
│ │ │ ├── format.scm
│ │ │ ├── hooks.bgl
│ │ │ ├── init.ss
│ │ │ ├── macro-defs.bb
│ │ │ ├── output.ss
│ │ │ ├── pp.scm
│ │ │ └── syntax.tst
│ │ ├── rtbook+.bgl
│ │ └── rtbook.bgl
│ ├── elk
│ │ ├── book.elk
│ │ └── others
│ │ │ ├── compat.ss
│ │ │ ├── expand.bb
│ │ │ ├── hooks.elk
│ │ │ ├── init.ss
│ │ │ ├── macro-defs.bb
│ │ │ └── output.ss
│ ├── gambit
│ │ ├── book.scm
│ │ ├── compat.ss
│ │ ├── expand.bb
│ │ ├── format.scm
│ │ ├── hooks.gsi
│ │ ├── init.ss
│ │ ├── macro-defs.bb
│ │ ├── output.ss
│ │ └── pp.scm
│ ├── meroonet
│ │ ├── Imakefile
│ │ ├── Notice
│ │ ├── meroonet.scm
│ │ ├── oo-tests.scm
│ │ ├── oo-tmp.scm
│ │ ├── variante1.scm
│ │ ├── variante2.scm
│ │ ├── variante3.scm
│ │ ├── variante4.scm
│ │ └── variante5.scm
│ ├── mitscheme
│ │ ├── book.mit
│ │ └── others
│ │ │ ├── compat.ss
│ │ │ ├── expand.sc
│ │ │ ├── hooks.mit
│ │ │ ├── init.ss
│ │ │ ├── macro-defs.sc
│ │ │ └── output.ss
│ ├── perl
│ │ └── check.prl
│ ├── reflection.doc
│ ├── scheme2c
│ │ ├── book.sc
│ │ └── others
│ │ │ ├── ReadMe
│ │ │ ├── compat.ss
│ │ │ ├── expand.sc
│ │ │ ├── hooks.sc
│ │ │ ├── init.ss
│ │ │ ├── macro-defs.sc
│ │ │ └── output.ss
│ ├── scm
│ │ ├── Init.scm
│ │ └── others
│ │ │ ├── ReadMe
│ │ │ ├── compat.ss
│ │ │ ├── expand.bb
│ │ │ ├── hooks.scm
│ │ │ ├── init.ss
│ │ │ ├── macro-defs.bb
│ │ │ ├── output.ss
│ │ │ ├── prop.scm
│ │ │ └── syntax.tst
│ ├── si
│ │ ├── after.scm
│ │ ├── bibexp.scm
│ │ ├── bibrun.scm
│ │ ├── chap8j.scm
│ │ ├── chap9b.scm
│ │ ├── chap9c.scm
│ │ ├── example.scm
│ │ ├── fact.scm
│ │ ├── ffact.scm
│ │ ├── fib.scm
│ │ ├── foo.scm
│ │ ├── loading.scm
│ │ ├── na.out
│ │ ├── reflisp.scm
│ │ └── tmp.scm
│ ├── src
│ │ ├── c
│ │ │ ├── Imakefile
│ │ │ ├── c10ex.c
│ │ │ ├── c10kex.c
│ │ │ ├── chap10ex.c
│ │ │ ├── chap10kex.c
│ │ │ ├── loop.c
│ │ │ ├── rt.c
│ │ │ ├── rt.h
│ │ │ ├── scheme.c
│ │ │ ├── scheme.h
│ │ │ ├── schemeklib.c
│ │ │ └── schemelib.c
│ │ ├── chap1.scm
│ │ ├── chap1.tst
│ │ ├── chap10a.scm
│ │ ├── chap10b.scm
│ │ ├── chap10c.scm
│ │ ├── chap10d.scm
│ │ ├── chap10e.scm
│ │ ├── chap10e.tst
│ │ ├── chap10ex.scm
│ │ ├── chap10f.scm
│ │ ├── chap10g.scm
│ │ ├── chap10h.scm
│ │ ├── chap10i.scm
│ │ ├── chap10j.scm
│ │ ├── chap10j.tst
│ │ ├── chap10k.scm
│ │ ├── chap10k.tst
│ │ ├── chap10l.scm
│ │ ├── chap10m.scm
│ │ ├── chap10n.scm
│ │ ├── chap10p.scm
│ │ ├── chap1a.scm
│ │ ├── chap1b.scm
│ │ ├── chap1c.scm
│ │ ├── chap1d.scm
│ │ ├── chap2a.scm
│ │ ├── chap2a.tst
│ │ ├── chap2b.scm
│ │ ├── chap2b.tst
│ │ ├── chap2c.scm
│ │ ├── chap2c.tst
│ │ ├── chap2d.scm
│ │ ├── chap2e.scm
│ │ ├── chap2e.tst
│ │ ├── chap2f.scm
│ │ ├── chap2f.tst
│ │ ├── chap2g.scm
│ │ ├── chap2g.tst
│ │ ├── chap2h.scm
│ │ ├── chap2h.tst
│ │ ├── chap3a.scm
│ │ ├── chap3a.tst
│ │ ├── chap3b.scm
│ │ ├── chap3c.scm
│ │ ├── chap3d.scm
│ │ ├── chap3e.scm
│ │ ├── chap3f.scm
│ │ ├── chap3f.tst
│ │ ├── chap3g.scm
│ │ ├── chap3h.scm
│ │ ├── chap3i.scm
│ │ ├── chap3j.scm
│ │ ├── chap4.scm
│ │ ├── chap4.tst
│ │ ├── chap4a.scm
│ │ ├── chap4a.tst
│ │ ├── chap5-bench.scm
│ │ ├── chap5a.scm
│ │ ├── chap5b.scm
│ │ ├── chap5b.tst
│ │ ├── chap5c.scm
│ │ ├── chap5c.tst
│ │ ├── chap5d.scm
│ │ ├── chap5e.scm
│ │ ├── chap5e.tst
│ │ ├── chap5f.scm
│ │ ├── chap5g.scm
│ │ ├── chap5g.tst
│ │ ├── chap5h.scm
│ │ ├── chap6a.scm
│ │ ├── chap6b.scm
│ │ ├── chap6b.tst
│ │ ├── chap6c.scm
│ │ ├── chap6d.scm
│ │ ├── chap6dd.scm
│ │ ├── chap6dd.tst
│ │ ├── chap6e.scm
│ │ ├── chap6f.scm
│ │ ├── chap6g.scm
│ │ ├── chap6g.tst
│ │ ├── chap6h.scm
│ │ ├── chap7a.scm
│ │ ├── chap7b.scm
│ │ ├── chap7c.scm
│ │ ├── chap7d.scm
│ │ ├── chap7d.tst
│ │ ├── chap7e.scm
│ │ ├── chap7f.scm
│ │ ├── chap7g.scm
│ │ ├── chap7h.scm
│ │ ├── chap7i.scm
│ │ ├── chap8.tst
│ │ ├── chap8a.scm
│ │ ├── chap8a.tst
│ │ ├── chap8b.scm
│ │ ├── chap8b.tst
│ │ ├── chap8c.scm
│ │ ├── chap8d.scm
│ │ ├── chap8e.scm
│ │ ├── chap8f.scm
│ │ ├── chap8g.scm
│ │ ├── chap8h.scm
│ │ ├── chap8h.tst
│ │ ├── chap8i.scm
│ │ ├── chap8i.tst
│ │ ├── chap8j.scm
│ │ ├── chap8j.tst
│ │ ├── chap8k.scm
│ │ ├── chap9a.scm
│ │ ├── chap9a.tst
│ │ ├── chap9b.scm
│ │ ├── chap9c.scm
│ │ ├── chap9c.tst
│ │ ├── chap9d.scm
│ │ ├── chap9e.scm
│ │ ├── chap9f.scm
│ │ ├── chap9z.scm
│ │ ├── scheme.tst
│ │ ├── showGC.s2c
│ │ └── tester.scm
│ └── tmp.si
│ │ ├── after.scm
│ │ ├── bibexp.scm
│ │ ├── bibrun.scm
│ │ ├── chap8j.scm
│ │ ├── chap9b.scm
│ │ ├── chap9c.scm
│ │ ├── example.scm
│ │ ├── fact.scm
│ │ ├── ffact.scm
│ │ ├── fib.scm
│ │ ├── foo.scm
│ │ ├── loading.scm
│ │ ├── na.out
│ │ ├── reflisp.scm
│ │ └── tmp.scm
├── jmc.lisp
└── sicp
│ ├── README.md
│ ├── ch1.scm
│ ├── ch2.scm
│ ├── ch2support.scm
│ ├── ch2tests.scm
│ ├── ch3.scm
│ ├── ch3support.scm
│ ├── ch4-ambeval.scm
│ ├── ch4-analyzingmceval.scm
│ ├── ch4-leval.scm
│ ├── ch4-mceval.scm
│ ├── ch4-query.scm
│ ├── ch4.scm
│ ├── ch5-compiler.scm
│ ├── ch5-eceval-compiler.scm
│ ├── ch5-eceval-support.scm
│ ├── ch5-eceval.scm
│ ├── ch5-regsim.scm
│ ├── ch5-syntax.scm
│ ├── ch5.scm
│ ├── instructors-manual
│ ├── .gitignore
│ ├── README.md
│ ├── concrete-test.rkt
│ ├── concrete.rkt
│ ├── concrete2-test.rkt
│ ├── concrete2.rkt
│ └── scans
│ │ ├── 01-eval.png
│ │ ├── 02-apply.png
│ │ ├── 03-eval-if.png
│ │ └── 04-environment.png
│ ├── load-eceval-compiler.scm
│ └── load-eceval.scm
└── workshops
├── europython2022
├── CONTRIBUTING.md
├── CONTRIBUTORS.md
├── GLOSSARY.md
├── LICENSE
├── README.md
├── ast.txt
├── babilonian-method.scm
├── lispy.png
├── norvigs-lispy.ipynb
├── norvigs-lispy.py
├── preview.sh
└── requirements.txt
├── pythonbrasil2021
├── CONTRIBUTING.md
├── CONTRIBUTORS.md
├── GLOSSÁRIO.md
├── LICENSE
├── README.md
├── ast.txt
├── babilonian-method.scm
├── lispy.png
├── norvigs-lispy.ipynb
├── norvigs-lispy.py
├── preview.sh
└── requirements.txt
├── pythonbrasil2022
├── CONTRIBUTING.md
├── CONTRIBUTORS.md
├── GLOSSÁRIO.md
├── LICENSE
├── README.md
├── ast.txt
├── babilonian-method.scm
├── lispy.png
├── norvigs-lispy.ipynb
├── norvigs-lispy.py
├── preview.sh
├── requirements.txt
└── runtime.txt
├── pythonsudeste2024
├── CONTRIBUTING.md
├── CONTRIBUTORS.md
├── GLOSSÁRIO.md
├── LICENSE
├── README.md
├── ast.txt
├── babilonian-method.scm
├── jupyturtle.py
├── lis.py
├── lispy.png
├── lispyturtle.ipynb
├── lispyturtle.py
└── requirements.txt
└── thoughtworks2021
├── LICENSE
├── README.md
├── book-excerpt.adoc
├── cc-by-88x31.png
├── lispy.png
├── norvigs-lispy.ipynb
├── pycon-us-2022.md
└── requirements.txt
/LICENSE:
--------------------------------------------------------------------------------
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.
--------------------------------------------------------------------------------
/calculator/README.md:
--------------------------------------------------------------------------------
1 | # Norvig's Lispy Calculator
2 |
3 | Peter Norvig describes a small subset of the **Scheme** language
4 | that works as a calculator—before he explains `lis.py`
5 | in [(How to Write a (Lisp) Interpreter (in Python))](https://norvig.com/lispy.html).
6 |
7 | The [`calc.py`](calc.py) program is derived from the Python 3.10
8 | [version of `lis.py`](../original/py3.10/),
9 | keeping only the code needed for the calculator language.
10 |
11 | _LR_
12 | São Paulo, June 19, 2021
13 |
--------------------------------------------------------------------------------
/calculator/mypy.ini:
--------------------------------------------------------------------------------
1 | [mypy]
2 | python_version = 3.10
3 |
--------------------------------------------------------------------------------
/conftest.py:
--------------------------------------------------------------------------------
1 | import pytest
2 |
3 | def pytest_addoption(parser):
4 | parser.addoption('--slow', action='store_true',
5 | help='run tests marked @slow')
6 |
7 |
8 | def pytest_runtest_setup(item):
9 | if 'slow' in item.keywords and not item.config.getoption('--slow'):
10 | pytest.skip('add --slow to run this test')
11 |
--------------------------------------------------------------------------------
/mylis/mylis_1/examples_test.py:
--------------------------------------------------------------------------------
1 | import math
2 |
3 | from mylis import run
4 |
5 | fact_src = """
6 | (define (! n)
7 | (if (< n 2)
8 | 1
9 | (* n (! (- n 1)))
10 | )
11 | )
12 | (! 42)
13 | """
14 | def test_factorial():
15 | got = run(fact_src)
16 | assert got == 1405006117752879898543142606244511569936384000000000
17 | assert got == math.factorial(42)
18 |
19 |
20 | gcd_src = """
21 | (define (mod m n)
22 | (- m (* n (quotient m n))))
23 | (define (gcd m n)
24 | (if (= n 0)
25 | m
26 | (gcd n (mod m n))))
27 | (gcd 18 45)
28 | """
29 | def test_gcd():
30 | got = run(gcd_src)
31 | assert got == 9
32 |
33 |
34 | quicksort_src = """
35 | (define (quicksort lst)
36 | (if (null? lst)
37 | lst
38 | (begin
39 | (define pivot (car lst))
40 | (define rest (cdr lst))
41 | (append
42 | (quicksort
43 | (filter (lambda (x) (< x pivot)) rest))
44 | (list pivot)
45 | (quicksort
46 | (filter (lambda (x) (>= x pivot)) rest)))
47 | )
48 | )
49 | )
50 | (quicksort (list 2 1 6 3 4 0 8 9 7 5))
51 | """
52 | def test_quicksort():
53 | got = run(quicksort_src)
54 | assert got == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
55 |
56 |
57 | # Example from Structure and Interpretation of Computer Programs
58 | # https://mitpress.mit.edu/sites/default/files/sicp/full-text/sicp/book/node12.html
59 |
60 | newton_src = """
61 | (define (sqrt x)
62 | (sqrt-iter 1.0 x))
63 | (define (sqrt-iter guess x)
64 | (if (good-enough? guess x)
65 | guess
66 | (sqrt-iter (improve guess x) x)))
67 | (define (good-enough? guess x)
68 | (< (abs (- (* guess guess) x)) 0.001))
69 | (define (improve guess x)
70 | (average guess (/ x guess)))
71 | (define (average x y)
72 | (/ (+ x y) 2))
73 | (sqrt 123454321)
74 | """
75 | def test_newton():
76 | got = run(newton_src)
77 | assert math.isclose(got, 11111)
78 |
79 |
80 | closure_src = """
81 | (define (make-adder increment)
82 | (lambda (x) (+ increment x))
83 | )
84 | (define inc (make-adder 1))
85 | (inc 99)
86 | """
87 | def test_closure():
88 | got = run(closure_src)
89 | assert got == 100
90 |
--------------------------------------------------------------------------------
/mylis/mylis_1/exceptions.py:
--------------------------------------------------------------------------------
1 | class InterpreterException(Exception):
2 | """Generic interpreter exception."""
3 |
4 | def __init__(self, value: str = ''):
5 | self.value = value
6 |
7 | def __str__(self) -> str:
8 | msg = self.__class__.__doc__ or ''
9 | if self.value:
10 | msg = msg.rstrip('.')
11 | if "'" in self.value:
12 | value = self.value
13 | else:
14 | value = repr(self.value)
15 | msg += f': {value}'
16 | return msg
17 |
18 |
19 | class ParserException(InterpreterException):
20 | """Generic exception while parsing."""
21 |
22 |
23 | class UnexpectedCloseParen(ParserException):
24 | """Unexpected close parenthesis."""
25 |
26 |
27 | class UnexpectedEndOfSource(ParserException):
28 | """Unexpected end of source code."""
29 |
30 |
31 | class EvaluatorException(InterpreterException):
32 | """Exception while evaluating."""
33 |
34 | class InvalidSyntax(EvaluatorException):
35 | """Invalid syntax."""
36 |
37 | class UndefinedSymbol(EvaluatorException):
38 | """Undefined symbol."""
39 |
--------------------------------------------------------------------------------
/mylis/mylis_1/meta_test.py:
--------------------------------------------------------------------------------
1 | import operator as op
2 |
3 | import mylis
4 |
5 | env_scm = """
6 | (define standard-env (list
7 | (list (quote not) not)
8 | (list (quote eq?) eq?)
9 | ))
10 | standard-env
11 | """
12 |
13 | def test_env_build():
14 | got = mylis.run(env_scm)
15 | assert got == [['not', op.not_], ['eq?', op.is_]]
16 |
17 | scan_scm = """
18 | (define l (quote (a b c)))
19 | (define (scan what where)
20 | (cond ((null? where) #f)
21 | ((equal? what (car where)) what)
22 | (else (scan what (cdr where)))))
23 | """
24 |
25 | def test_scan():
26 | source = scan_scm + '(scan (quote a) l )'
27 | got = mylis.run(source)
28 | assert got == 'a'
29 |
30 |
31 | def test_scan_not_found():
32 | source = scan_scm + '(scan (quote z) l )'
33 | got = mylis.run(source)
34 | assert got is False
35 |
36 |
37 | lookup_scm = """
38 | (define env (list
39 | (list (quote not) not)
40 | (list (quote eq?) eq?)
41 | ))
42 | (define (lookup what where)
43 | (cond ((null? where) #f)
44 | ((equal? what (car (car where))) (car (cdr (car where))))
45 | (else (lookup what (cdr where)))))
46 | """
47 |
48 | def test_lookup():
49 | source = lookup_scm + '(lookup (quote eq?) env)'
50 | got = mylis.run(source)
51 | assert got == op.is_
52 |
53 |
54 | def test_lookup_not_found():
55 | source = lookup_scm + '(lookup (quote z) env )'
56 | got = mylis.run(source)
57 | assert got is False
58 |
--------------------------------------------------------------------------------
/mylis/mylis_1/mypy.ini:
--------------------------------------------------------------------------------
1 | [mypy]
2 | python_version = 3.10
3 |
--------------------------------------------------------------------------------
/mylis/mylis_1/requirements.txt:
--------------------------------------------------------------------------------
1 | pytest==6.2.4
2 | dialogue-tester==1.0b5
3 |
--------------------------------------------------------------------------------
/mylis/mylis_1_closure/examples_test.py:
--------------------------------------------------------------------------------
1 | import math
2 |
3 | from mylis import run
4 |
5 | fact_src = """
6 | (define (! n)
7 | (if (< n 2)
8 | 1
9 | (* n (! (- n 1)))
10 | )
11 | )
12 | (! 42)
13 | """
14 | def test_factorial():
15 | got = run(fact_src)
16 | assert got == 1405006117752879898543142606244511569936384000000000
17 | assert got == math.factorial(42)
18 |
19 |
20 | gcd_src = """
21 | (define (mod m n)
22 | (- m (* n (quotient m n))))
23 | (define (gcd m n)
24 | (if (= n 0)
25 | m
26 | (gcd n (mod m n))))
27 | (gcd 18 45)
28 | """
29 | def test_gcd():
30 | got = run(gcd_src)
31 | assert got == 9
32 |
33 |
34 | quicksort_src = """
35 | (define (quicksort lst)
36 | (if (null? lst)
37 | lst
38 | (begin
39 | (define pivot (car lst))
40 | (define rest (cdr lst))
41 | (append
42 | (quicksort
43 | (filter (lambda (x) (< x pivot)) rest))
44 | (list pivot)
45 | (quicksort
46 | (filter (lambda (x) (>= x pivot)) rest)))
47 | )
48 | )
49 | )
50 | (quicksort (list 2 1 6 3 4 0 8 9 7 5))
51 | """
52 | def test_quicksort():
53 | got = run(quicksort_src)
54 | assert got == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
55 |
56 |
57 | # Example from Structure and Interpretation of Computer Programs
58 | # https://mitpress.mit.edu/sites/default/files/sicp/full-text/sicp/book/node12.html
59 |
60 | newton_src = """
61 | (define (sqrt x)
62 | (sqrt-iter 1.0 x))
63 | (define (sqrt-iter guess x)
64 | (if (good-enough? guess x)
65 | guess
66 | (sqrt-iter (improve guess x) x)))
67 | (define (good-enough? guess x)
68 | (< (abs (- (* guess guess) x)) 0.001))
69 | (define (improve guess x)
70 | (average guess (/ x guess)))
71 | (define (average x y)
72 | (/ (+ x y) 2))
73 | (sqrt 123454321)
74 | """
75 | def test_newton():
76 | got = run(newton_src)
77 | assert math.isclose(got, 11111)
78 |
79 |
80 | closure_src = """
81 | (define (make-adder increment)
82 | (lambda (x) (+ increment x))
83 | )
84 | (define inc (make-adder 1))
85 | (inc 99)
86 | """
87 | def test_closure():
88 | got = run(closure_src)
89 | assert got == 100
90 |
--------------------------------------------------------------------------------
/mylis/mylis_1_closure/exceptions.py:
--------------------------------------------------------------------------------
1 | class InterpreterException(Exception):
2 | """Generic interpreter exception."""
3 |
4 | def __init__(self, value: str = ''):
5 | self.value = value
6 |
7 | def __str__(self) -> str:
8 | msg = self.__class__.__doc__ or ''
9 | if self.value:
10 | msg = msg.rstrip('.')
11 | if "'" in self.value:
12 | value = self.value
13 | else:
14 | value = repr(self.value)
15 | msg += f': {value}'
16 | return msg
17 |
18 |
19 | class ParserException(InterpreterException):
20 | """Generic exception while parsing."""
21 |
22 |
23 | class UnexpectedCloseParen(ParserException):
24 | """Unexpected close parenthesis."""
25 |
26 |
27 | class UnexpectedEndOfSource(ParserException):
28 | """Unexpected end of source code."""
29 |
30 |
31 | class EvaluatorException(InterpreterException):
32 | """Exception while evaluating."""
33 |
34 | class InvalidSyntax(EvaluatorException):
35 | """Invalid syntax."""
36 |
37 | class UndefinedSymbol(EvaluatorException):
38 | """Undefined symbol."""
39 |
--------------------------------------------------------------------------------
/mylis/mylis_1_closure/meta_test.py:
--------------------------------------------------------------------------------
1 | import operator as op
2 |
3 | import mylis
4 |
5 | env_scm = """
6 | (define standard-env (list
7 | (list (quote not) not)
8 | (list (quote eq?) eq?)
9 | ))
10 | standard-env
11 | """
12 |
13 | def test_env_build():
14 | got = mylis.run(env_scm)
15 | assert got == [['not', op.not_], ['eq?', op.is_]]
16 |
17 | scan_scm = """
18 | (define l (quote (a b c)))
19 | (define (scan what where)
20 | (cond ((null? where) #f)
21 | ((equal? what (car where)) what)
22 | (else (scan what (cdr where)))))
23 | """
24 |
25 | def test_scan():
26 | source = scan_scm + '(scan (quote a) l )'
27 | got = mylis.run(source)
28 | assert got == 'a'
29 |
30 |
31 | def test_scan_not_found():
32 | source = scan_scm + '(scan (quote z) l )'
33 | got = mylis.run(source)
34 | assert got is False
35 |
36 |
37 | lookup_scm = """
38 | (define env (list
39 | (list (quote not) not)
40 | (list (quote eq?) eq?)
41 | ))
42 | (define (lookup what where)
43 | (cond ((null? where) #f)
44 | ((equal? what (car (car where))) (car (cdr (car where))))
45 | (else (lookup what (cdr where)))))
46 | """
47 |
48 | def test_lookup():
49 | source = lookup_scm + '(lookup (quote eq?) env)'
50 | got = mylis.run(source)
51 | assert got == op.is_
52 |
53 |
54 | def test_lookup_not_found():
55 | source = lookup_scm + '(lookup (quote z) env )'
56 | got = mylis.run(source)
57 | assert got is False
58 |
--------------------------------------------------------------------------------
/mylis/mylis_1_closure/mypy.ini:
--------------------------------------------------------------------------------
1 | [mypy]
2 | python_version = 3.10
3 |
--------------------------------------------------------------------------------
/mylis/mylis_1_closure/requirements.txt:
--------------------------------------------------------------------------------
1 | pytest==6.2.4
2 | dialogue-tester==1.0b5
3 |
--------------------------------------------------------------------------------
/mylis/mylis_2/.gitignore:
--------------------------------------------------------------------------------
1 | compiled/
--------------------------------------------------------------------------------
/mylis/mylis_2/examples/count-change.scm:
--------------------------------------------------------------------------------
1 | (define (count-change amount)
2 | (cc amount 5))
3 |
4 | (define (cc amount kinds-of-coins)
5 | (cond ((= amount 0) 1)
6 | ((or (< amount 0) (= kinds-of-coins 0)) 0)
7 | (else (+ (cc amount
8 | (- kinds-of-coins 1))
9 | (cc (- amount
10 | (first-denomination kinds-of-coins))
11 | kinds-of-coins)))))
12 |
13 | (define (first-denomination kinds-of-coins)
14 | (cond ((= kinds-of-coins 1) 1)
15 | ((= kinds-of-coins 2) 5)
16 | ((= kinds-of-coins 3) 10)
17 | ((= kinds-of-coins 4) 25)
18 | ((= kinds-of-coins 5) 50)))
19 |
20 | (display (count-change amt))
21 |
--------------------------------------------------------------------------------
/mylis/mylis_2/examples/fact-iter.scm:
--------------------------------------------------------------------------------
1 | (define (! n)
2 | (factorial-iter n 1))
3 |
4 | (define (factorial-iter n product)
5 | (if (= n 1)
6 | product
7 | (factorial-iter (- n 1) (* n product))))
8 | (display (! x))
9 |
--------------------------------------------------------------------------------
/mylis/mylis_2/examples/fact.scm:
--------------------------------------------------------------------------------
1 | (define (! n)
2 | (if (<= n 1)
3 | 1
4 | (* n (! (- n 1)))))
5 | (display (! x))
6 |
--------------------------------------------------------------------------------
/mylis/mylis_2/examples/fibo-n.scm:
--------------------------------------------------------------------------------
1 | (define (fibo n)
2 | (fibo-iter 1 0 n))
3 |
4 | (define (fibo-iter a b count)
5 | (if (= count 0)
6 | b
7 | (fibo-iter (+ a b) a (- count 1))))
8 |
9 | (display (fibo n))
10 |
--------------------------------------------------------------------------------
/mylis/mylis_2/examples/fibo-rec.scm:
--------------------------------------------------------------------------------
1 | (define (fibo n)
2 | (cond ((= n 0) 0)
3 | ((= n 1) 1)
4 | (else (+ (fibo (- n 1))
5 | (fibo (- n 2))))))
6 |
7 | (cond
8 | ((>= n 20)
9 | (display (quote (This will take a while)))))
10 |
11 | (display (fibo n))
12 |
--------------------------------------------------------------------------------
/mylis/mylis_2/examples/fibo-seq.scm:
--------------------------------------------------------------------------------
1 | (define (fibo-seq n)
2 | (fibo-seq-iter 1 0 n ()))
3 |
4 | (define (fibo-seq-iter a b count seq)
5 | (if (= count 0)
6 | (reverse seq)
7 | (fibo-seq-iter
8 | (+ a b)
9 | a
10 | (decrement count)
11 | (cons a seq))))
12 |
13 | (define (decrement n) (- n 1))
14 |
15 | (define (reverse lis)
16 | (if (null? lis)
17 | ()
18 | (append (reverse (cdr lis))
19 | (list (car lis)))))
20 |
21 | (display (fibo-seq n))
22 |
--------------------------------------------------------------------------------
/mylis/mylis_2/examples/member.scm:
--------------------------------------------------------------------------------
1 | (define (member item lst)
2 | (cond ((null? lst) #f)
3 | ((equal? item (car lst)) lst)
4 | (else (member item (cdr lst)))))
5 |
6 | (define (member? item lst)
7 | (and (member item lst) item))
8 |
9 | (define small-primes
10 | (list 5 7 11 13 17 19 23 29 31 37 41 43
11 | 47 53 59 61 67 71 73 79 83 89 97))
12 |
13 | (display (member? n small-primes))
14 |
--------------------------------------------------------------------------------
/mylis/mylis_2/examples/newton.scm:
--------------------------------------------------------------------------------
1 | (define (sqrt x)
2 | (sqrt-iter 1.0 x))
3 | (define (sqrt-iter guess x)
4 | (if (good-enough? guess x)
5 | guess
6 | (sqrt-iter (improve guess x) x)))
7 | (define (good-enough? guess x)
8 | (< (abs (- (* guess guess) x)) 0.00000001))
9 | (define (improve guess x)
10 | (average guess (/ x guess)))
11 | (define (average x y)
12 | (/ (+ x y) 2))
13 | (display (sqrt x))
14 |
--------------------------------------------------------------------------------
/mylis/mylis_2/examples/prime.scm:
--------------------------------------------------------------------------------
1 | (define (smallest-divisor n)
2 | (find-divisor n 2))
3 |
4 | (define (find-divisor n test-divisor)
5 | (cond ((> (* test-divisor test-divisor) n) n)
6 | ((divides? test-divisor n) test-divisor)
7 | (else (find-divisor n (+ test-divisor 1)))))
8 |
9 | (define (divides? a b)
10 | (= (remainder b a) 0))
11 |
12 | (define (prime? n)
13 | (= n (smallest-divisor n)))
14 |
15 | (display (prime? n))
16 |
--------------------------------------------------------------------------------
/mylis/mylis_2/examples/sumints.scm:
--------------------------------------------------------------------------------
1 | (define (sum n acc)
2 | (if (= n 0)
3 | acc
4 | (sum (- n 1) (+ n acc))))
5 |
6 | (define (pow10 n)
7 | (if (= n 0)
8 | 1
9 | (* 10 (pow10 (- n 1)))))
10 |
11 | (define (report first last)
12 | (if (= first last)
13 | 0
14 | (begin
15 | (define n (pow10 first))
16 | (display (list first n (sum n 0)))
17 | (report (+ first 1) last))))
18 |
19 | (display (quote (This takes > 30s to run)))
20 | (report 1 7)
21 |
--------------------------------------------------------------------------------
/mylis/mylis_2/exceptions.py:
--------------------------------------------------------------------------------
1 | class InterpreterException(Exception):
2 | """Generic interpreter exception."""
3 |
4 | def __init__(self, value: str = ''):
5 | self.value = value
6 |
7 | def __str__(self) -> str:
8 | msg = self.__class__.__doc__ or ''
9 | if self.value:
10 | msg = msg.rstrip('.')
11 | if "'" in self.value:
12 | value = self.value
13 | else:
14 | value = repr(self.value)
15 | msg += f': {value}'
16 | return msg
17 |
18 |
19 | class ParserException(InterpreterException):
20 | """Generic exception while parsing."""
21 |
22 |
23 | class UnexpectedCloseParen(ParserException):
24 | """Unexpected close parenthesis."""
25 |
26 |
27 | class UnexpectedEndOfSource(ParserException):
28 | """Unexpected end of source code."""
29 |
30 |
31 | class EvaluatorException(InterpreterException):
32 | """Exception while evaluating."""
33 |
34 | class InvalidSyntax(EvaluatorException):
35 | """Invalid syntax."""
36 |
37 | class UndefinedSymbol(EvaluatorException):
38 | """Undefined symbol."""
39 |
--------------------------------------------------------------------------------
/mylis/mylis_2/exercises/ackermann.py:
--------------------------------------------------------------------------------
1 | """This module contains a code example related to
2 |
3 | Think Python, 2nd Edition
4 | by Allen Downey
5 | http://thinkpython2.com
6 |
7 | Copyright 2015 Allen Downey
8 |
9 | License: http://creativecommons.org/licenses/by/4.0/
10 | """
11 |
12 | def ackermann(m, n):
13 | """Computes the Ackermann function A(m, n)
14 |
15 | See http://en.wikipedia.org/wiki/Ackermann_function
16 |
17 | n, m: non-negative integers
18 | """
19 | global calls
20 | calls += 1
21 | if m == 0:
22 | return n + 1
23 | if n == 0:
24 | return ackermann(m - 1, 1)
25 | return ackermann(m - 1, ackermann(m, n - 1))
26 |
27 |
28 | if __name__ == '__main__':
29 |
30 | # NOTE:
31 | # Clear the cache before each iteration to avoid
32 | # cached results from previous iterations affecting
33 | # the counting of calls.
34 |
35 | import sys, functools
36 |
37 | if len(sys.argv) > 1:
38 | ackermann = functools.cache(ackermann)
39 |
40 | for m in range(0, 5):
41 | for n in range(0, 10):
42 | if hasattr(ackermann, 'cache_clear'):
43 | ackermann.cache_clear()
44 | calls = 0
45 | try:
46 | res = ackermann(m, n)
47 | except RecursionError:
48 | res = '...'
49 | print(f'A({m}, {n}) = {res:>4}\t[{calls:,.0f} calls]')
50 |
51 | if len(sys.argv) == 1:
52 | print(f'To enable caching, pass the "c" command-line option.')
53 |
--------------------------------------------------------------------------------
/mylis/mylis_2/exercises/fibonacci.py:
--------------------------------------------------------------------------------
1 | import math
2 |
3 |
4 | def fibonacci_gen():
5 | """Idiomatic Fibonacci sequence generator"""
6 | a, b = 0, 1
7 | while True:
8 | yield a
9 | a, b = b, a + b
10 |
11 |
12 | def fibonacci_round(n):
13 | """This works only for n<=70 with 64 bit floats"""
14 | assert n <= 70, 'Not enough precision for n >= 70'
15 | sqrt5 = math.sqrt(5)
16 | phi = (1 + sqrt5) / 2
17 | return round(phi ** n / sqrt5)
18 |
19 |
20 | if __name__ == '__main__':
21 |
22 | for i, fg in zip(range(100), fibonacci_gen()):
23 | fr = fibonacci_round(i)
24 | print(f'{i:3}\t{fr}\t{fr - fg}')
25 |
--------------------------------------------------------------------------------
/mylis/mylis_2/metacircular.rkt:
--------------------------------------------------------------------------------
1 | #lang racket/base
2 |
3 | (define (MAKE-GLOBAL-ENV) (list
4 | (list (quote NOT) not)
5 | (list (quote EQ?) eq?)
6 | (list (quote EQUAL?) equal?)
7 | (list (quote ADD) +)
8 | (list (quote SUB) -)
9 | (list (quote MUL) *)
10 | (list (quote DIV) /)
11 | (list (quote MAP) map)
12 | (list (quote LIST) list)
13 | (list (quote APPEND) append)
14 | ))
15 |
16 | (define GLOBAL-ENV (MAKE-GLOBAL-ENV))
17 |
18 | (define (LOOKUP name env)
19 | (cond
20 | ((null? env) #f)
21 | ((equal? name (car (car env))) (car (cdr (car env))))
22 | (else (LOOKUP name (cdr env)))))
23 |
24 | (define (REST list) (cdr list))
25 | (define (FIRST list) (car list))
26 | (define (SECOND list) (car (cdr list)))
27 | (define (THIRD list) (car (cdr (cdr list))))
28 | (define (FOURTH list) (car (cdr (cdr (cdr list)))))
29 |
30 | (define (EVLIS exps env)
31 | (if (null? exps)
32 | (quote ())
33 | (cons (EVAL (FIRST exps) env)
34 | (EVLIS (REST exps) env))
35 | )
36 | )
37 |
38 | (define (EVAL exp env)
39 | (cond
40 | ((number? exp) exp)
41 | ((symbol? exp) (LOOKUP exp env))
42 | ((equal? (FIRST exp) (quote QUOTE))
43 | (SECOND exp) )
44 | ((equal? (FIRST exp) (quote IF))
45 | (if (EVAL (SECOND exp) env)
46 | (EVAL (THIRD exp) env)
47 | (EVAL (FOURTH exp) env)))
48 | ((equal? (FIRST exp) (quote LAMBDA))
49 | (MAKE-PROCEDURE (SECOND exp) (THIRD exp) env))
50 | ((equal? (FIRST exp) (quote DEFINE-GLOBAL))
51 | (set! GLOBAL-ENV (cons (list (SECOND exp) (EVAL (THIRD exp) env)) GLOBAL-ENV)))
52 | (else
53 | (apply (EVAL (FIRST exp) env) (EVLIS (REST exp) env)))))
54 |
55 | (define (MAKE-PROCEDURE parms body definition-env)
56 | (lambda (args)
57 | (EVAL body
58 | (append
59 | (map list parms (list args))
60 | definition-env))
61 | )
62 | )
63 |
64 | (provide LOOKUP GLOBAL-ENV EVAL)
65 |
--------------------------------------------------------------------------------
/mylis/mylis_2/metacircular.scm:
--------------------------------------------------------------------------------
1 | (define (GLOBAL-ENV) (list
2 | (list (quote NOT) not)
3 | (list (quote EQ?) eq?)
4 | (list (quote EQUAL?) equal?)
5 | (list (quote ADD) +)
6 | (list (quote SUB) -)
7 | (list (quote MUL) *)
8 | (list (quote DIV) /)
9 | (list (quote MAP) map)
10 | (list (quote LIST) list)
11 | (list (quote APPEND) append)
12 | ))
13 |
14 | (define (LOOKUP name env)
15 | (cond
16 | ((null? env) #f)
17 | ((equal? name (car (car env))) (car (cdr (car env))))
18 | (else (LOOKUP name (cdr env)))))
19 |
20 | (define (REST list) (cdr list))
21 | (define (FIRST list) (car list))
22 | (define (SECOND list) (car (cdr list)))
23 | (define (THIRD list) (car (cdr (cdr list))))
24 | (define (FOURTH list) (car (cdr (cdr (cdr list)))))
25 |
26 | (define (EVLIS exps env)
27 | (if (null? exps)
28 | (quote ())
29 | (cons (EVAL (FIRST exps) env)
30 | (EVLIS (REST exps) env))
31 | )
32 | )
33 |
34 | (define (EVAL exp env)
35 | (cond
36 | ((number? exp) exp)
37 | ((symbol? exp) (LOOKUP exp env))
38 | ((equal? (FIRST exp) (quote QUOTE))
39 | (SECOND exp)
40 | )
41 | ((equal? (FIRST exp) (quote IF))
42 | (if (EVAL (SECOND exp) env)
43 | (EVAL (THIRD exp) env)
44 | (EVAL (FOURTH exp) env)
45 | )
46 | )
47 | ((equal? (FIRST exp) (quote LAMBDA))
48 | (lambda (SECOND exp) (THIRD exp))
49 | )
50 | (else
51 | (apply (EVAL (FIRST exp) env) (EVLIS (REST exp) env))
52 | )
53 | )
54 | )
55 |
56 | (define (MAKE-PROCEDURE parms body definition-env)
57 | (lambda (args)
58 | (EVAL body
59 | (APPEND
60 | (MAP LIST parms args)
61 | definition-env))
62 | )
63 | )
64 |
--------------------------------------------------------------------------------
/mylis/mylis_2/metacircular_test.rkt:
--------------------------------------------------------------------------------
1 | #lang racket/base
2 |
3 | (require "metacircular.rkt")
4 | (require rackunit)
5 |
6 |
7 | ; LOOKUP
8 | (check-equal? (LOOKUP 'ADD GLOBAL-ENV) + "Existing name")
9 | (check-equal? (LOOKUP 'NO-SUCH-NAME GLOBAL-ENV) #f "Non-existing name")
10 |
11 | ; EVAL
12 | (check-equal? (EVAL '1 '()) 1 "Number literal")
13 | (check-equal? (EVAL 'ADD GLOBAL-ENV) + "Built-in procedure")
14 | (check-equal? (EVAL '(QUOTE (A (B C))) GLOBAL-ENV) '(A (B C)) "QUOTE form")
15 | (check-equal? (EVAL '(IF (EQUAL? 3 3) 1 0) GLOBAL-ENV) 1 "IF form, consequence branch")
16 | (check-equal? (EVAL '(IF (EQUAL? 3 4) 1 0) GLOBAL-ENV) 0 "IF form, alternative branch")
17 | (check-true (procedure? (EVAL '(LAMBDA (N) (MUL N 2)) GLOBAL-ENV)) "LAMBDA form, definition")
18 | (check-equal? (EVAL '((LAMBDA (N) (MUL N 2)) 4) GLOBAL-ENV) 8 "LAMBDA form, application")
19 | (check-equal? (begin (EVAL '(DEFINE-GLOBAL X 8) GLOBAL-ENV) (car GLOBAL-ENV)) '(X 8)) ; how do I inspect the GLOBAL-ENV?
20 | ; I don't yet know how to write DEFINE, because it would need to update the local environment
21 | (check-equal? (begin (EVAL '(DEFINE-GLOBAL X2 (LAMBDA (N) (MUL N 2))) GLOBAL-ENV) (EVAL '(X2 8) GLOBAL-ENV)) 16)
22 |
--------------------------------------------------------------------------------
/mylis/mylis_2/mypy.ini:
--------------------------------------------------------------------------------
1 | [mypy]
2 | python_version = 3.10
3 |
--------------------------------------------------------------------------------
/mylis/mylis_2/requirements.txt:
--------------------------------------------------------------------------------
1 | pytest==6.2.4
2 | dialogue-tester==1.0b5
3 |
--------------------------------------------------------------------------------
/mylis/mylis_3/README.md:
--------------------------------------------------------------------------------
1 | Rewriting from scratch, refactoring modules.
2 | `mylis.py` (TBD) will be the main module.
3 |
4 | ## Changes
5 |
6 | Changed behavior since `mylis_2` includes:
7 |
8 | ### Alternative braces allowed
9 |
10 | The characters `([{}])` may be used to build s-expressions.
11 | The close brace must match open brace.
12 |
13 | Use them as you wish, but the conventional usage in `mylis_3` is:
14 |
15 | - `()` for function application
16 | - `{}` for special forms
17 | - `[]` for everything else
18 |
19 | Example 1:
20 |
21 | ```
22 | {lambda [a b] {if (>= a b) a b}}
23 | ```
24 |
25 | Example 2:
26 |
27 | ```
28 | {cond
29 | [(> x 0) x]
30 | [(= x 0) 0]
31 | [(< x 0) (- 0 x)]}
32 | ```
33 |
--------------------------------------------------------------------------------
/mylis/mylis_3/__init__.py:
--------------------------------------------------------------------------------
1 | import os, sys
2 | sys.path.append(os.path.dirname(os.path.realpath(__file__)))
--------------------------------------------------------------------------------
/mylis/mylis_3/environ.py:
--------------------------------------------------------------------------------
1 | import collections
2 | import functools as ft
3 | import math
4 | import operator as op
5 |
6 | from mytypes import Symbol
7 | from parser import s_expr
8 |
9 | class Environment(collections.ChainMap):
10 | "A ChainMap that allows updating an item in-place."
11 |
12 | def change(self, key: Symbol, value: object) -> None:
13 | "Find where key is defined and change the value there."
14 | for map in self.maps:
15 | if key in map:
16 | map[key] = value
17 | return
18 | raise KeyError(key)
19 |
20 |
21 | def core_env() -> Environment:
22 | "An environment for an s-expression calculator."
23 | env = Environment(vars(math)) # sin, cos, sqrt, pi, ...
24 | env.update(
25 | {
26 | '+': lambda *args: sum(args),
27 | '-': op.sub,
28 | '*': op.mul,
29 | '/': op.truediv,
30 | 'quotient': op.floordiv,
31 | '=': ft.partial(variadic_comparison, op.eq),
32 | '<': ft.partial(variadic_comparison, op.lt),
33 | '>': ft.partial(variadic_comparison, op.gt),
34 | '<=': ft.partial(variadic_comparison, op.le),
35 | '>=': ft.partial(variadic_comparison, op.ge),
36 | 'abs': abs,
37 | 'begin': lambda *x: x[-1],
38 | 'eq?': op.is_,
39 | 'equal?': op.eq,
40 | 'max': max,
41 | 'min': min,
42 | 'not': op.not_,
43 | 'number?': lambda x: isinstance(x, (int, float)),
44 | 'procedure?': callable,
45 | 'modulo': op.mod,
46 | 'round': round,
47 | 'symbol?': lambda x: isinstance(x, Symbol),
48 | 'display': display,
49 | }
50 | )
51 | return env
52 |
53 |
54 | def display(obj: object) -> None:
55 | output = s_expr(obj)
56 | print(output)
57 |
58 |
59 | def variadic_comparison(op, current, *rest):
60 | for arg in rest:
61 | if not op(current, arg):
62 | return False
63 | current = arg
64 | return True
65 |
--------------------------------------------------------------------------------
/mylis/mylis_3/environ_test.py:
--------------------------------------------------------------------------------
1 | from environ import core_env
2 | from evaluator import evaluate
3 | from mytypes import Expression
4 | from parser import parse
5 |
6 | from pytest import mark
7 |
8 | @mark.parametrize('source, expected', [
9 | ('(+ 1 2)', 3),
10 | ('(+ 1)', 1),
11 | ('(+ 1 2 3)', 6),
12 | ('(+)', 0),
13 | ])
14 | def test_variadic_add(source: str, expected: Expression) -> None:
15 | got = evaluate(parse(source), core_env())
16 | assert got == expected
17 |
18 |
19 | @mark.parametrize('source, expected', [
20 | ('(= 3 3)', True),
21 | ('(= 1 3)', False),
22 | ('(= 2 2 2)', True),
23 | ('(= 2 2 1)', False),
24 | ])
25 | def test_variadic_eq(source: str, expected: Expression) -> None:
26 | got = evaluate(parse(source), core_env())
27 | assert got == expected
28 |
29 |
30 | @mark.parametrize('source, expected', [
31 | ('(= 3 3)', True),
32 | ('(= 1 3)', False),
33 | ('(= 2 2 2)', True),
34 | ('(= 2 2 1)', False),
35 | ('(< 3 3)', False),
36 | ('(< 1 3)', True),
37 | ('(< 1 2 2)', False),
38 | ('(< 1 2 3)', True),
39 | ('(<= 3 3)', True),
40 | ('(<= 1 3)', True),
41 | ('(<= 1 2 2)', True),
42 | ('(<= 1 2 3)', True),
43 | ('(<= 1 2 1)', False),
44 | ('(> 3 3)', False),
45 | ('(> 3 1)', True),
46 | ('(> 2 1 2)', False),
47 | ('(> 3 2 1)', True),
48 | ('(>= 3 1)', True),
49 | ('(>= 1 3)', False),
50 | ('(>= 3 2 1)', True),
51 | ('(>= 3 2 2)', True),
52 | ('(>= 2 1 2)', False),
53 | ])
54 | def test_variadic_comparison(source: str, expected: Expression) -> None:
55 | got = evaluate(parse(source), core_env())
56 | assert got == expected
--------------------------------------------------------------------------------
/mylis/mylis_3/examples/fact-iter.scm:
--------------------------------------------------------------------------------
1 | (define (! n)
2 | (factorial-iter n 1))
3 |
4 | (define (factorial-iter n product)
5 | (if (= n 1)
6 | product
7 | (factorial-iter (- n 1) (* n product))))
8 | (display (! x))
9 |
--------------------------------------------------------------------------------
/mylis/mylis_3/examples/fact.scm:
--------------------------------------------------------------------------------
1 | (define (! n)
2 | (if (<= n 1)
3 | 1
4 | (* n (! (- n 1)))))
5 | (display (! x))
6 |
--------------------------------------------------------------------------------
/mylis/mylis_3/mypy.ini:
--------------------------------------------------------------------------------
1 | [mypy]
2 | explicit_package_bases = True
3 |
--------------------------------------------------------------------------------
/mylis/mylis_3/mytypes.py:
--------------------------------------------------------------------------------
1 | from collections.abc import MutableMapping
2 | from typing import TypeAlias
3 |
4 |
5 | # cannot use NewType because we need isinstance()
6 | # to support (symbol? x) in environ.py
7 | Symbol: TypeAlias = str
8 | Number: TypeAlias = int | float
9 | Atom: TypeAlias = int | float | Symbol
10 | Expression: TypeAlias = Atom | list # type: ignore
11 |
12 |
13 | class InterpreterException(Exception):
14 | """Generic interpreter exception."""
15 |
16 | def __init__(self, value: str = ''):
17 | self.value = value
18 |
19 | def __str__(self) -> str:
20 | msg = self.__class__.__doc__ or ''
21 | if self.value:
22 | msg = msg.rstrip('.')
23 | if "'" in self.value:
24 | value = self.value
25 | else:
26 | value = repr(self.value)
27 | msg += f': {value}'
28 | return msg
29 |
30 |
31 | class ParserException(InterpreterException):
32 | """Generic exception while parsing."""
33 |
34 |
35 | class UnexpectedCloseBrace(ParserException):
36 | """Unexpected close brace."""
37 |
38 |
39 | class BraceNeverClosed(ParserException):
40 | """Open brace was never closed."""
41 |
42 |
43 | class EvaluatorException(InterpreterException):
44 | """Exception while evaluating."""
45 |
46 |
47 | class InvalidSyntax(EvaluatorException):
48 | """Invalid syntax."""
49 |
50 |
51 | class UndefinedSymbol(EvaluatorException):
52 | """Undefined symbol."""
53 |
--------------------------------------------------------------------------------
/mylis/mylis_3/requirements.txt:
--------------------------------------------------------------------------------
1 | attrs==22.1.0
2 | black==22.1.0
3 | blue==0.9.1
4 | click==8.1.3
5 | colorama==0.4.6
6 | dialogue-tester==1.0b5
7 | exceptiongroup==1.0.4
8 | flake8==4.0.1
9 | iniconfig==1.1.1
10 | mccabe==0.6.1
11 | mypy==0.991
12 | mypy-extensions==0.4.3
13 | packaging==21.3
14 | pathspec==0.10.2
15 | platformdirs==2.5.4
16 | pluggy==1.0.0
17 | pycodestyle==2.8.0
18 | pyflakes==2.4.0
19 | pyparsing==3.0.9
20 | pytest==7.2.0
21 | tomli==2.0.1
22 | typing_extensions==4.4.0
23 |
--------------------------------------------------------------------------------
/original/LICENSE:
--------------------------------------------------------------------------------
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.
--------------------------------------------------------------------------------
/original/README.md:
--------------------------------------------------------------------------------
1 | # Norvig's originals and updates
2 |
3 | This directory contains:
4 |
5 | * `norvig/`:
6 | Norvig's [`lis.py` unchanged](https://github.com/norvig/pytudes/blob/c33cd6835a506a57d9fe73e3a8317d49babb13e8/py/lis.py),
7 | and [`lispy.py`](https://github.com/norvig/pytudes/blob/c33cd6835a506a57d9fe73e3a8317d49babb13e8/py/lispy.py) with
8 | [minor changes](https://github.com/norvig/pytudes/pull/106) to run on Python 3,
9 | and the `lispytest.py` custom test script;
10 | * `py3.9/`: `lis.py` with type hints and a few minor edits—requires Python 3.9;
11 | * `py3.10/`: `lis.py` with type hints, pattern matching, and minor edits—requires Python 3.10.
12 |
13 | The `py3.9/` and `py3.10/` directories also have `lis_test.py` to run with
14 | [pytest](https://docs.pytest.org).
15 | These files include all the
16 | [`lis_tests` suite](https://github.com/norvig/pytudes/blob/60168bce8cdfacf57c92a5b2979f0b2e95367753/py/lispytest.py#L5)
17 | from `norvig/lispytest.py`,
18 | and additional separate tests for each expression and special form handled by `evaluate`.
19 |
--------------------------------------------------------------------------------
/original/norvig/py2.7/README.md:
--------------------------------------------------------------------------------
1 | # lis.py: Python 2.7 edition
2 |
3 | The _lis.py_ interpreter in this directory is the one described in (and linked from) Peter Norvig's post
4 | [_(How to Write a (Lisp) Interpreter (in Python))_](https://norvig.com/lispy.html).
5 |
6 | This version of _lis.py_ requires Python 2.7 because it uses `raw_input` (now named `input`) and `apply` which was removed in Python 3 as it became redundant with the evolution of Python's syntax
7 | to allow `function(*args, **kwargs)` instead of `apply(function, args, kwargs)`.
8 |
9 | On the other hand, the Python 2.7 _lis.py_ implements the `set!` special form which the Python 3 _lis.py_ doesn't have—but the more advanced _lispy.py_ does.
10 |
--------------------------------------------------------------------------------
/original/py3.10/README.md:
--------------------------------------------------------------------------------
1 | # Changes for Python 3.10
2 |
3 | This is a variation of the Python 3.9
4 | [version](../py3.9/) of `lis.py`,
5 | to demonstrate some features of Python 3.10.
6 |
7 | The most important change is using pattern matching in `evaluate()`.
8 | Language processing is great to show why `match/case`
9 | is much more than a `switch` statement.
10 | To showcase nested list destructuring in `case`,
11 | I added support to the `(define (fun parm...) body)` special form.
12 |
13 | Other Python 3.10 features used:
14 |
15 | * union types written as `A | B` instead of `Union[A, B]`;
16 | * explicit type alias definitions with `typing.TypeAlias`.
17 |
18 |
19 | _LR_
20 |
21 | São Paulo, June 19, 2021
22 |
--------------------------------------------------------------------------------
/original/py3.10/examples_test.py:
--------------------------------------------------------------------------------
1 | import math
2 |
3 | from lis import run
4 |
5 |
6 | fact_src = """
7 | (define (! n)
8 | (if (< n 2)
9 | 1
10 | (* n (! (- n 1)))
11 | )
12 | )
13 | (! 42)
14 | """
15 | def test_factorial():
16 | got = run(fact_src)
17 | assert got == 1405006117752879898543142606244511569936384000000000
18 | assert got == math.factorial(42)
19 |
20 |
21 | gcd_src = """
22 | (define (mod m n)
23 | (- m (* n (quotient m n))))
24 | (define (gcd m n)
25 | (if (= n 0)
26 | m
27 | (gcd n (mod m n))))
28 | (gcd 18 45)
29 | """
30 | def test_gcd():
31 | got = run(gcd_src)
32 | assert got == 9
33 |
34 |
35 | quicksort_src = """
36 | (define (quicksort lst)
37 | (if (null? lst)
38 | lst
39 | (begin
40 | (define pivot (car lst))
41 | (define rest (cdr lst))
42 | (append
43 | (quicksort
44 | (filter (lambda (x) (< x pivot)) rest))
45 | (list pivot)
46 | (quicksort
47 | (filter (lambda (x) (>= x pivot)) rest)))
48 | )
49 | )
50 | )
51 | (quicksort (list 2 1 6 3 4 0 8 9 7 5))
52 | """
53 | def test_quicksort():
54 | got = run(quicksort_src)
55 | assert got == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
56 |
57 |
58 | # Example from Structure and Interpretation of Computer Programs
59 | # https://mitpress.mit.edu/sites/default/files/sicp/full-text/sicp/book/node12.html
60 |
61 | newton_src = """
62 | (define (sqrt x)
63 | (sqrt-iter 1.0 x))
64 | (define (sqrt-iter guess x)
65 | (if (good-enough? guess x)
66 | guess
67 | (sqrt-iter (improve guess x) x)))
68 | (define (good-enough? guess x)
69 | (< (abs (- (* guess guess) x)) 0.001))
70 | (define (improve guess x)
71 | (average guess (/ x guess)))
72 | (define (average x y)
73 | (/ (+ x y) 2))
74 | (sqrt 123454321)
75 | """
76 | def test_newton():
77 | got = run(newton_src)
78 | assert math.isclose(got, 11111)
79 |
80 |
81 | closure_src = """
82 | (define (make-adder increment)
83 | (lambda (x) (+ increment x))
84 | )
85 | (define inc (make-adder 1))
86 | (inc 99)
87 | """
88 | def test_newton():
89 | got = run(closure_src)
90 | assert got == 100
91 |
--------------------------------------------------------------------------------
/original/py3.10/meta_test.py:
--------------------------------------------------------------------------------
1 | import operator as op
2 |
3 | from lis import run
4 |
5 | env_scm = """
6 | (define standard-env (list
7 | (list (quote +) +)
8 | (list (quote -) -)
9 | ))
10 | standard-env
11 | """
12 |
13 | def test_env_build():
14 | got = run(env_scm)
15 | assert got == [['+', op.add], ['-', op.sub]]
16 |
17 | scan_scm = """
18 | (define l (quote (a b c)))
19 | (define (scan what where)
20 | (if (null? where)
21 | (quote ())
22 | (if (eq? what (car where))
23 | what
24 | (scan what (cdr where))))
25 | )
26 | """
27 |
28 | def test_scan():
29 | source = scan_scm + '(scan (quote a) l )'
30 | got = run(source)
31 | assert got == 'a'
32 |
33 |
34 | def test_scan_not_found():
35 | source = scan_scm + '(scan (quote z) l )'
36 | got = run(source)
37 | assert got == []
38 |
39 |
40 | lookup_scm = """
41 | (define env (list
42 | (list (quote +) +)
43 | (list (quote -) -)
44 | ))
45 | (define (lookup what where)
46 | (if (null? where)
47 | (quote ())
48 | (if (eq? what (car (car where)))
49 | (car (cdr (car where)))
50 | (lookup what (cdr where))))
51 | )
52 | """
53 |
54 | def test_lookup():
55 | source = lookup_scm + '(lookup (quote +) env)'
56 | got = run(source)
57 | assert got == op.add
58 |
59 |
60 | def test_lookup_not_found():
61 | source = lookup_scm + '(lookup (quote z) env )'
62 | got = run(source)
63 | assert got == []
64 |
65 |
--------------------------------------------------------------------------------
/original/py3.10/mypy.ini:
--------------------------------------------------------------------------------
1 | [mypy]
2 | python_version = 3.10
3 |
--------------------------------------------------------------------------------
/original/py3.9/README.md:
--------------------------------------------------------------------------------
1 | # Changes from Norvig's code
2 |
3 | Peter Norvig's [`lis.py`](../norvig/) code is beautiful.
4 |
5 | I made a few changes for didactic reasons.
6 | These were the most important changes:
7 |
8 | * added type hints;
9 | * in `Procedure.__call__`, changed `env` to `local_env`;
10 | * removed module level `global_env`;
11 | * renamed function `atom` to `parse_atom`;
12 | * renamed function `eval` to `evaluate`;
13 | * in `evaluate`, made `env` parameter mandatory.
14 |
15 | I also rewrote Norvig's custom `lispytest.py` script as
16 | `lis_test.py`, to run with [pytest](https://docs.pytest.org).
17 | My rewrite includes Norvig's original
18 | [`lis_tests` suite](https://github.com/norvig/pytudes/blob/60168bce8cdfacf57c92a5b2979f0b2e95367753/py/lispytest.py#L5)
19 | adds a few more test cases.
20 |
21 | _LR_
22 | São Paulo, June 19, 2021
23 |
--------------------------------------------------------------------------------
/original/py3.9/mypy.ini:
--------------------------------------------------------------------------------
1 | [mypy]
2 | python_version = 3.9
3 |
--------------------------------------------------------------------------------
/pytest.ini:
--------------------------------------------------------------------------------
1 | [pytest]
2 | addopts = --doctest-modules
3 | markers =
4 | slow: marks test as slow (see conftest.py)
5 | tail_call_optimization: enable or disable TCO
6 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/bigloo/book.bgl:
--------------------------------------------------------------------------------
1 | ;;; $Id: book.bgl,v 1.9 1994/08/26 19:19:14 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; This file defines an interpreter containing all that is needed to
13 | ;;; interpret the programs of C. Queinnec's book. This file is to be
14 | ;;; compiled by Bigloo. It contains Meroonet, the syntax-case package
15 | ;;; of Hieb and Dybvig, and the tester utility to run test suites.
16 |
17 | (module book (main main)
18 | ;; Import the run-time of this interpreter (split into 2 files
19 | ;; since it was too large to be compiled easily)
20 | (import (rtbook "bigloo/rtbook.bgl")
21 | (rtbookp "bigloo/rtbook+.bgl") ) )
22 |
23 | ;;; Start the interpreter. In fact everything comes from the rtbook
24 | ;;; module which is separately compiled in order to provide a library
25 | ;;; which can be linked with other programs. See an example with the
26 | ;;; test.chap6a.bgl entry of the Makefile.
27 |
28 | (define (main options)
29 | (format #f "") ; forces rtbookp to be linked
30 | (start options) )
31 |
32 | ;;; end of book.bgl
33 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/bigloo/others/compat.ss:
--------------------------------------------------------------------------------
1 | ;;; compat.ss
2 | ;;; Robert Hieb & Kent Dybvig
3 | ;;; 92/06/18
4 |
5 | ;;; This file contains nonstandard help procedures.
6 | ;;; They are all present in Chez Scheme, but are easily defined
7 | ;;; in any standard Scheme system.
8 | ;;; These versions do no error checking.
9 |
10 | ; In Chez Scheme "(void)" returns an object that is ignored by the
11 | ; REP loop. It is returned whenever a "nonspecified" value is specified
12 | ; by the standard. The following should pick up an appropriate value.
13 |
14 | (define void
15 | (let ((void-object (if #f #f)))
16 | (lambda () void-object)))
17 |
18 | ; "andmap" is like "map" except instead of "consing" the results it
19 | ; "ands" them, quitting early if #f" is obtained.
20 | ; The following does no error checking.
21 |
22 | (define andmap
23 | (lambda (f first . rest)
24 | (cond
25 | ((null? rest)
26 | (let mapf ((l first))
27 | (or (null? l)
28 | (and (f (car l)) (mapf (cdr l))))))
29 | ((null? (cdr rest))
30 | (let mapf ((l1 first) (l2 (car rest)))
31 | (or (null? l1)
32 | (and (f (car l1) (car l2)) (mapf (cdr l1) (cdr l2))))))
33 | (else
34 | (let mapf ((first first) (rest rest))
35 | (or (null? first)
36 | (and (apply f (car first) (map car rest))
37 | (mapf (cdr first) (map cdr rest)))))))))
38 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/bigloo/others/hooks.bgl:
--------------------------------------------------------------------------------
1 | ;;; $Id: hooks.bgl,v 1.1 1994/07/17 13:55:52 queinnec Exp $
2 | ;;; hooks.ss
3 | ;;; Robert Hieb & Kent Dybvig
4 | ;;; 92/06/18
5 |
6 | ;;; adapted for Bigloo by Christian Queinnec
7 |
8 | (define eval-hook eval)
9 |
10 | (define expand-install-hook
11 | (lambda (expand)
12 | (error-hook 'expand-install-hook "Not yet implemented" 34) ) )
13 |
14 | ;;; In Chez Scheme, the following reports:
15 | ;;; "Error in : ."
16 | ;;; "who" is a symbol, "why" is a string, and "what" is an arbitrary object.
17 |
18 | (define error-hook error)
19 |
20 | ;;; New symbols are used to generate non-capturing bindings. If it is
21 | ;;; impossible to generate unique symbols, output identifiers during
22 | ;;; expansion and either feed the result directly into the compiler or
23 | ;;; make another pass to perform alpha substitution.
24 |
25 | (define new-symbol-hook gensym)
26 |
27 | ;;; "put-global-definition-hook" should overwrite existing definitions.
28 |
29 | (define put-global-definition-hook
30 | (lambda (symbol binding)
31 | (putprop! symbol '*macro-transformer* binding)))
32 |
33 | ;;; "get-global-definition-hook" should return "#f" if no binding
34 | ;;; has been established "put-global-definition-hook" for the symbol.
35 |
36 | (define get-global-definition-hook
37 | (lambda (symbol)
38 | (getprop symbol '*macro-transformer*)))
39 |
40 | ;;; end of hooks.bgl
41 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/bigloo/others/init.ss:
--------------------------------------------------------------------------------
1 | ;;; init.ss
2 | ;;; Robert Hieb & Kent Dybvig
3 | ;;; 92/06/18
4 |
5 | ; These initializations are done here rather than "expand.ss" so that
6 | ; "expand.ss" can be loaded twice (for bootstrapping purposes).
7 |
8 | (define expand-syntax #f)
9 | (define syntax-dispatch #f)
10 | (define generate-temporaries #f)
11 | (define identifier? #f)
12 | (define syntax-error #f)
13 | (define syntax-object->datum #f)
14 | (define bound-identifier=? #f)
15 | (define free-identifier=? #f)
16 | (define install-global-transformer #f)
17 | (define implicit-identifier #f)
18 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/elk/others/compat.ss:
--------------------------------------------------------------------------------
1 | ;;; compat.ss
2 | ;;; Robert Hieb & Kent Dybvig
3 | ;;; 92/06/18
4 |
5 | ;;; This file contains nonstandard help procedures.
6 | ;;; They are all present in Chez Scheme, but are easily defined
7 | ;;; in any standard Scheme system.
8 | ;;; These versions do no error checking.
9 |
10 | ; In Chez Scheme "(void)" returns an object that is ignored by the
11 | ; REP loop. It is returned whenever a "nonspecified" value is specified
12 | ; by the standard. The following should pick up an appropriate value.
13 |
14 | (define void
15 | (let ((void-object (if #f #f)))
16 | (lambda () void-object)))
17 |
18 | ; "andmap" is like "map" except instead of "consing" the results it
19 | ; "ands" them, quitting early if #f" is obtained.
20 | ; The following does no error checking.
21 |
22 | (define andmap
23 | (lambda (f first . rest)
24 | (cond
25 | ((null? rest)
26 | (let mapf ((l first))
27 | (or (null? l)
28 | (and (f (car l)) (mapf (cdr l))))))
29 | ((null? (cdr rest))
30 | (let mapf ((l1 first) (l2 (car rest)))
31 | (or (null? l1)
32 | (and (f (car l1) (car l2)) (mapf (cdr l1) (cdr l2))))))
33 | (else
34 | (let mapf ((first first) (rest rest))
35 | (or (null? first)
36 | (and (apply f (car first) (map car rest))
37 | (mapf (cdr first) (map cdr rest)))))))))
38 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/elk/others/hooks.elk:
--------------------------------------------------------------------------------
1 | ;;; $Id: hooks.elk,v 1.1 1996/01/29 19:43:46 queinnec Exp $
2 | ;;; hooks.ss
3 | ;;; Robert Hieb & Kent Dybvig
4 | ;;; 92/06/18
5 |
6 | ;;; adapted for Elk by Christian Queinnec
7 |
8 | (define (eval-hook e)
9 | (eval e top-level-environment) )
10 |
11 | (define expand-install-hook
12 | (lambda (expand)
13 | (error-hook 'expand-install-hook "Not yet implemented" 34) ) )
14 |
15 | ;;; In Chez Scheme, the following reports:
16 | ;;; "Error in : ."
17 | ;;; "who" is a symbol, "why" is a string, and "what" is an arbitrary object.
18 |
19 | (define error-hook error)
20 |
21 | ;;; New symbols are used to generate non-capturing bindings. If it is
22 | ;;; impossible to generate unique symbols, output identifiers during
23 | ;;; expansion and either feed the result directly into the compiler or
24 | ;;; make another pass to perform alpha substitution.
25 |
26 | (define new-symbol-hook gensym)
27 |
28 | ;;; "put-global-definition-hook" should overwrite existing definitions.
29 |
30 | (define put-global-definition-hook
31 | (lambda (symbol binding)
32 | (put symbol '*macro-transformer* binding)))
33 |
34 | ;;; "get-global-definition-hook" should return "#f" if no binding
35 | ;;; has been established "put-global-definition-hook" for the symbol.
36 |
37 | (define get-global-definition-hook
38 | (lambda (symbol)
39 | (get symbol '*macro-transformer*)))
40 |
41 | ;;; end of hooks.elk
42 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/elk/others/init.ss:
--------------------------------------------------------------------------------
1 | ;;; init.ss
2 | ;;; Robert Hieb & Kent Dybvig
3 | ;;; 92/06/18
4 |
5 | ; These initializations are done here rather than "expand.ss" so that
6 | ; "expand.ss" can be loaded twice (for bootstrapping purposes).
7 |
8 | (define expand-syntax #f)
9 | (define syntax-dispatch #f)
10 | (define generate-temporaries #f)
11 | (define identifier? #f)
12 | (define syntax-error #f)
13 | (define syntax-object->datum #f)
14 | (define bound-identifier=? #f)
15 | (define free-identifier=? #f)
16 | (define install-global-transformer #f)
17 | (define implicit-identifier #f)
18 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/elk/others/output.ss:
--------------------------------------------------------------------------------
1 | ;;; output.ss
2 | ;;; Robert Hieb & Kent Dybvig
3 | ;;; 92/06/18
4 |
5 | ; The output routines can be tailored to feed a specific system or compiler.
6 | ; They are set up here to generate the following subset of standard Scheme:
7 |
8 | ; :==
9 | ; |
10 | ; | (set! )
11 | ; | (define )
12 | ; | (lambda (*) )
13 | ; | (lambda )
14 | ; | (lambda (+ . ) )
15 | ; | (letrec (+) )
16 | ; | (if )
17 | ; | (begin )
18 | ; | (quote )
19 | ; :== (+)
20 | ; :== ( )
21 | ; :==
22 |
23 | ; Definitions are generated only at top level.
24 |
25 | (define build-application
26 | (lambda (fun-exp arg-exps)
27 | `(,fun-exp ,@arg-exps)))
28 |
29 | (define build-conditional
30 | (lambda (test-exp then-exp else-exp)
31 | `(if ,test-exp ,then-exp ,else-exp)))
32 |
33 | (define build-lexical-reference (lambda (var) var))
34 |
35 | (define build-lexical-assignment
36 | (lambda (var exp)
37 | `(set! ,var ,exp)))
38 |
39 | (define build-global-reference (lambda (var) var))
40 |
41 | (define build-global-assignment
42 | (lambda (var exp)
43 | `(set! ,var ,exp)))
44 |
45 | (define build-lambda
46 | (lambda (vars exp)
47 | `(lambda ,vars ,exp)))
48 |
49 | (define build-improper-lambda
50 | (lambda (vars var exp)
51 | `(lambda (,@vars . ,var) ,exp)))
52 |
53 | (define build-data
54 | (lambda (exp)
55 | `(quote ,exp)))
56 |
57 | (define build-identifier
58 | (lambda (id)
59 | `(quote ,id)))
60 |
61 | (define build-sequence
62 | (lambda (exps)
63 | (if (null? (cdr exps))
64 | (car exps)
65 | `(begin ,(car exps) ,(build-sequence (cdr exps))))))
66 |
67 | (define build-letrec
68 | (lambda (vars val-exps body-exp)
69 | (if (null? vars)
70 | body-exp
71 | `(letrec ,(map list vars val-exps) ,body-exp))))
72 |
73 | (define build-global-definition
74 | (lambda (var val)
75 | `(define ,var ,val)))
76 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/gambit/compat.ss:
--------------------------------------------------------------------------------
1 | ;;; compat.ss
2 | ;;; Robert Hieb & Kent Dybvig
3 | ;;; 92/06/18
4 |
5 | ;;; This file contains nonstandard help procedures.
6 | ;;; They are all present in Chez Scheme, but are easily defined
7 | ;;; in any standard Scheme system.
8 | ;;; These versions do no error checking.
9 |
10 | ; In Chez Scheme "(void)" returns an object that is ignored by the
11 | ; REP loop. It is returned whenever a "nonspecified" value is specified
12 | ; by the standard. The following should pick up an appropriate value.
13 |
14 | (define void
15 | (let ((void-object (if #f #f)))
16 | (lambda () void-object)))
17 |
18 | ; "andmap" is like "map" except instead of "consing" the results it
19 | ; "ands" them, quitting early if #f" is obtained.
20 | ; The following does no error checking.
21 |
22 | (define andmap
23 | (lambda (f first . rest)
24 | (cond
25 | ((null? rest)
26 | (let mapf ((l first))
27 | (or (null? l)
28 | (and (f (car l)) (mapf (cdr l))))))
29 | ((null? (cdr rest))
30 | (let mapf ((l1 first) (l2 (car rest)))
31 | (or (null? l1)
32 | (and (f (car l1) (car l2)) (mapf (cdr l1) (cdr l2))))))
33 | (else
34 | (let mapf ((first first) (rest rest))
35 | (or (null? first)
36 | (and (apply f (car first) (map car rest))
37 | (mapf (cdr first) (map cdr rest)))))))))
38 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/gambit/hooks.gsi:
--------------------------------------------------------------------------------
1 | ;;; $Id: hooks.gsi,v 1.2 1996/01/29 19:10:07 queinnec Exp queinnec $
2 | ;;; hooks.ss
3 | ;;; Robert Hieb & Kent Dybvig
4 | ;;; 92/06/18
5 |
6 | ;;; adapted for Gambit by Christian Queinnec
7 |
8 | (define eval-hook eval)
9 |
10 | (define expand-install-hook
11 | (lambda (expand)
12 | (error-hook 'expand-install-hook "Not yet implemented" 34) ) )
13 |
14 | ;;; In Chez Scheme, the following reports:
15 | ;;; "Error in : ."
16 | ;;; "who" is a symbol, "why" is a string, and "what" is an arbitrary object.
17 | ;;; error-hook is defined in book.scm
18 |
19 | ;(define error-hook syntax-case-error)
20 |
21 | ;;; New symbols are used to generate non-capturing bindings. If it is
22 | ;;; impossible to generate unique symbols, output identifiers during
23 | ;;; expansion and either feed the result directly into the compiler or
24 | ;;; make another pass to perform alpha substitution.
25 |
26 | (define (new-symbol-hook x)
27 | (gensym (cond ((symbol? x) x)
28 | ((string? x) (string->symbol x))
29 | (else 'g) )) )
30 |
31 | ;;; "put-global-definition-hook" should overwrite existing definitions.
32 |
33 | (define put-global-definition-hook
34 | (lambda (symbol binding)
35 | (put symbol '*macro-transformer* binding)))
36 |
37 | ;;; "get-global-definition-hook" should return "#f" if no binding
38 | ;;; has been established "put-global-definition-hook" for the symbol.
39 |
40 | (define get-global-definition-hook
41 | (lambda (symbol)
42 | (get symbol '*macro-transformer*)))
43 |
44 | ;;; end of hooks.gsi
45 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/gambit/init.ss:
--------------------------------------------------------------------------------
1 | ;;; init.ss
2 | ;;; Robert Hieb & Kent Dybvig
3 | ;;; 92/06/18
4 |
5 | ; These initializations are done here rather than "expand.ss" so that
6 | ; "expand.ss" can be loaded twice (for bootstrapping purposes).
7 |
8 | (define expand-syntax #f)
9 | (define syntax-dispatch #f)
10 | (define generate-temporaries #f)
11 | (define identifier? #f)
12 | (define syntax-error #f)
13 | (define syntax-object->datum #f)
14 | (define bound-identifier=? #f)
15 | (define free-identifier=? #f)
16 | (define install-global-transformer #f)
17 | (define implicit-identifier #f)
18 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/gambit/output.ss:
--------------------------------------------------------------------------------
1 | ;;; output.ss
2 | ;;; Robert Hieb & Kent Dybvig
3 | ;;; 92/06/18
4 |
5 | ; The output routines can be tailored to feed a specific system or compiler.
6 | ; They are set up here to generate the following subset of standard Scheme:
7 |
8 | ; :==
9 | ; |
10 | ; | (set! )
11 | ; | (define )
12 | ; | (lambda (*) )
13 | ; | (lambda )
14 | ; | (lambda (+ . ) )
15 | ; | (letrec (+) )
16 | ; | (if )
17 | ; | (begin )
18 | ; | (quote )
19 | ; :== (+)
20 | ; :== ( )
21 | ; :==
22 |
23 | ; Definitions are generated only at top level.
24 |
25 | (define build-application
26 | (lambda (fun-exp arg-exps)
27 | `(,fun-exp ,@arg-exps)))
28 |
29 | (define build-conditional
30 | (lambda (test-exp then-exp else-exp)
31 | `(if ,test-exp ,then-exp ,else-exp)))
32 |
33 | (define build-lexical-reference (lambda (var) var))
34 |
35 | (define build-lexical-assignment
36 | (lambda (var exp)
37 | `(set! ,var ,exp)))
38 |
39 | (define build-global-reference (lambda (var) var))
40 |
41 | (define build-global-assignment
42 | (lambda (var exp)
43 | `(set! ,var ,exp)))
44 |
45 | (define build-lambda
46 | (lambda (vars exp)
47 | `(lambda ,vars ,exp)))
48 |
49 | (define build-improper-lambda
50 | (lambda (vars var exp)
51 | `(lambda (,@vars . ,var) ,exp)))
52 |
53 | (define build-data
54 | (lambda (exp)
55 | `(quote ,exp)))
56 |
57 | (define build-identifier
58 | (lambda (id)
59 | `(quote ,id)))
60 |
61 | (define build-sequence
62 | (lambda (exps)
63 | (if (null? (cdr exps))
64 | (car exps)
65 | `(begin ,(car exps) ,(build-sequence (cdr exps))))))
66 |
67 | (define build-letrec
68 | (lambda (vars val-exps body-exp)
69 | (if (null? vars)
70 | body-exp
71 | `(letrec ,(map list vars val-exps) ,body-exp))))
72 |
73 | (define build-global-definition
74 | (lambda (var val)
75 | `(define ,var ,val)))
76 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/meroonet/variante1.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: variante1.scm,v 1.5 2006/11/25 17:44:13 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; This file is part of the Meroonet package.
13 |
14 | ;;; VARIANT 1: a simple-minded define-class
15 |
16 | ;;; a define-class that works only for interpreters, the class is
17 | ;;; created at macroexpansion time and grafted to the inheritance tree
18 | ;;; at that same time. The version in Meroonet is safer.
19 |
20 | (define-meroonet-macro (define-class name super-name
21 | own-field-descriptions )
22 | (let ((class (register-class name super-name
23 | own-field-descriptions )))
24 | (Class-generate-related-names class) ) )
25 |
26 | ;;; end of variante1.scm
27 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/meroonet/variante4.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: variante4.scm,v 1.5 2006/11/25 17:43:56 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; This file is part of the Meroonet package.
13 |
14 | ;;; VARIANT 4: enhanced(?) define-method
15 |
16 | ;;; This ne define-method a la CLOS creates a generic function on the
17 | ;;; fly if it does not exist yet. The sole problem is to do it a
18 | ;;; evaluation-time and not at macro-expansion time.
19 |
20 | (define-meroonet-macro (define-method call . body)
21 | (parse-variable-specifications
22 | (cdr call)
23 | (lambda (discriminant variables)
24 | (let ((g (gensym))(c (gensym))) ; make g and c hygienic
25 | `(begin
26 | (unless (->Generic ',(car call))
27 | (define-generic ,call) ) ; new
28 | (register-method
29 | ',(car call)
30 | (lambda (,g ,c)
31 | (lambda ,(flat-variables variables)
32 | (define (call-next-method)
33 | ((if (Class-superclass ,c)
34 | (vector-ref (Generic-dispatch-table ,g)
35 | (Class-number
36 | (Class-superclass ,c) ) )
37 | (Generic-default ,g) )
38 | . ,(flat-variables variables) ) )
39 | . ,body ) )
40 | ',(cadr discriminant)
41 | ',(cdr call) ) ) ) ) ) )
42 |
43 | ;;; I am not really fond of that but we can nevertheless test the
44 | ;;; previous definition.
45 |
46 | (define-method (ugly a (b Class) . c)
47 | 'barf )
48 |
49 | (unless (eq? 'barf (ugly 2 Field-class))
50 | (meroonet-error "Failed test on define-method") )
51 |
52 | ;;; end of variante4.scm
53 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/mitscheme/others/compat.ss:
--------------------------------------------------------------------------------
1 | ;;; compat.ss
2 | ;;; Robert Hieb & Kent Dybvig
3 | ;;; 92/06/18
4 |
5 | ;;; This file contains nonstandard help procedures.
6 | ;;; They are all present in Chez Scheme, but are easily defined
7 | ;;; in any standard Scheme system.
8 | ;;; These versions do no error checking.
9 |
10 | ; In Chez Scheme "(void)" returns an object that is ignored by the
11 | ; REP loop. It is returned whenever a "nonspecified" value is specified
12 | ; by the standard. The following should pick up an appropriate value.
13 |
14 | (define void
15 | (let ((void-object (if #f #f)))
16 | (lambda () void-object)))
17 |
18 | ; "andmap" is like "map" except instead of "consing" the results it
19 | ; "ands" them, quitting early if #f" is obtained.
20 | ; The following does no error checking.
21 |
22 | (define andmap
23 | (lambda (f first . rest)
24 | (cond
25 | ((null? rest)
26 | (let mapf ((l first))
27 | (or (null? l)
28 | (and (f (car l)) (mapf (cdr l))))))
29 | ((null? (cdr rest))
30 | (let mapf ((l1 first) (l2 (car rest)))
31 | (or (null? l1)
32 | (and (f (car l1) (car l2)) (mapf (cdr l1) (cdr l2))))))
33 | (else
34 | (let mapf ((first first) (rest rest))
35 | (or (null? first)
36 | (and (apply f (car first) (map car rest))
37 | (mapf (cdr first) (map cdr rest)))))))))
38 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/mitscheme/others/hooks.mit:
--------------------------------------------------------------------------------
1 | ;;; $Id: hooks.bgl,v 1.1 1994/07/17 13:55:52 queinnec Exp $
2 | ;;; hooks.ss
3 | ;;; Robert Hieb & Kent Dybvig
4 | ;;; 92/06/18
5 |
6 | ;;; adapted for Bigloo by Christian Queinnec
7 |
8 | (define (eval-hook e)
9 | (eval e user-initial-environment) )
10 |
11 | (define expand-install-hook
12 | (lambda (expand)
13 | (error-hook 'expand-install-hook "Not yet implemented" 34) ) )
14 |
15 | ;;; In Chez Scheme, the following reports:
16 | ;;; "Error in : ."
17 | ;;; "who" is a symbol, "why" is a string, and "what" is an arbitrary object.
18 |
19 | (define error-hook error)
20 |
21 | ;;; New symbols are used to generate non-capturing bindings. If it is
22 | ;;; impossible to generate unique symbols, output identifiers during
23 | ;;; expansion and either feed the result directly into the compiler or
24 | ;;; make another pass to perform alpha substitution.
25 |
26 | (define new-symbol-hook gensym)
27 |
28 | ;;; "put-global-definition-hook" should overwrite existing definitions.
29 |
30 | (define put-global-definition-hook
31 | (lambda (symbol binding)
32 | (putprop symbol '*macro-transformer* binding)))
33 |
34 | ;;; "get-global-definition-hook" should return "#f" if no binding
35 | ;;; has been established "put-global-definition-hook" for the symbol.
36 |
37 | (define get-global-definition-hook
38 | (lambda (symbol)
39 | (getprop symbol '*macro-transformer*)))
40 |
41 | ;;; end of hooks.bgl
42 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/mitscheme/others/init.ss:
--------------------------------------------------------------------------------
1 | ;;; init.ss
2 | ;;; Robert Hieb & Kent Dybvig
3 | ;;; 92/06/18
4 |
5 | ; These initializations are done here rather than "expand.ss" so that
6 | ; "expand.ss" can be loaded twice (for bootstrapping purposes).
7 |
8 | (define expand-syntax #f)
9 | (define syntax-dispatch #f)
10 | (define generate-temporaries #f)
11 | (define identifier? #f)
12 | (define syntax-error #f)
13 | (define syntax-object->datum #f)
14 | (define bound-identifier=? #f)
15 | (define free-identifier=? #f)
16 | (define install-global-transformer #f)
17 | (define implicit-identifier #f)
18 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/perl/check.prl:
--------------------------------------------------------------------------------
1 | #! /usr/local/bin/perl
2 | # Check if a test is correct.
3 | # Syntax: check.prl resulting-file test-name
4 | $usage = "Usage: check.prl resulting-file test-name" ;
5 | # Given the name of the test, this script tries to know if the results are
6 | # correct. Given tester.scm, single tests are correct but the interpreter
7 | # stops as soon as it encounters an error. The name of the test allows to
8 | # know what sort of test it is.
9 |
10 | # Main:
11 | if ( @ARGV == 2 ) {
12 | ($file,$test) = @ARGV ;
13 | } else {
14 | die $usage ;
15 | }
16 |
17 | $_ = $test ;
18 | # If the name of the test is normal then all tests should be done.
19 | # In that case tester returns the symbol DONE.
20 | $string = "= done" ;
21 | # This is the case for tests that share allocation frame as in Lisp
22 | $string = "The following test forces a continuation to return multiply"
23 | if /shared\./ ;
24 | # This is for shallow-binding in presence of Scheme continuation
25 | $string = "shallow binding without unwind-protect when escaping"
26 | if /shallow\./ ;
27 | # eval as a function fails to access the current lexical environment
28 | $string = "eval as a function will fail"
29 | if /evalf\./ ;
30 | # escape a la CommonLisp fail are not full Scheme continuations.
31 | $string = "(to return multiply|out of their dynamic extent)"
32 | if /dynext\./ ;
33 | # Don't inspect the result of a test prefixed by no. A no.test is
34 | # generally temporary, it is in the course of being fixed.
35 | # a test prefixed by loop can only be done (and killed) by hand.
36 | # A test prefixed by big may fail because of memory.
37 | if ( /(no|loop|big)\./ ) {
38 | print "\n\n\t\t$test is SKIPPED\n\n" ;
39 | exit 0 ;
40 | };
41 |
42 | # Scan the file until finding the string and exit accordingly
43 | open(RESULT,"<$file");
44 | while ( ) {
45 | if ( /$string/i ) {
46 | print "\n\n\t\t$test is SUCCESSFUL\n\n" ;
47 | exit 0 ;
48 | }
49 | }
50 | # If we arrive there then the test was wrong.
51 | print "\n\n\t\tFAILING $test \n\n" ;
52 | exit 1 ;
53 |
54 | # end of check.prl
55 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/scheme2c/others/compat.ss:
--------------------------------------------------------------------------------
1 | ;;; compat.ss
2 | ;;; Robert Hieb & Kent Dybvig
3 | ;;; 92/06/18
4 |
5 | ;;; This file contains nonstandard help procedures.
6 | ;;; They are all present in Chez Scheme, but are easily defined
7 | ;;; in any standard Scheme system.
8 | ;;; These versions do no error checking.
9 |
10 | ; In Chez Scheme "(void)" returns an object that is ignored by the
11 | ; REP loop. It is returned whenever a "nonspecified" value is specified
12 | ; by the standard. The following should pick up an appropriate value.
13 |
14 | (define void
15 | (let ((void-object (if #f #f)))
16 | (lambda () void-object)))
17 |
18 | ; "andmap" is like "map" except instead of "consing" the results it
19 | ; "ands" them, quitting early if #f" is obtained.
20 | ; The following does no error checking.
21 |
22 | (define andmap
23 | (lambda (f first . rest)
24 | (cond
25 | ((null? rest)
26 | (let mapf ((l first))
27 | (or (null? l)
28 | (and (f (car l)) (mapf (cdr l))))))
29 | ((null? (cdr rest))
30 | (let mapf ((l1 first) (l2 (car rest)))
31 | (or (null? l1)
32 | (and (f (car l1) (car l2)) (mapf (cdr l1) (cdr l2))))))
33 | (else
34 | (let mapf ((first first) (rest rest))
35 | (or (null? first)
36 | (and (apply f (car first) (map car rest))
37 | (mapf (cdr first) (map cdr rest)))))))))
38 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/scheme2c/others/hooks.sc:
--------------------------------------------------------------------------------
1 | ;;; $Id: hooks.sc,v 1.2 1994/07/17 18:33:19 queinnec Exp $
2 | ;;; hooks.ss
3 | ;;; Robert Hieb & Kent Dybvig
4 | ;;; 92/06/18
5 |
6 | ;;; adapted for Scheme->c by Christian Queinnec
7 |
8 | (define eval-hook eval)
9 |
10 | (define expand-install-hook
11 | (lambda (expand)
12 | (error-hook 'expand-install-hook "Not yet implemented" expand) ) )
13 |
14 | ;;; In Chez Scheme, the following reports:
15 | ;;; "Error in : ."
16 | ;;; "who" is a symbol, "why" is a string, and "what" is an arbitrary object.
17 |
18 | (define error-hook
19 | (lambda (who why what)
20 | (error who " ~A: ~A.~%" why what) ) )
21 |
22 | ;;; New symbols are used to generate non-capturing bindings. If it is
23 | ;;; impossible to generate unique symbols, output identifiers during
24 | ;;; expansion and either feed the result directly into the compiler or
25 | ;;; make another pass to perform alpha substitution.
26 |
27 | ;;; gensym does not exist in Scheme->C and has to be defined before
28 | ;;; this assignment since no error will be perceived in compiled code.
29 |
30 | (define new-symbol-hook gensym)
31 |
32 | ;;; "put-global-definition-hook" should overwrite existing definitions.
33 |
34 | (define put-global-definition-hook
35 | (lambda (symbol binding)
36 | (putprop symbol '*macro-transformer* binding)))
37 |
38 | ;;; "get-global-definition-hook" should return "#f" if no binding
39 | ;;; has been established "put-global-definition-hook" for the symbol.
40 |
41 | (define get-global-definition-hook
42 | (lambda (symbol)
43 | (getprop symbol '*macro-transformer*)))
44 |
45 | ;;; end of hooks.sc
46 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/scheme2c/others/init.ss:
--------------------------------------------------------------------------------
1 | ;;; init.ss
2 | ;;; Robert Hieb & Kent Dybvig
3 | ;;; 92/06/18
4 |
5 | ; These initializations are done here rather than "expand.ss" so that
6 | ; "expand.ss" can be loaded twice (for bootstrapping purposes).
7 |
8 | (define expand-syntax #f)
9 | (define syntax-dispatch #f)
10 | (define generate-temporaries #f)
11 | (define identifier? #f)
12 | (define syntax-error #f)
13 | (define syntax-object->datum #f)
14 | (define bound-identifier=? #f)
15 | (define free-identifier=? #f)
16 | (define install-global-transformer #f)
17 | (define implicit-identifier #f)
18 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/scm/others/compat.ss:
--------------------------------------------------------------------------------
1 | ;;; compat.ss
2 | ;;; Robert Hieb & Kent Dybvig
3 | ;;; 92/06/18
4 |
5 | ;;; This file contains nonstandard help procedures.
6 | ;;; They are all present in Chez Scheme, but are easily defined
7 | ;;; in any standard Scheme system.
8 | ;;; These versions do no error checking.
9 |
10 | ; In Chez Scheme "(void)" returns an object that is ignored by the
11 | ; REP loop. It is returned whenever a "nonspecified" value is specified
12 | ; by the standard. The following should pick up an appropriate value.
13 |
14 | (define void
15 | (let ((void-object (if #f #f)))
16 | (lambda () void-object)))
17 |
18 | ; "andmap" is like "map" except instead of "consing" the results it
19 | ; "ands" them, quitting early if #f" is obtained.
20 | ; The following does no error checking.
21 |
22 | (define andmap
23 | (lambda (f first . rest)
24 | (cond
25 | ((null? rest)
26 | (let mapf ((l first))
27 | (or (null? l)
28 | (and (f (car l)) (mapf (cdr l))))))
29 | ((null? (cdr rest))
30 | (let mapf ((l1 first) (l2 (car rest)))
31 | (or (null? l1)
32 | (and (f (car l1) (car l2)) (mapf (cdr l1) (cdr l2))))))
33 | (else
34 | (let mapf ((first first) (rest rest))
35 | (or (null? first)
36 | (and (apply f (car first) (map car rest))
37 | (mapf (cdr first) (map cdr rest)))))))))
38 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/scm/others/init.ss:
--------------------------------------------------------------------------------
1 | ;;; init.ss
2 | ;;; Robert Hieb & Kent Dybvig
3 | ;;; 92/06/18
4 |
5 | ; These initializations are done here rather than "expand.ss" so that
6 | ; "expand.ss" can be loaded twice (for bootstrapping purposes).
7 |
8 | (define expand-syntax #f)
9 | (define syntax-dispatch #f)
10 | (define generate-temporaries #f)
11 | (define identifier? #f)
12 | (define syntax-error #f)
13 | (define syntax-object->datum #f)
14 | (define bound-identifier=? #f)
15 | (define free-identifier=? #f)
16 | (define install-global-transformer #f)
17 | (define implicit-identifier #f)
18 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/scm/others/output.ss:
--------------------------------------------------------------------------------
1 | ;;; output.ss
2 | ;;; Robert Hieb & Kent Dybvig
3 | ;;; 92/06/18
4 |
5 | ; The output routines can be tailored to feed a specific system or compiler.
6 | ; They are set up here to generate the following subset of standard Scheme:
7 |
8 | ; :==
9 | ; |
10 | ; | (set! )
11 | ; | (define )
12 | ; | (lambda (*) )
13 | ; | (lambda )
14 | ; | (lambda (+ . ) )
15 | ; | (letrec (+) )
16 | ; | (if )
17 | ; | (begin )
18 | ; | (quote )
19 | ; :== (+)
20 | ; :== ( )
21 | ; :==
22 |
23 | ; Definitions are generated only at top level.
24 |
25 | (define build-application
26 | (lambda (fun-exp arg-exps)
27 | `(,fun-exp ,@arg-exps)))
28 |
29 | (define build-conditional
30 | (lambda (test-exp then-exp else-exp)
31 | `(if ,test-exp ,then-exp ,else-exp)))
32 |
33 | (define build-lexical-reference (lambda (var) var))
34 |
35 | (define build-lexical-assignment
36 | (lambda (var exp)
37 | `(set! ,var ,exp)))
38 |
39 | (define build-global-reference (lambda (var) var))
40 |
41 | (define build-global-assignment
42 | (lambda (var exp)
43 | `(set! ,var ,exp)))
44 |
45 | (define build-lambda
46 | (lambda (vars exp)
47 | `(lambda ,vars ,exp)))
48 |
49 | (define build-improper-lambda
50 | (lambda (vars var exp)
51 | `(lambda (,@vars . ,var) ,exp)))
52 |
53 | (define build-data
54 | (lambda (exp)
55 | `(quote ,exp)))
56 |
57 | (define build-identifier
58 | (lambda (id)
59 | `(quote ,id)))
60 |
61 | (define build-sequence
62 | (lambda (exps)
63 | (if (null? (cdr exps))
64 | (car exps)
65 | `(begin ,(car exps) ,(build-sequence (cdr exps))))))
66 |
67 | (define build-letrec
68 | (lambda (vars val-exps body-exp)
69 | (if (null? vars)
70 | body-exp
71 | `(letrec ,(map list vars val-exps) ,body-exp))))
72 |
73 | (define build-global-definition
74 | (lambda (var val)
75 | `(define ,var ,val)))
76 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/scm/others/prop.scm:
--------------------------------------------------------------------------------
1 | ;
2 | ; a trivial implementation of property list routines
3 | ; get, put, remprop oz/91
4 | ;
5 |
6 | (define *namelist* '()) ; top-level name/plst chain
7 | (define *lastlook* '(() ())) ; look-aside cache
8 |
9 | ; locate the name/plst pair
10 |
11 | (define (nameprop name)
12 | (if (eq? name (car *lastlook*))
13 | *lastlook*
14 | (let ((pair (assq name *namelist*)))
15 | (if pair
16 | (set! *lastlook* pair))
17 | pair)))
18 | ;
19 | ; get returns property value found or '() to indicate absence
20 | ; of said property. As a consequence, '() cannot be a property value.
21 | ;
22 | (define (get name prop)
23 | (let ((plst (nameprop name)))
24 | (if plst
25 | (let ((item (assq prop (cdr plst))))
26 | (if item
27 | (cdr item)
28 | '()))
29 | '())))
30 |
31 | (define (put name prop valu)
32 | (let ((plst (nameprop name)))
33 | (if plst ; found a property list
34 | (let ((item (assq prop (cdr plst))))
35 | (if item
36 | (set-cdr! item valu) ; replace the current prop value
37 | (begin
38 | (set! item (cons prop valu))
39 | (set-cdr! plst (cons item (cdr plst))))))
40 | (let ((item (cons prop valu)))
41 | (set! plst (cons item '())) ; create a new property list
42 | (set! *namelist* (cons (cons name plst) *namelist*)))))
43 | valu)
44 |
45 | (define (remq prop plst)
46 | (if (null? plst)
47 | '()
48 | (if (eq? prop (caar plst))
49 | (cdr plst)
50 | (let loop ((nlst plst))
51 | (if (null? (cdr nlst))
52 | plst
53 | (if (eq? prop (caadr nlst))
54 | (begin (set-cdr! nlst (cddr nlst))
55 | plst)
56 | (loop (cdr nlst))))))))
57 |
58 | (define (remprop name prop)
59 | (let ((plst (nameprop name)))
60 | (if plst
61 | (set-cdr! plst (remq prop (cdr plst)))
62 | '())))
63 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/si/bibexp.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: bibexp.scm,v 1.2 1994/08/21 19:35:32 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | (define (fact2 n) (if (= n 0) 1 (* n (fact2 (- n 1)))))
13 |
14 | (define-abbreviation (factorial n)
15 | (if (and (integer? n) (> n 0)) (fact2 n) `(fact1 ,n)) )
16 |
17 | ;;; end of bibexp.scm
18 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/si/bibrun.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: bibrun.scm,v 1.2 1994/08/21 19:35:32 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | (define (fact1 n) (if (= n 0) 1 (* n (fact1 (- n 1)))))
13 |
14 | ;;; end of bibrun.scm
15 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/si/chap9b.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap9b.scm,v 1.2 1994/08/21 19:35:32 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | (define (fact1 n) (if (= n 0) 1 (* n (fact1 (- n 1)))))
13 |
14 | (define-abbreviation (factorial n)
15 | (define (fact2 n) (if (= n 1) 1 (* n (fact2 (- n 1)))))
16 | (if (and (integer? n) (> n 0)) (fact2 n) `(fact1 ,n)) )
17 |
18 | (define (some-facts)
19 | (list (factorial 5) (factorial (+ 3 2))) )
20 |
21 | ;;; end of chap9b.scm
22 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/si/chap9c.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap9c.scm,v 1.2 1994/08/21 19:35:32 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | (define (fact n) (if (= n 0) 1 (* n (fact (- n 1)))))
13 |
14 | (define-abbreviation (factorial n)
15 | (if (and (integer? n) (> n 0)) (fact n) `(fact ,n)) )
16 |
17 | ;;; end of chap9c.scm
18 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/si/example.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id$
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | (set! fact
13 | ((lambda (fact) (lambda (n)
14 | (if (< n 0)
15 | "Toctoc la tete!"
16 | (fact n fact (lambda (x) x)) ) ))
17 | (lambda (n f k)
18 | (if (= n 0)
19 | (k 1)
20 | (f (- n 1) f (lambda (r) (k (* n r)))) ) ) ) )
21 |
22 | ;;; end of example.scm
23 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/si/fact.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id$
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; Test for chap7e.scm.
13 | ;;; Be above directories /si/ and /so/ then evaluate:
14 | ;;; (compile-file "si/fact")
15 | ;;; It generates the file si/fact.so that can be read.
16 |
17 | (set! fact
18 | (lambda (n)
19 | (if (= n 0) 1
20 | (* n (fact (- n 1))) ) ) )
21 |
22 | ;;;; end of fact.scm
23 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/si/fib.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id$
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; (compile-file "si/fib")
13 |
14 | (set! fib (lambda (n)
15 | (if (= n 0) 1
16 | (if (= n 1) 1
17 | (+ (fib (- n 1)) (fib (- n 2))) ) ) ))
18 |
19 | ;;; end of fib.scm
20 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/si/foo.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id$
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; This file introduces a lot of things to test.
13 | ;;; (compile-file "si/foo")
14 | ;;; (run-application 100 "si/foo")
15 |
16 | (set! foo '(a b))
17 |
18 | (set! bar
19 | (lambda (x)
20 | (cons x (dynamic x)) ) )
21 |
22 | (set! hux (dynamic-let (x '(c d))
23 | (dynamic-let (y 'foo)
24 | (dynamic-let (x foo)
25 | (bar 'bar) ) ) ))
26 |
27 | (display (list foo hux))
28 |
29 | ;;; end of foo.scm
30 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/si/loading.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id$
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; Try load. si/fact will assign to fact so it can be used after.
13 |
14 | (display (load "si/fact"))
15 |
16 | (display (fact 5))
17 |
18 | ;;; end of loading.scm
19 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/c/Imakefile:
--------------------------------------------------------------------------------
1 | # $Id: Imakefile,v 1.5 1994/08/21 19:36:30 queinnec Exp $
2 |
3 | ###(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ### This file is part of the files that accompany the book:
5 | ### LISP Implantation Semantique Programmation (InterEditions, France)
6 | ### By Christian Queinnec
7 | ### Newest version may be retrieved from:
8 | ### (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ### Check the README file before using this file.
10 | ###(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | work : scheme.o schemelib.o schemeklib.o
13 | clean :: cleanMakefile
14 |
15 | CC = gcc
16 | CFLAGS = -ansi -pedantic -gg
17 |
18 | # scheme.[hc] is the minimal runtime library used for chap10e.scm.
19 | # schemelib.c is the definition of the predefined global variables.
20 | # schemeklib.c is the definition of the predefined global variables when
21 | # using a CPS conversion.
22 | # rt.[hc] is an adaptation of an older library used for chap6f.scm
23 |
24 | scheme.o : scheme.h
25 | schemelib.o : scheme.h
26 | schemeklib.o : scheme.h
27 | rt.o : rt.h
28 |
29 | LIB = scheme.o schemelib.o
30 | #LIB = scheme.o schemeklib.o
31 |
32 | chap10e : chap10e.c ${LIB}
33 | ${CC} ${CFLAGS} -o chap10e chap10e.c ${LIB}
34 | chap10e
35 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/c/c10ex.c:
--------------------------------------------------------------------------------
1 | /* Compiler to C $Revision: 1.23 $
2 | (BEGIN
3 | (SET! INDEX 1)
4 | ((LAMBDA
5 | (CNTER . TMP)
6 | (SET! TMP (CNTER (LAMBDA (I) (LAMBDA X (CONS I X)))))
7 | (IF CNTER (CNTER TMP) INDEX))
8 | (LAMBDA (F) (SET! INDEX (+ 1 INDEX)) (F INDEX))
9 | 'FOO)) */
10 |
11 | #include "scheme.h"
12 |
13 | /* Global environment: */
14 | SCM_DefineGlobalVariable(INDEX,"INDEX");
15 |
16 | /* Quotations: */
17 | #define thing3 SCM_nil /* () */
18 | SCM_DefineString(thing4_object,"FOO");
19 | #define thing4 SCM_Wrap(&thing4_object)
20 | SCM_DefineSymbol(thing2_object,thing4); /* FOO */
21 | #define thing2 SCM_Wrap(&thing2_object)
22 | #define thing1 SCM_Int2fixnum(1)
23 | #define thing0 thing1 /* 1 */
24 |
25 | /* Functions: */
26 | SCM_DefineClosure(function_0, );
27 |
28 | SCM_DeclareFunction(function_0) {
29 | SCM_DeclareLocalVariable(F,0);
30 | return ((INDEX=SCM_Plus(thing1,
31 | SCM_CheckedGlobal(INDEX))),
32 | SCM_invoke1(F,
33 | SCM_CheckedGlobal(INDEX)));
34 | }
35 |
36 | SCM_DefineClosure(function_1, SCM I; );
37 |
38 | SCM_DeclareFunction(function_1) {
39 | SCM_DeclareLocalDottedVariable(X,0);
40 | return SCM_cons(SCM_Free(I),
41 | X);
42 | }
43 |
44 | SCM_DefineClosure(function_2, );
45 |
46 | SCM_DeclareFunction(function_2) {
47 | SCM_DeclareLocalVariable(I,0);
48 | return SCM_close(function_1,-1,1,I);
49 | }
50 |
51 | SCM_DefineClosure(function_3, );
52 |
53 | SCM_DeclareFunction(function_3) {
54 | SCM TMP_2; SCM CNTER_1;
55 | return ((INDEX=thing0),
56 | (CNTER_1=SCM_close(function_0,1,0),
57 | TMP_2=SCM_cons(thing2,
58 | thing3),
59 | (TMP_2= SCM_allocate_box(TMP_2),
60 | ((SCM_Content(TMP_2)=SCM_invoke1(CNTER_1,
61 | SCM_close(function_2,1,0))),
62 | ((CNTER_1 != SCM_false)
63 | ? SCM_invoke1(CNTER_1,
64 | SCM_Content(TMP_2))
65 | : SCM_CheckedGlobal(INDEX))))));
66 | }
67 |
68 |
69 | /* Expression: */
70 | void main(argc,argv) {
71 | SCM r;
72 | int i;
73 | for ( i=0; i<=10000; i++ ) r = (SCM_invoke0(SCM_close(function_3,0,0)));
74 | SCM_print(r);
75 | exit(0);
76 | }
77 |
78 | /* End of generated code. */
79 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/c/chap10ex.c:
--------------------------------------------------------------------------------
1 | /* Compiler to C $Revision: 4.0 $
2 | (BEGIN
3 | (SET! INDEX 1)
4 | ((LAMBDA
5 | (CNTER . TMP)
6 | (SET! TMP (CNTER (LAMBDA (I) (LAMBDA X (CONS I X)))))
7 | (IF CNTER (CNTER TMP) INDEX))
8 | (LAMBDA (F) (SET! INDEX (+ 1 INDEX)) (F INDEX))
9 | 'FOO)) */
10 |
11 | #include "scheme.h"
12 |
13 | /* Global environment: */
14 | SCM_DefineGlobalVariable(INDEX,"INDEX");
15 |
16 | /* Quotations: */
17 | #define thing3 SCM_nil /* () */
18 | SCM_DefineString(thing4_object,"FOO");
19 | #define thing4 SCM_Wrap(&thing4_object)
20 | SCM_DefineSymbol(thing2_object,thing4); /* FOO */
21 | #define thing2 SCM_Wrap(&thing2_object)
22 | #define thing1 SCM_Int2fixnum(1)
23 | #define thing0 thing1 /* 1 */
24 |
25 | /* Functions: */
26 | SCM_DefineClosure(function_0, );
27 |
28 | SCM_DeclareFunction(function_0) {
29 | SCM_DeclareLocalVariable(F,0);
30 | return ((INDEX=SCM_Plus(thing1,
31 | SCM_CheckedGlobal(INDEX))),
32 | SCM_invoke1(F,
33 | SCM_CheckedGlobal(INDEX)));
34 | }
35 |
36 | SCM_DefineClosure(function_1, SCM I; );
37 |
38 | SCM_DeclareFunction(function_1) {
39 | SCM_DeclareLocalDottedVariable(X,0);
40 | return SCM_cons(SCM_Free(I),
41 | X);
42 | }
43 |
44 | SCM_DefineClosure(function_2, );
45 |
46 | SCM_DeclareFunction(function_2) {
47 | SCM_DeclareLocalVariable(I,0);
48 | return SCM_close(SCM_CfunctionAddress(function_1),-1,1,I);
49 | }
50 |
51 | SCM_DefineClosure(function_3, );
52 |
53 | SCM_DeclareFunction(function_3) {
54 | SCM TMP_2; SCM CNTER_1;
55 | return ((INDEX=thing0),
56 | (CNTER_1=SCM_close(SCM_CfunctionAddress(function_0),1,0),
57 | TMP_2=SCM_cons(thing2,
58 | thing3),
59 | (TMP_2= SCM_allocate_box(TMP_2),
60 | ((SCM_Content(TMP_2)=
61 | SCM_invoke1(CNTER_1,SCM_close(SCM_CfunctionAddress
62 | (function_2),1,0))),
63 | ((CNTER_1 != SCM_false)
64 | ? SCM_invoke1(CNTER_1,
65 | SCM_Content(TMP_2))
66 | : SCM_CheckedGlobal(INDEX))))));
67 | }
68 |
69 |
70 | /* Expression: */
71 | void main(void) {
72 | SCM_print(SCM_invoke0(SCM_close(SCM_CfunctionAddress(function_3),
73 | 0,0)));
74 | exit(0);
75 | }
76 |
77 | /* End of generated code. */
78 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/c/loop.c:
--------------------------------------------------------------------------------
1 | /* $Id
2 | * Essai de faire boucler cpp. Ben non! cela ne boucle pas!!!
3 | */
4 |
5 | #define LOOP(x) (x+LOOP(x+1))
6 | #define BAR(x) (x+LOOP(x+1))
7 |
8 | void main () {
9 | int i = 0;
10 | return BAR(i);
11 | }
12 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap1.tst:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap1.tst,v 4.0 1995/07/10 06:50:32 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; Testing functions of chapter 1.
13 |
14 | (set! K (lambda (a b) a))
15 | ---
16 | (K 1 2)
17 | 1
18 |
19 | ;;; end of chap1.tst
20 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap10e.tst:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap10e.tst,v 4.0 1995/07/10 06:50:39 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; Additional tests for chap10e the compiler from Scheme to C.
13 |
14 | ;;; negative numbers
15 | (- 3 33)
16 | -30
17 | ;;; explicit syntax for booleans
18 | (if (car (cons #f #t)) 'void #t)
19 | #t
20 |
21 | ;;; test closures sharing
22 | (if (car (cons 2 3))
23 | 22 33 )
24 | 22
25 | ;;; This would need to recognize functions equivalence modulo alpha-conversion.
26 | ((lambda (x)
27 | (list (lambda () x) (lambda () x)) )
28 | 'x )
29 | (?- ?-)
30 |
31 | ;;; foreign interface
32 | (list (system "date"))
33 | ---
34 |
35 | ;;; This program was used as a running example of the compiler. It is
36 | ;;; no longer true since the example is now in src/chap10ex.scm.
37 | ;;; Pay attention that `if' should be ternary for objectify.
38 | (begin
39 | (set! foo (lambda (l c)
40 | ((lambda (max)
41 | ((lambda (walk) (walk l walk c))
42 | (lambda (l f c)
43 | (if (pair? l)
44 | (begin (if (< max (car l))
45 | (set! max (car l))
46 | #t )
47 | (f (cdr l) f (lambda (max min)
48 | (if (< (car l) min)
49 | (cons max (car l))
50 | (c max min) ) ) ) )
51 | (c max 100) ) ) ) )
52 | 0 ) ))
53 | (foo '(1 2 3 2 1) cons) )
54 | (3 . 1)
55 |
56 | ;;; end of chap10e.tst
57 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap10ex.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap10ex.scm,v 4.0 1995/07/10 06:50:39 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; This is the running example of chapter 10. This expression will be
13 | ;;; compiled to C in the src/c/chap10ex.c file. It contains at least
14 | ;;; one example of all syntactic structures.
15 |
16 | (begin
17 | (set! index 1)
18 | ((lambda (cnter . tmp)
19 | (set! tmp (cnter (lambda (i) (lambda x (cons i x)))))
20 | (if cnter (cnter tmp) index) )
21 | (lambda (f)
22 | (set! index (+ 1 index))
23 | (f index) )
24 | 'foo ) )
25 |
26 | ;;; end of chap10ex.scm
27 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap10j.tst:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap10j.tst,v 4.0 1995/07/10 06:50:46 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; Some tests about initialization analysis
13 |
14 | (set! x 22)
15 | 22
16 | (set! x y)
17 | *** ; y uninitialized
18 | (begin (set! x (lambda () y))
19 | (set! y (lambda () x))
20 | (x) )
21 | ---
22 | (begin (set! x (lambda () y))
23 | (set! y (x)) )
24 | *** ; y uninitialized
25 | ((lambda (x)
26 | (set! y x)
27 | y )
28 | 33 )
29 | 33
30 |
31 | ;;; end of chap10j.tst
32 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap10k.tst:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap10k.tst,v 4.0 1995/07/10 06:50:48 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; Additional tests for chap10k (compiler to C with CPS)
13 |
14 | ;;; testing non duplication of the continuation (Look generated code).
15 | (list (if #t 33 44))
16 | (33)
17 |
18 | ;;; end of chap10k.tst
19 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap10n.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap10n.scm,v 4.0 1995/07/10 06:50:50 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; Tests: this function redefines the one of chap10f.scm (for use by
13 | ;;; chap10e.scm) and allows to test letify in conjunction with chap10e.
14 |
15 | (define (compile->C e out)
16 | (set! g.current '())
17 | (let* ((ee (letify (Sexp->object e) '()))
18 | (prg (extract-things! (lift! ee))) )
19 | (gather-temporaries! (closurize-main! prg))
20 | (generate-C-program out e prg) ) )
21 |
22 | ;;; end of chap10n.scm
23 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap10p.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap10p.scm,v 4.0 1995/07/10 06:50:51 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; This function allows to test the CPS compiler with the
13 | ;;; initialization analysis.
14 |
15 | (define (compile->C e out)
16 | (set! g.current '())
17 | (let* ((ee (letify (cpsify (initialization-analyze! (Sexp->object e))) '()))
18 | (prg (extract-things! (lift! ee))) )
19 | (gather-temporaries! (closurize-main! prg))
20 | (generate-C-program out e prg) ) )
21 |
22 | ;;; end of chap10p.scm
23 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap1a.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap1a.scm,v 4.0 1995/07/10 06:50:52 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; variants of chapter 1.
13 |
14 | ;;; This eprogn confers a special value to (begin):
15 |
16 | (define (eprogn exps env)
17 | (if (pair? exps)
18 | (if (pair? (cdr exps))
19 | (begin (evaluate (car exps) env)
20 | (eprogn (cdr exps) env) )
21 | (evaluate (car exps) env) )
22 | empty-begin ) )
23 |
24 | (define empty-begin 813)
25 |
26 | ;;; This is an explicit left-to-right evlis
27 |
28 | (define (evlis exps env)
29 | (if (pair? exps)
30 | (let ((argument1 (evaluate (car exps) env)))
31 | (cons argument1 (evlis (cdr exps) env)) )
32 | '() ) )
33 |
34 | ;;; end of chap1a.scm
35 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap1c.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap1c.scm,v 4.0 1995/07/10 06:50:54 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; variants of chapter 1.
13 |
14 | (define (s.make-function variables body env)
15 | (lambda (values current.env)
16 | (let ((old-bindings
17 | (map (lambda (var val)
18 | (let ((old-value (getprop var 'apval)))
19 | (putprop var 'apval val)
20 | (cons var old-value) ) )
21 | variables
22 | values ) ))
23 | (let ((result (eprogn body current.env)))
24 | (for-each (lambda (b) (putprop (car b) 'apval (cdr b)))
25 | old-bindings )
26 | result ) ) ) )
27 |
28 | (define (s.lookup id env)
29 | (getprop id 'apval) )
30 |
31 | (define (s.update! id env value)
32 | (putprop id 'apval value) )
33 |
34 | ;;; end of chap1c.scm
35 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap2a.tst:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap2a.tst,v 4.0 1995/07/10 06:50:56 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; Testing the chap2a interpreter
13 |
14 | 33
15 | 33
16 | xyy
17 | *** ; unexistant
18 | 'foo
19 | foo
20 | (if #t 1 2)
21 | 1
22 | (if #f 2 3)
23 | 3
24 | (begin 1 2 3)
25 | 3
26 | (begin (set! a 3) a)
27 | 3
28 | (cons 3 4)
29 | (3 . 4)
30 | ((lambda (x y) (cons x y))
31 | 1 2 )
32 | (1 . 2)
33 | cons
34 | *** ; cons not a variable
35 | ((lambda (f) (f 1 2))
36 | cons )
37 | *** ; cons not a variable
38 | (apply (lambda (x y) (cons y x)) '1 '2 '())
39 | (2 . 1)
40 |
41 | ; no computation in functional position
42 | ((if #t cons list) 1 22)
43 | ***
44 |
45 |
46 | ;;; end of chap2a.tst
47 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap2b.tst:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap2b.tst,v 4.0 1995/07/10 06:50:58 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; testing chap2b
13 |
14 | 33
15 | 33
16 | xyy
17 | *** ; unexistant
18 | 'foo
19 | foo
20 | (if #t 1 2)
21 | 1
22 | (if #f 2 3)
23 | 3
24 | (begin 1 2 3)
25 | 3
26 | (begin (set! a 3) a)
27 | 3
28 | (cons 3 4)
29 | (3 . 4)
30 | ((lambda (x y) (cons x y))
31 | 1 2 )
32 | (1 . 2)
33 | cons
34 | *** ; cons not a variable
35 | ((lambda (f) (f 1 2))
36 | cons )
37 | *** ; cons not a variable
38 | (apply (lambda (x y) (cons y x)) '1 '2 '())
39 | (2 . 1)
40 |
41 | ; no computation in functional position
42 | ((if #t cons list) 1 22)
43 | ***
44 |
45 |
46 | (flet ((square (x) (* x x)))
47 | (square 3) )
48 | 9
49 | (apply (function cons) 1 2 '())
50 | (1 . 2)
51 | (funcall (function cons) 1 2)
52 | (1 . 2)
53 | (funcall (function funcall) (function cons) 1 2)
54 | (1 . 2)
55 |
56 | ((lambda (f)(apply f (list 3)))
57 | (flet ((square (x) (* x x)))
58 | (function (lambda (x) (square (square x)))) ))
59 | 81
60 |
61 | (labels ((fact (n) (if (= n 0) 1 (* n (fact (- n 1))))))
62 | (fact 5) )
63 | 120
64 | (labels ((odd? (n) (if (= n 0) #f (even? (- n 1))))
65 | (even? (n) (if (= n 0) #t (odd? (- n 1)))) )
66 | (list (odd? 5) (odd? 4) (even? 5) (even? 4)) )
67 | (#t #f #f #t)
68 |
69 | ;;; end of chap2b.tst
70 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap2c.tst:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap2c.tst,v 4.0 1995/07/10 06:51:00 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | 33
13 | 33
14 | xyy
15 | *** ; unexistant
16 | 'foo
17 | foo
18 | (if #t 1 2)
19 | 1
20 | (if #f 2 3)
21 | 3
22 | (begin 1 2 3)
23 | 3
24 | (begin (set! a 3) a)
25 | 3
26 | (cons 3 4)
27 | (3 . 4)
28 | ((lambda (x y) (cons x y))
29 | 1 2 )
30 | (1 . 2)
31 | cons
32 | *** ; cons not a variable
33 | ((lambda (f) (f 1 2))
34 | cons )
35 | *** ; cons not a variable
36 | (apply (function (lambda (x y) (cons y x))) '1 '2 '())
37 | (2 . 1)
38 |
39 | ; no computation in functional position
40 | ((if #t cons list) 1 22)
41 | ***
42 |
43 | (begin (set! foo 33)
44 | (dynamic foo) )
45 | *** ; no dynamic variable foo
46 | (dynamic-let ((foo (* 2 3)))
47 | (dynamic foo) )
48 | 6
49 | (begin (set! bar (function (lambda () (dynamic foo))))
50 | (dynamic-let ((foo (* 2 3)))
51 | (apply bar '()) ) )
52 | 6
53 | (begin (set! foo (function (lambda () (dynamic foo))))
54 | (dynamic-let ((foo (* 2 3)))
55 | (apply foo '()) ) )
56 | 6
57 | (dynamic-let ((foo (* 2 3)))
58 | (dynamic-set! foo (* 4 4))
59 | (list (dynamic-let ((foo 55))
60 | (dynamic-set! foo (* 5 5))
61 | (dynamic foo) )
62 | (dynamic foo) ) )
63 | (25 16)
64 |
65 |
66 |
67 |
68 | ;;; end of chap2c.tst
69 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap2g.tst:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap2g.tst,v 4.0 1995/07/10 06:51:06 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; tests on let.
13 |
14 | (let () 33)
15 | 33
16 | (let ((x 1)) x)
17 | 1
18 | (let ((x (* 2 3))
19 | (y (* 4 5)) )
20 | (+ x y) )
21 | 26
22 | (let ((x 1))
23 | 1 2 3 )
24 | 3
25 |
26 | ;;; tests on uninitialied variables
27 | xyzy
28 | *** ; undefined
29 |
30 | ;;; tests on letrec
31 | (letrec ((fact (lambda (n)
32 | (if (= n 1) 1 (* n (fact (- n 1)))) )))
33 | (letrec ((odd? (lambda (n) (if (= n 0) #f (even? (- n 1)))))
34 | (even? (lambda (n) (if (= n 0) #t (odd? (- n 1))))) )
35 | (list (fact 5) (odd? 5) (odd? 4) (even? 4) (even? 3)) ) )
36 | (120 #t #f #t #f)
37 |
38 | ((label fact (lambda (n) (if (= n 1) 1 (* n (fact (- n 1))))))
39 | 5 )
40 | 120
41 |
42 | ;;; end of chap2g.tst
43 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap2h.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap2h.scm,v 4.0 1995/07/10 06:51:07 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; Add innovation to chap1.scm ie allow (2 '(a b c)) or ((car cdr) '(a b)).
13 |
14 | (define (invoke fn args)
15 | (cond ((procedure? fn) (fn args))
16 | ((number? fn)
17 | (if (= (length args) 1)
18 | (if (>= fn 0) (list-ref (car args) fn)
19 | (list-tail (car args) (- fn)) )
20 | (wrong "Incorrect arity" fn) ) )
21 | ((pair? fn)
22 | (map (lambda (f) (invoke f args))
23 | fn ) )
24 | (else (wrong "Cannot apply" fn)) ) )
25 |
26 | ;;; end of chap2h.scm
27 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap2h.tst:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap2h.tst,v 4.0 1995/07/10 06:51:08 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | (2 '(a b c d))
13 | c
14 | (-1 '(a b c))
15 | (b c)
16 |
17 | (2 '(foo bar hux wok))
18 | hux
19 | (-2 '(foo bar hux wok))
20 | (hux wok)
21 | (0 '(foo bar hux wok))
22 | foo
23 | (2 (-3 '(a b c d e f g h)))
24 | f
25 |
26 | ((list + - *) 5 3)
27 | (8 2 15)
28 |
29 | ;;; end of chap2h.tst
30 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap3a.tst:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap3a.tst,v 4.0 1995/07/10 06:51:09 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; Test for chap3a.scm
13 |
14 | (find-symbol 'a '(a))
15 | #t
16 | (find-symbol 'a '(b . c))
17 | #f
18 | (find-symbol 'a '((((b . c) . d) e f . g) . a))
19 | #t
20 | (find-symbol 'a '((((b . c) . d) e f . g) . h))
21 | #f
22 |
23 | ;;; end of chap3a.tst
24 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap3b.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap3b.scm,v 4.2 2005/07/19 09:20:44 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | (define-syntax throw
13 | (syntax-rules ()
14 | ((throw tag value)
15 | (let* ((label tag) ; compute once
16 | (escape (assv label (dynamic *active-catchers*))) )
17 | (if (pair? escape)
18 | ((cdr escape) value)
19 | (wrong "No associated catch to" label) ) ) ) ) )
20 |
21 | (define-syntax catch
22 | (syntax-rules ()
23 | ((catch tag . body)
24 | (block label
25 | (dynamic-let ((*active-catchers*
26 | (cons (cons tag (lambda (x)
27 | (return-from label x) ))
28 | (dynamic *active-catchers*) ) ))
29 | . body ) ) ) ) )
30 |
31 | (define (find-symbol id tree)
32 | (define (find tree)
33 | (if (pair? tree)
34 | (or (find (car tree))
35 | (find (cdr tree)) )
36 | (if (eq? tree id) (throw 'find #t) #f) ) )
37 | (catch 'find (find tree)) )
38 |
39 |
40 | ;;; end of chap3b.scm
41 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap3c.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap3c.scm,v 4.1 1996/02/16 19:28:34 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | (define-syntax block
13 | (syntax-rules ()
14 | ((block label . body)
15 | (let ((label (list 'label)))
16 | (catch label . body) ) ) ) )
17 |
18 | (define-syntax return-from
19 | (syntax-rules ()
20 | ((return-from label value)
21 | (throw label value) ) ) )
22 |
23 | (define (find-symbol id tree)
24 | (block find
25 | (letrec ((find (lambda (tree)
26 | (if (pair? tree)
27 | (or (find (car tree))
28 | (find (cdr tree)) )
29 | (if (eq? id tree) (return-from find #t)
30 | #f ) ) )))
31 | (find tree) ) ) )
32 |
33 | ;;; end of chap3c.scm
34 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap3d.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap3d.scm,v 4.0 1995/07/10 06:51:12 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | (define (find-symbol id tree)
13 | (call/cc
14 | (lambda (exit)
15 | (define (find tree)
16 | (if (pair? tree)
17 | (or (find (car tree))
18 | (find (cdr tree)) )
19 | (if (eq? tree id) (exit #t) #f) ) )
20 | (find tree) ) ) )
21 |
22 | (define (fact n)
23 | (let ((r 1)(k 'void))
24 | (call/cc (lambda (c) (set! k c) 'void))
25 | (set! r (* r n))
26 | (set! n ( - n 1))
27 | (if (= n 1) r (k 'recurse)) ) ) ;\relax{\tt k}$\equiv$ {\tt goto}~! \endlisp
28 |
29 |
30 | ;;; end of chap3d.scm
31 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap3e.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap3e.scm,v 4.0 1995/07/10 06:51:13 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | (define (fact n)
13 | (let ((r 1))
14 | (let ((k (call/cc (lambda (c) c))))
15 | (set! r (* r n))
16 | (set! n ( - n 1))
17 | (if (= n 1) r (k k)) ) ) )
18 |
19 | (define-syntax catch
20 | (syntax-rules
21 | ((catch tag . body)
22 | (let ((saved-catchers *active-catchers*))
23 | (unwind-protect
24 | (block label
25 | (set! *active-catchers*
26 | (cons (cons tag (lambda (x) (return-from label x)))
27 | *active-catchers* ) )
28 | . body )
29 | (set! *active-catchers* saved-catchers) ) ) ) ) )
30 |
31 | (define-syntax let/cc
32 | (syntax-rules ()
33 | ((let/cc variable . body)
34 | (block variable
35 | (let ((variable (lambda (x) (return-from variable x))))
36 | . body ) ) ) ) )
37 |
38 | ;;; end of chap3e.scm
39 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap3h.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap3h.scm,v 4.1 1996/02/16 19:28:34 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; UNWIND-PROTECT
13 |
14 | (define-class unwind-protect-cont continuation (cleanup r))
15 |
16 | (define-class protect-return-cont continuation (value))
17 |
18 | (define (evaluate-unwind-protect form cleanup r k)
19 | (evaluate form
20 | r
21 | (make-unwind-protect-cont k cleanup r) ) )
22 |
23 | (define-method (resume (k unwind-protect-cont) v)
24 | (evaluate-begin (unwind-protect-cont-cleanup k)
25 | (unwind-protect-cont-r k)
26 | (make-protect-return-cont
27 | (unwind-protect-cont-k k) v ) ) )
28 |
29 | (define-method (resume (k protect-return-cont) v)
30 | (resume (protect-return-cont-k k) (protect-return-cont-value k)) )
31 |
32 | (define-method (resume (k throwing-cont) v) ; \modified
33 | (unwind (throwing-cont-k k) v (throwing-cont-cont k)) )
34 |
35 | (define-class unwind-cont continuation (value target))
36 |
37 | (define-method (unwind (k unwind-protect-cont) v target)
38 | (evaluate-begin (unwind-protect-cont-cleanup k)
39 | (unwind-protect-cont-r k)
40 | (make-unwind-cont
41 | (unwind-protect-cont-k k) v target ) ) )
42 |
43 | (define-method (resume (k unwind-cont) v)
44 | (unwind (unwind-cont-k k)
45 | (unwind-cont-value k)
46 | (unwind-cont-target k) ) )
47 |
48 | (define-method (block-lookup (r block-env) n k v) ; \modified
49 | (if (eq? n (block-env-name r))
50 | (unwind k v (block-env-cont r))
51 | (block-lookup (block-env-others r) n k v) ) )
52 |
53 | ;;; end of chap3h.scm
54 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap3i.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id$
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | (define (fact n k)
13 | (if (= n 1) (k 1)
14 | (fact (- n 1) (lambda (r) (k (* n r)))) ) )
15 |
16 | (define (divide p q f)
17 | (f (quotient p q) (remainder p q)) )
18 |
19 | (define (bezout n p k) ; assume $n>p$
20 | (divide
21 | n p (lambda (q r)
22 | (if (= r 0)
23 | (if (= p 1)
24 | (k 0 1) ; since $ 0 \times 1 - 1 \times 0 = 1 $
25 | (error "not relatively prime" n p) )
26 | (bezout
27 | p r (lambda (u v)
28 | (k v (- u (* v q))) ) ) ) ) ) )
29 | ;;; (bezout 45 5 list)
30 | ;;; (bezout 45 53 list)
31 | ;;; (bezout 1991 1960 list)
32 |
33 | (define (cc f)
34 | (let ((reified? #f))
35 | (let ((k (the-current-continuation)))
36 | (if reified? k (begin (set! reified? #t) (f k))) ) ) )
37 |
38 |
39 | (define (cps-fact n k)
40 | (if (= n 0) (k 1) (cps-fact (- n 1) (lambda (v) (k (* n v))))) )
41 |
42 | (define (make-box value)
43 | (let ((box
44 | (call/cc
45 | (lambda (exit)
46 | (letrec
47 | ((behavior
48 | (call/cc
49 | (lambda (store)
50 | (exit (lambda (msg . new)
51 | (call/cc
52 | (lambda (caller)
53 | (case msg
54 | ((get) (store (cons (car behavior)
55 | caller )))
56 | ((set)
57 | (store
58 | (cons (car new)
59 | caller ) ) ) ) ) ) )) ) ) ))
60 | ((cdr behavior) (car behavior)) ) ) ) ) )
61 | (box 'set value)
62 | box ) )
63 |
64 | ;;; end of chap3i.scm
65 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap4a.tst:
--------------------------------------------------------------------------------
1 | ;;; Particular tests for the interpreter
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; testing eq?
13 | (eq? 'a 'b)
14 | #f
15 | (eq? 'a 'a)
16 | #t
17 | (eq? (cons 1 2) (cons 1 2))
18 | #f
19 | ((lambda (a) (eq? a a))
20 | (cons 1 2) )
21 | #t
22 | ((lambda (a) (eq? a a))
23 | (lambda (x) x) )
24 | #t
25 | (eq? (lambda (x) 1) (lambda (x y) 2))
26 | #f
27 |
28 | ;;; testing eqv? (same as eq? plus eqv?)
29 | (eqv? '1 '2)
30 | #f
31 | (eqv? 1 1)
32 | #t
33 | (eqv? 'a 'b)
34 | #f
35 | (eqv? 'a 'a)
36 | #t
37 | (eqv? (cons 1 2) (cons 1 2))
38 | #f
39 | ((lambda (a) (eqv? a a))
40 | (cons 1 2) )
41 | #t
42 | ((lambda (a) (eqv? a a))
43 | (lambda (x) x) )
44 | #t
45 | (eqv? (lambda (x) 1) (lambda (x y) 2))
46 | #f
47 |
48 | ;;; Testing the special OR (backtracking without side-effect).
49 | ((lambda (x)
50 | (or (begin (set! x (+ x 1))
51 | #f )
52 | (if (= x 1) 'OK 'KO) ) )
53 | 1 )
54 | OK
55 | ((lambda (x)
56 | (or (begin (set! x (+ x 1))
57 | #f )
58 | (if (= x 1) (begin (set! x 3) x) 'KO) ) )
59 | 1 )
60 | 3
61 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap5-bench.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap5-bench.scm,v 4.0 1995/07/10 06:51:22 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 | ;;; This expression serves to compare the speed of two denotational
12 | ;;; interpreters (chap5a and chap5d).
13 |
14 | (begin
15 | (set! primes
16 | (lambda (n f max)
17 | ((lambda (filter)
18 | (begin
19 | (set! filter (lambda (p)
20 | (lambda (n) (= 0 (remainder n p))) ))
21 | (if (> n max)
22 | '()
23 | (if (f n)
24 | (primes (+ n 1) f max)
25 | (cons n
26 | ((lambda (ff)
27 | (primes (+ n 1)
28 | (lambda (p) (if (f p) t (ff p)))
29 | max ) )
30 | (filter n) ) ) ) ) ) )
31 | 'wait ) ) )
32 | (display (primes 2 (lambda (x) f) 50)) )
33 |
34 | ;;; With interpreted-chap5a on blaye: 62. seconds
35 | ;;; With interpreted-chap5d on blaye: 58. seconds
36 | ;;; With compiled-chap5a on blaye: 20. seconds
37 | ;;; With compiled-chap5d on blaye: 12. seconds
38 | ;;; With interpreted-chap6a on blaye: 2. seconds
39 | ;;; Compiled to C on blaye: 0.02 seconds
40 | ;;; size:
41 | ;;; text data bss dec hex
42 | ;;; 28672 4096 96 32864 8060
43 |
44 | ;;; end of chap5-bench.scm
45 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap5b.tst:
--------------------------------------------------------------------------------
1 | ;;; Test for Lambda-calculus
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 | ;;; The initial environment maps identifiers to numbers and also contain
12 | ;;; the addition as a delta rule.
13 |
14 | a
15 | 1
16 | b
17 | 2
18 | (+ a)
19 | ---
20 | ((+ a) b)
21 | 3
22 |
23 | ((lambda (K) ((K a) b))
24 | (lambda (x) (lambda (y) x)) )
25 | 1
26 |
27 | ((lambda (S)
28 | ((lambda (K) (((S K) K) b))
29 | (lambda (x) (lambda (y) x)) ) )
30 | (lambda (f) (lambda (g) (lambda (x) ((f x) (g x))))) )
31 | 2
32 |
33 | ((label fact (lambda (x)
34 | ((((= x) a)
35 | a )
36 | ((* x) (fact ((- x) a))) ) ))
37 | a )
38 | 1
39 | ((label fact (lambda (x)
40 | ((((= x) a)
41 | a )
42 | ((* x) (fact ((- x) a))) ) ))
43 | ((+ b) b) )
44 | 24
45 |
46 | ;;; this one loops
47 | ((lambda (Y)
48 | ((lambda (meta-fact)
49 | ((Y meta-fact) ((+ b) b)) )
50 | (lambda (f)
51 | (lambda (x)
52 | ((((= x) a)
53 | a )
54 | ((* x) (f ((- x) a))) ) ) ) ) )
55 | (lambda (f)
56 | ((lambda (x) (f (lambda (y) ((x x) y))))
57 | (lambda (x) (f (lambda (y) ((x x) y)))) ) ) )
58 | 24
59 |
60 | ;;; end of chap5b.tst
61 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap5c.tst:
--------------------------------------------------------------------------------
1 | ;;; Testing dynamic binding
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | (dynamic-let (a 1) 2)
13 | 2
14 | (dynamic-let (a 1) 2 3 4)
15 | 4
16 | (dynamic-let (a 1) (dynamic a))
17 | 1
18 | (dynamic-let (a 1) (+ (dynamic a) (dynamic a)))
19 | 2
20 | (dynamic-let (a 1)
21 | (dynamic-let (a (+ (dynamic a) (dynamic a)))
22 | (dynamic a) ) )
23 | 2
24 |
25 | ;;; with respect to functions
26 | ((lambda (f)
27 | (dynamic-let (a 1) (f)) )
28 | (lambda () (dynamic a)) )
29 | 1
30 | ((lambda (f)
31 | (dynamic-let (a 1)
32 | (list (f)
33 | (dynamic-let (a 2) (f)) ) ) )
34 | (lambda () (dynamic a)) )
35 | (1 2)
36 |
37 | ;;; with respect to continuations
38 | (dynamic-let (a 1)
39 | ((call/cc (lambda (k)
40 | ((lambda (f)
41 | (dynamic-let (a 2) f) )
42 | (lambda () (dynamic a)) ) ))) )
43 | 1
44 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap5e.tst:
--------------------------------------------------------------------------------
1 | ;;; Some tests for chap5e.scm
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | 3
13 | (3)
14 | '(a b)
15 | ((a b))
16 | (car '(a b))
17 | (a a)
18 | (if 1 2 3)
19 | (2)
20 | (+ 2 3)
21 | (5 5 5 5 5 5)
22 | (list 2 3 4)
23 | ((2 3 4) (2 3 4) (2 3 4) (2 3 4) (2 3 4) (2 3 4)
24 | (2 3 4) (2 3 4) (2 3 4) (2 3 4) (2 3 4) (2 3 4)
25 | (2 3 4) (2 3 4) (2 3 4) (2 3 4) (2 3 4) (2 3 4)
26 | (2 3 4) (2 3 4) (2 3 4) (2 3 4) (2 3 4) (2 3 4) )
27 | (call/cc
28 | (lambda (k)
29 | ((k 1) (k 2)) ) )
30 | (1 1 1 1 2 2 2 2)
31 |
32 | ;;; end of chap5e.tst
33 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap5g.tst:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap5g.tst,v 4.0 1995/07/10 06:51:32 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | (define zlurp (+ 2 3))
13 | ---
14 | zlurp
15 | 5
16 | (set! zlurp 6)
17 | ---
18 | zlurp
19 | 6
20 |
21 | ((lambda (zixxy)
22 | (define zixxy (* 2 2)) )
23 | 3 )
24 | ---
25 | zixxy
26 | 4
27 |
28 | ;;; end of chap5g.tst
29 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap6b.tst:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap6b.tst,v 4.0 1995/07/10 06:51:36 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; testing that new variables are automatically created.
13 |
14 | pqoeur
15 | ***
16 | (begin (set! zld 3)
17 | zld )
18 | 3
19 | (lambda () oiuerq 3)
20 | ---
21 | ((lambda (x) oeewfiuerq x) 3)
22 | ***
23 |
24 | ;;; end of chap6b.tst
25 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap6dd.tst:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap6dd.tst,v 4.0 1995/07/10 06:51:40 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; Tests for chap6dd.scm
13 |
14 | (redefine car)
15 | ---
16 | (set! car car)
17 | ---
18 | (car '(a b))
19 | a
20 | ((lambda (old v)
21 | (begin (set! old car)
22 | (set! car cdr)
23 | (set! v (car '(a b)))
24 | (set! car old)
25 | v ) )
26 | 'old 'v )
27 | (b)
28 |
29 | ;;; testing errors
30 | (redefine car)
31 | ***
32 | (redefine foo)
33 | ***
34 |
35 | ;;; end of chap6dd.tst
36 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap6g.tst:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap6g.tst,v 4.0 1995/07/10 06:51:45 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; test that only explicitely defined variables are created.
13 |
14 | (define xfoo 3)
15 | ---
16 | (define xfoo (lambda () xbar))
17 | ***
18 | (begin (define xfoo (lambda () xbar))
19 | (define xbar 33) )
20 | ---
21 | (begin (define xfoo (lambda () xbar))
22 | (define xbar (lambda () xhux)) )
23 | ***
24 | (begin (define xfoo (lambda () xbar))
25 | (define xbar (lambda () (xhux))) )
26 | ***
27 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap6h.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap6h.scm,v 4.0 1995/07/10 06:51:46 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; Improvement on chap6d.scm for niladic functions.
13 |
14 | (define (meaning-fix-abstraction n* e+ r tail?)
15 | (let ((arity (length n*)))
16 | (if (= arity 0)
17 | (let ((m+ (meaning-sequence e+ r #t)))
18 | (THUNK-CLOSURE m+) )
19 | (let* ((r2 (r-extend* r n*))
20 | (m+ (meaning-sequence e+ r2 #t)) )
21 | (FIX-CLOSURE m+ arity) ) ) ) )
22 |
23 | (define (THUNK-CLOSURE m+)
24 | (let ((arity+1 (+ 0 1)))
25 | (lambda ()
26 | (define (the-function v* sr)
27 | (if (= (activation-frame-argument-length v*) arity+1)
28 | (begin (set! *env* sr)
29 | (m+) )
30 | (wrong "Incorrect arity") ) )
31 | (make-closure the-function *env*) ) ) )
32 |
33 | ;;; end of chap6h.scm
34 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap8b.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap8b.scm,v 4.3 2006/11/27 11:34:27 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; Modification of chap4a.scm to introduce eval as a special form
13 |
14 | (define (evaluate e r s k)
15 | (if (atom? e)
16 | (if (symbol? e) (evaluate-variable e r s k)
17 | (evaluate-quote e r s k) )
18 | (case (car e)
19 | ((quote) (evaluate-quote (cadr e) r s k))
20 | ((if) (evaluate-if (cadr e) (caddr e) (cadddr e) r s k))
21 | ((begin) (evaluate-begin (cdr e) r s k))
22 | ((set!) (evaluate-set! (cadr e) (caddr e) r s k))
23 | ((lambda) (evaluate-lambda (cadr e) (cddr e) r s k))
24 | ((eval) (evaluate-eval (cadr e) r s k)) ; \modified
25 | (else (evaluate-application (car e) (cdr e) r s k)) ) ) )
26 |
27 | (define (evaluate-eval e r s k)
28 | (evaluate e r s
29 | (lambda (v ss)
30 | (let ((ee (transcode-back v ss)))
31 | (if (program? ee)
32 | (evaluate ee r ss k)
33 | (wrong "Illegal program" ee) ) ) ) ) )
34 |
35 | ;;; Create some additional locations
36 |
37 | (definitial bar 0)
38 | (definitial x 0)
39 | (definitial z 0)
40 | (definitial wrek 0)
41 |
42 | (defprimitive display
43 | (lambda (v* s k)
44 | (display (transcode-back (car v*) s))
45 | (k (car v*) s) )
46 | 1 )
47 |
48 | ;;; end of chap8b.scm
49 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap8b.tst:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap8b.tst,v 4.0 1995/07/10 06:52:02 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; Eval with dynamic variables
13 |
14 | "Testing dynamic variables"
15 | ---
16 | (dynamic-let (a 1)
17 | (eval '(dynamic a)) )
18 | 1
19 | (dynamic-let (a 1)
20 | (dynamic-let (b 2)
21 | (eval '(dynamic b)) ) )
22 | 2
23 | ((lambda (f)
24 | (dynamic-let (a 33)
25 | ((eval 'f)) ) )
26 | (lambda () (dynamic a)) )
27 | 33
28 |
29 | ;;; end of chap8b.tst
30 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap8c.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap8c.scm,v 4.3 2006/11/24 18:41:11 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; Modification of chap6d.scm to introduce eval as a special form
13 |
14 | (define (meaning e r tail?)
15 | (if (atom? e)
16 | (if (symbol? e) (meaning-reference e r tail?)
17 | (meaning-quotation e r tail?) )
18 | (case (car e)
19 | ((quote) (meaning-quotation (cadr e) r tail?))
20 | ((lambda) (meaning-abstraction (cadr e) (cddr e) r tail?))
21 | ((if) (meaning-alternative (cadr e) (caddr e) (cadddr e)
22 | r tail? ))
23 | ((begin) (meaning-sequence (cdr e) r tail?))
24 | ((set!) (meaning-assignment (cadr e) (caddr e) r tail?))
25 | ((eval) (meaning-eval (cadr e) r tail?)) ; \modified
26 | (else (meaning-application (car e) (cdr e)
27 | r tail? )) ) ) )
28 |
29 | (define (meaning-eval e r tail?)
30 | (let ((m (meaning e r #f)))
31 | (lambda ()
32 | (let ((v (m)))
33 | (if (program? v)
34 | (let ((mm (meaning v r tail?)))
35 | (mm) )
36 | (wrong "Illegal program" v) ) ) ) ) )
37 |
38 | ;;; Add additional locations
39 |
40 | (defvariable wrek)
41 | (define original.g.current
42 | (let ((g g.current))
43 | (lambda () g) ) )
44 |
45 | ;;; end of chap8c.scm
46 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap8e.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap8e.scm,v 4.0 1995/07/10 06:52:05 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | (defprimitive eval
13 | (lambda (v)
14 | (if (program? v)
15 | (evaluate v env.global)
16 | (wrong "Illegal program" v) ))
17 | 1 )
18 |
19 | ;;; end of chap8e.scm
20 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap8f.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap8f.scm,v 4.1 2006/11/24 18:40:55 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; eval as a function in the bytecode compiler
13 |
14 | (definitial eval
15 | (let* ((arity 1)
16 | (arity+1 (+ arity 1)) )
17 | (make-primitive
18 | (lambda ()
19 | (if (= arity+1 (activation-frame-argument-length *val*))
20 | (let ((v (activation-frame-argument *val* 0)))
21 | (if (program? v)
22 | (compile-and-run v r.init #t)
23 | (signal-exception #t (list "Illegal program" v)) ) )
24 | (signal-exception
25 | #t (list "Incorrect arity" 'eval) ) ) ) ) ) )
26 |
27 | ;;; same as in chap8d.scm
28 |
29 | (define (compile-and-run v r tail?)
30 | (set! g.current '())
31 | (for-each g.current-extend! sg.current.names)
32 | (set! *quotations* (vector->list *constants*))
33 | (set! *dynamic-variables* *dynamic-variables*)
34 | (let ((code (apply vector (append (meaning v r #f) (RETURN)))))
35 | (set! sg.current.names (map car (reverse g.current)))
36 | (let ((v (make-vector (length sg.current.names) undefined-value)))
37 | (vector-copy! sg.current v 0 (vector-length sg.current))
38 | (set! sg.current v) )
39 | (set! *constants* (apply vector *quotations*))
40 | (set! *dynamic-variables* *dynamic-variables*)
41 | (unless tail? (stack-push *pc*))
42 | (set! *pc* (install-code! code)) ) )
43 |
44 | ;;; end of chap8f.scm
45 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap9a.tst:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap9a.tst,v 4.0 1995/07/10 06:52:15 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; testing chap9a.scm
13 | ;;; If define-abbreviation is not defined, here is a simulation
14 | (define-pervasive-macro (define-abbreviation . parms)
15 | `(define-pervasive-macro . ,parms) )
16 | ---
17 |
18 | (with-gensym (a b c)
19 | (list a b c) )
20 | ---
21 |
22 | ;;; exercice 1
23 | (let ((i 0)
24 | (r '()) )
25 | (repeat1 :while (begin (set! i (+ i 1)) (< i 10))
26 | :unless (= 0 (modulo i 2))
27 | :do (set! r (cons i r)) )
28 | r )
29 | (9 7 5 3 1)
30 |
31 | (pp (expand '(generate-vector-of-fix-makers 3)))
32 | ---
33 |
34 | (define-alias gg generate-vector-of-fix-makers)
35 | ---
36 | (pp (expand '(gg 3)))
37 | ---
38 |
39 | ;;; tests chap9b.tst
40 | (enumerate)
41 | --- ; prints 0
42 | (enumerate (display 'a))
43 | --- ; prints 0a1
44 | (enumerate (display 'a) (display 'b))
45 | --- ; prints 0a1b2
46 | (enumerate (display 'a) (display 'b) (display 'c))
47 | --- ; prints 0a1b2c3
48 | (enumerate (display 'a) (display 'b) (display 'c) (display 'd))
49 | --- ; prints 0a1b2c3d4
50 |
51 | (meroon-if #t 1 2)
52 | 1
53 | (meroon-if #f 1 2)
54 | 2
55 | (meroon-if '() 1 2)
56 | 1
57 |
58 | (define-inline (acons x y al)
59 | (cons (cons x y) al) )
60 | ---
61 | (acons 'a 1 (acons 'b 2 '()))
62 | ((a . 1) (b . 2))
63 | (apply acons 'a '1 '(()))
64 | ((a . 1))
65 |
66 | ;;; end of chap9a.tst
67 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap9b.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap9b.scm,v 4.0 1995/07/10 06:52:16 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | (define-abbreviation (define-abbreviation call . body)
13 | `(install-macro! ',(car call) (lambda ,(cdr call) . ,body)) )
14 |
15 | (define-abbreviation (define-meroonet-macro call . body)
16 | `(begin (define-abbreviation ,call . ,body)
17 | (eval '(define-abbreviation ,call . ,body)) ) )
18 |
19 | (define-abbreviation (meroon-if condition consequent . alternant)
20 | `(if (let ((tmp ,condition))
21 | (or tmp (null? tmp)) )
22 | ,consequent . ,alternant ) )
23 |
24 | (define-abbreviation (define-inline call . body)
25 | (let ((name (car call))
26 | (variables (cdr call)) )
27 | `(begin
28 | (define-abbreviation (,name . arguments)
29 | (cons (cons 'lambda (cons ',variables ',body))
30 | arguments ) )
31 | (define ,call (,name . ,variables)) ) ) )
32 |
33 | (define-syntax enumerate
34 | (syntax-rules ()
35 | ((enumerate) (display 0))
36 | ((enumerate e1 e2 ...)
37 | (begin (display 0) (enumerate-aux e1 (e1) e2 ...) ) ) ) )
38 |
39 | (define-syntax enumerate-aux
40 | (syntax-rules ()
41 | ((enumerate-aux e1 len) (begin e1 (display (length 'len))))
42 | ((enumerate-aux e1 len e2 e3 ...)
43 | (begin e1 (display (length 'len))
44 | (enumerate-aux e2 (e2 . len) e3 ...) ) ) ) )
45 |
46 | ;;; end of chap9b.scm
47 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap9f.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap9f.scm,v 4.0 1995/07/10 06:52:22 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; Only one world
13 |
14 | (define (make-macro-environment current-level)
15 | (let ((metalevel (delay current-level)))
16 | (list (make-Magic-Keyword 'eval-in-abbreviation-world
17 | (special-eval-in-abbreviation-world metalevel) )
18 | (make-Magic-Keyword 'define-abbreviation
19 | (special-define-abbreviation metalevel))
20 | (make-Magic-Keyword 'let-abbreviation
21 | (special-let-abbreviation metalevel))
22 | (make-Magic-Keyword 'with-aliases
23 | (special-with-aliases metalevel) ) ) ) )
24 |
25 | ;;; end of chap9f.scm
26 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/chap9z.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap9z.scm,v 4.2 2006/11/25 17:01:28 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; Various excerpts for chapter on macros
13 |
14 | (define (prepare expression directives)
15 | (let ((macroexpand (generate-macroexpand directives)))
16 | (really-prepare (macroexpand expression)) ) )
17 |
18 | (define (prepare expression)
19 | (really-prepare (macroexpand expression)) )
20 |
21 | (define-abbreviation (while condition . body) \[\hfill\em{LOOP}\]
22 | `(if ,condition (begin (begin . ,body)
23 | (while ,condition . ,body) )) )
24 |
25 | (define-abbreviation (incredible x) \[\hfill\em{BAD TASTE}\]
26 | (call/cc (lambda (k) `(quote (,k ,x)))) )
27 |
28 | (define-abbreviation (define-immediate-abbreviation call . body)
29 | (let ((name (gensym)))
30 | `(begin (define ,name (lambda ,(cdr call) . ,body))
31 | (define-abbreviation ,call (,name . ,(cdr call))) ) ) )
32 |
33 | ;;; end of chap9z.scm
34 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/src/showGC.s2c:
--------------------------------------------------------------------------------
1 | ;;; Show GC in Scheme->C
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | (set! after-collect
13 | (let ((gc-count 0))
14 | (lambda (heap-size allocated-size full-gc-threshold)
15 | (set! gc-count (+ 1 gc-count))
16 | (format stderr-port
17 | " [~A, %used=~A/~A]~%"
18 | gc-count
19 | allocated-size
20 | heap-size ) ) ) )
21 |
22 | ;;; end of showGC.s2c
23 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/tmp.si/bibexp.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: bibexp.scm,v 1.2 1994/08/21 19:35:32 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | (define (fact2 n) (if (= n 0) 1 (* n (fact2 (- n 1)))))
13 |
14 | (define-abbreviation (factorial n)
15 | (if (and (integer? n) (> n 0)) (fact2 n) `(fact1 ,n)) )
16 |
17 | ;;; end of bibexp.scm
18 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/tmp.si/bibrun.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: bibrun.scm,v 1.2 1994/08/21 19:35:32 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | (define (fact1 n) (if (= n 0) 1 (* n (fact1 (- n 1)))))
13 |
14 | ;;; end of bibrun.scm
15 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/tmp.si/chap9b.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap9b.scm,v 1.2 1994/08/21 19:35:32 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | (define (fact1 n) (if (= n 0) 1 (* n (fact1 (- n 1)))))
13 |
14 | (define-abbreviation (factorial n)
15 | (define (fact2 n) (if (= n 1) 1 (* n (fact2 (- n 1)))))
16 | (if (and (integer? n) (> n 0)) (fact2 n) `(fact1 ,n)) )
17 |
18 | (define (some-facts)
19 | (list (factorial 5) (factorial (+ 3 2))) )
20 |
21 | ;;; end of chap9b.scm
22 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/tmp.si/chap9c.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id: chap9c.scm,v 1.2 1994/08/21 19:35:32 queinnec Exp $
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | (define (fact n) (if (= n 0) 1 (* n (fact (- n 1)))))
13 |
14 | (define-abbreviation (factorial n)
15 | (if (and (integer? n) (> n 0)) (fact n) `(fact ,n)) )
16 |
17 | ;;; end of chap9c.scm
18 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/tmp.si/example.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id$
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | (set! fact
13 | ((lambda (fact) (lambda (n)
14 | (if (< n 0)
15 | "Toctoc la tete!"
16 | (fact n fact (lambda (x) x)) ) ))
17 | (lambda (n f k)
18 | (if (= n 0)
19 | (k 1)
20 | (f (- n 1) f (lambda (r) (k (* n r)))) ) ) ) )
21 |
22 | ;;; end of example.scm
23 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/tmp.si/fact.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id$
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; Test for chap7e.scm.
13 | ;;; Be above directories /si/ and /so/ then evaluate:
14 | ;;; (compile-file "si/fact")
15 | ;;; It generates the file si/fact.so that can be read.
16 |
17 | (set! fact
18 | (lambda (n)
19 | (if (= n 0) 1
20 | (* n (fact (- n 1))) ) ) )
21 |
22 | ;;;; end of fact.scm
23 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/tmp.si/fib.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id$
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; (compile-file "si/fib")
13 |
14 | (set! fib (lambda (n)
15 | (if (= n 0) 1
16 | (if (= n 1) 1
17 | (+ (fib (- n 1)) (fib (- n 2))) ) ) ))
18 |
19 | ;;; end of fib.scm
20 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/tmp.si/foo.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id$
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; This file introduces a lot of things to test.
13 | ;;; (compile-file "si/foo")
14 | ;;; (run-application 100 "si/foo")
15 |
16 | (set! foo '(a b))
17 |
18 | (set! bar
19 | (lambda (x)
20 | (cons x (dynamic x)) ) )
21 |
22 | (set! hux (dynamic-let (x '(c d))
23 | (dynamic-let (y 'foo)
24 | (dynamic-let (x foo)
25 | (bar 'bar) ) ) ))
26 |
27 | (display (list foo hux))
28 |
29 | ;;; end of foo.scm
30 |
--------------------------------------------------------------------------------
/references/LiSP-2ndEdition-2006Dec11/tmp.si/loading.scm:
--------------------------------------------------------------------------------
1 | ;;; $Id$
2 |
3 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
4 | ;;; This file is part of the files that accompany the book:
5 | ;;; LISP Implantation Semantique Programmation (InterEditions, France)
6 | ;;; By Christian Queinnec
7 | ;;; Newest version may be retrieved from:
8 | ;;; (IP 128.93.2.54) ftp.inria.fr:INRIA/Projects/icsla/Books/LiSP*.tar.gz
9 | ;;; Check the README file before using this file.
10 | ;;;(((((((((((((((((((((((((((((((( L i S P ))))))))))))))))))))))))))))))))
11 |
12 | ;;; Try load. si/fact will assign to fact so it can be used after.
13 |
14 | (display (load "si/fact"))
15 |
16 | (display (fact 5))
17 |
18 | ;;; end of loading.scm
19 |
--------------------------------------------------------------------------------
/references/jmc.lisp:
--------------------------------------------------------------------------------
1 | ; The Lisp defined in McCarthy's 1960 paper, translated into CL.
2 | ; Assumes only quote, atom, eq, cons, car, cdr, cond.
3 | ; Bug reports to lispcode@paulgraham.com.
4 |
5 | (defun null. (x)
6 | (eq x '()))
7 |
8 | (defun and. (x y)
9 | (cond (x (cond (y 't) ('t '())))
10 | ('t '())))
11 |
12 | (defun not. (x)
13 | (cond (x '())
14 | ('t 't)))
15 |
16 | (defun append. (x y)
17 | (cond ((null. x) y)
18 | ('t (cons (car x) (append. (cdr x) y)))))
19 |
20 | (defun list. (x y)
21 | (cons x (cons y '())))
22 |
23 | (defun pair. (x y)
24 | (cond ((and. (null. x) (null. y)) '())
25 | ((and. (not. (atom x)) (not. (atom y)))
26 | (cons (list. (car x) (car y))
27 | (pair. (cdr x) (cdr y))))))
28 |
29 | (defun assoc. (x y)
30 | (cond ((eq (caar y) x) (cadar y))
31 | ('t (assoc. x (cdr y)))))
32 |
33 | (defun eval. (e a)
34 | (cond
35 | ((atom e) (assoc. e a))
36 | ((atom (car e))
37 | (cond
38 | ((eq (car e) 'quote) (cadr e))
39 | ((eq (car e) 'atom) (atom (eval. (cadr e) a)))
40 | ((eq (car e) 'eq) (eq (eval. (cadr e) a)
41 | (eval. (caddr e) a)))
42 | ((eq (car e) 'car) (car (eval. (cadr e) a)))
43 | ((eq (car e) 'cdr) (cdr (eval. (cadr e) a)))
44 | ((eq (car e) 'cons) (cons (eval. (cadr e) a)
45 | (eval. (caddr e) a)))
46 | ((eq (car e) 'cond) (evcon. (cdr e) a))
47 | ('t (eval. (cons (assoc. (car e) a)
48 | (cdr e))
49 | a))))
50 | ((eq (caar e) 'label)
51 | (eval. (cons (caddar e) (cdr e))
52 | (cons (list. (cadar e) (car e)) a)))
53 | ((eq (caar e) 'lambda)
54 | (eval. (caddar e)
55 | (append. (pair. (cadar e) (evlis. (cdr e) a))
56 | a)))))
57 |
58 | (defun evcon. (c a)
59 | (cond ((eval. (caar c) a)
60 | (eval. (cadar c) a))
61 | ('t (evcon. (cdr c) a))))
62 |
63 | (defun evlis. (m a)
64 | (cond ((null. m) '())
65 | ('t (cons (eval. (car m) a)
66 | (evlis. (cdr m) a)))))
67 |
68 |
--------------------------------------------------------------------------------
/references/sicp/README.md:
--------------------------------------------------------------------------------
1 | # Code from SICP
2 |
3 | Code from the book
4 | [_Structure and Interpretation of Computer Programs_](https://mitpress.mit.edu/sites/default/files/sicp/index.html) by Hal Abelson, Jerry Sussman & Julie Sussman.
5 |
6 |
--------------------------------------------------------------------------------
/references/sicp/ch2support.scm:
--------------------------------------------------------------------------------
1 | ;;; CODE FROM OTHER CHAPTERS OF STRUCTURE AND INTERPRETATION OF
2 | ;;; COMPUTER PROGRAMS NEEDED BY CHAPTER 2
3 |
4 | ;;;from chapter 1
5 | (define (square x) (* x x))
6 |
7 | ;;;from section 1.2.5, for Section 2.1.1
8 | (define (gcd a b)
9 | (if (= b 0)
10 | a
11 | (gcd b (remainder a b))))
12 |
13 | ;;;from section 1.2.2, for Section 2.2.3
14 | (define (fib n)
15 | (cond ((= n 0) 0)
16 | ((= n 1) 1)
17 | (else (+ (fib (- n 1))
18 | (fib (- n 2))))))
19 |
20 | ;;; ***not in book, but needed for code before quote is introduced***
21 | (define nil '())
22 |
23 | ;;;-----------
24 | ;;;from section 3.3.3 for section 2.4.3
25 | ;;; to support operation/type table for data-directed dispatch
26 |
27 | (define (assoc key records)
28 | (cond ((null? records) false)
29 | ((equal? key (caar records)) (car records))
30 | (else (assoc key (cdr records)))))
31 |
32 | (define (make-table)
33 | (let ((local-table (list '*table*)))
34 | (define (lookup key-1 key-2)
35 | (let ((subtable (assoc key-1 (cdr local-table))))
36 | (if subtable
37 | (let ((record (assoc key-2 (cdr subtable))))
38 | (if record
39 | (cdr record)
40 | false))
41 | false)))
42 | (define (insert! key-1 key-2 value)
43 | (let ((subtable (assoc key-1 (cdr local-table))))
44 | (if subtable
45 | (let ((record (assoc key-2 (cdr subtable))))
46 | (if record
47 | (set-cdr! record value)
48 | (set-cdr! subtable
49 | (cons (cons key-2 value)
50 | (cdr subtable)))))
51 | (set-cdr! local-table
52 | (cons (list key-1
53 | (cons key-2 value))
54 | (cdr local-table)))))
55 | 'ok)
56 | (define (dispatch m)
57 | (cond ((eq? m 'lookup-proc) lookup)
58 | ((eq? m 'insert-proc!) insert!)
59 | (else (error "Unknown operation -- TABLE" m))))
60 | dispatch))
61 |
62 | (define operation-table (make-table))
63 | (define get (operation-table 'lookup-proc))
64 | (define put (operation-table 'insert-proc!))
65 |
66 | ;;;-----------
--------------------------------------------------------------------------------
/references/sicp/ch3support.scm:
--------------------------------------------------------------------------------
1 | ;;; CODE TO SUPPORT CHAPTER 3 OF STRUCTURE AND INTERPRETATION OF
2 | ;;; COMPUTER PROGRAMS
3 | ;;; NB. This code is *not* from the book
4 |
5 | ;;; In addition to code supplied here
6 | ;;;**For 3.4, might want parallel-execute as implemented for MIT Scheme
7 | ;;;**For 3.5, need stream special forms, which are not in Standard Scheme
8 |
9 |
10 | ;;For Section 3.1.2 -- written as suggested in footnote,
11 | ;; though the values of a, b, m may not be very "appropriately chosen"
12 | (define (rand-update x)
13 | (let ((a 27) (b 26) (m 127))
14 | (modulo (+ (* a x) b) m)))
15 |
16 |
17 | ;;For Section 3.3.4, used by and-gate
18 | ;;Note: logical-and should test for valid signals, as logical-not does
19 | (define (logical-and x y)
20 | (if (and (= x 1) (= y 1))
21 | 1
22 | 0))
23 |
24 |
25 | ;;For Section 3.5 -- useful for looking at finite amounts of infinite streams
26 | ;;Print the first n elements of the stream s.
27 | ;;One version prints on one line, one on separate lines
28 |
29 | (define (print-n s n)
30 | (if (> n 0)
31 | (begin (display (stream-car s))
32 | (display ",")
33 | (print-n (stream-cdr s) (- n 1)))))
34 |
35 | (define (print-n s n)
36 | (if (> n 0)
37 | (begin (newline)
38 | (display (stream-car s))
39 | (print-n (stream-cdr s) (- n 1)))))
40 |
41 |
42 | ;;For Section 3.5.2, to check power series (exercises 3.59-3.62)
43 | ;;Evaluate and accumulate n terms of the series s at the given x
44 | ;;Uses horner-eval from ex 2.34
45 | (define (eval-power-series s x n)
46 | (horner-eval x (first-n-of-series s n)))
47 | (define (first-n-of-series s n)
48 | (if (= n 0)
49 | '()
50 | (cons (stream-car s) (first-n-of-series (stream-cdr s) (- n 1)))))
51 |
--------------------------------------------------------------------------------
/references/sicp/instructors-manual/.gitignore:
--------------------------------------------------------------------------------
1 | compiled/
--------------------------------------------------------------------------------
/references/sicp/instructors-manual/README.md:
--------------------------------------------------------------------------------
1 | # Concrete Metacircular Evaluator
2 |
3 | Code adapted from the "evaluator with concrete syntax" listed on pages 121-122 of
4 | ["Instructor's Manual to accompany Structure and Interpretation of Computer Programs"](https://mitpress.mit.edu/books/instructors-manual-ta-structure-and-interpretation-computer-programs-second-edition)
5 | by Julie Sussman with Harold Abelson and Gerald Jay Sussman.
6 |
7 | Tested with [Racket](https://racket-lang.org/) 8.3.
8 |
9 | Changes from the original code:
10 |
11 | * function names changed to ALL-CAPS to avoid confusion with
12 | functions provided by Racket;
13 | * parameters named `env` renamed to `environ` to avoid confusion with `exp`;
14 | * use of `#t`, `#f` instead of `true`, `false`;
15 | * APPLY rewritten to avoid the need for `primitive-procedure?`;
16 | the corresponding function in Racket is `primitive?` but it is not
17 | useful to us because `(primitive? +)` returns `#f`, so we can't use it
18 | to distinguish user-defined procedures from those predefined by Racket;
19 | * added MAKE-BUILTINS to create an environment with predefined procedures;
20 | * added `concrete-test.rkt` with `rackunit` tests;
21 | * added `(provide ...)` to make `concrete.rkt` functions available for testing.
22 |
--------------------------------------------------------------------------------
/references/sicp/instructors-manual/concrete-test.rkt:
--------------------------------------------------------------------------------
1 | #lang racket/base
2 |
3 | (require rackunit
4 | "concrete.rkt")
5 |
6 | (check-false (ASSQ 'x '()) "Empty bindings")
7 | (check-equal? (ASSQ 'x '([x . 8])) '(x . 8) "First binding")
8 | (check-equal? (ASSQ 'x '([y . 7] [x . 8])) '(x . 8) "Older binding")
9 | (check-equal? (ASSQ 'z '([x . 8])) #f "No binding")
10 |
11 | (check-equal? (MAKE-FRAME '(a) '(1)) '([a . 1]) "Smalest frame")
12 | (check-equal? (EXTEND-ENVIRONMENT '(a b) '(1 2) '()) '(([a . 1] [b . 2])))
13 | (check-equal? (EXTEND-ENVIRONMENT '() '() '()) '(()) )
14 |
15 | (check-equal? (EXTEND-ENVIRONMENT '(a b) '(1 2)
16 | (EXTEND-ENVIRONMENT '(c d) '(3 4) '()))
17 | '(((a . 1) (b . 2))
18 | ((c . 3) (d . 4))))
19 |
20 | (check-exn (regexp "Unbound variable 'x") (lambda ()
21 | (LOOKUP-VARIABLE-VALUE 'x '())))
22 |
23 | (check-equal? (LOOKUP-VARIABLE-VALUE 'x '(([x . 8]))) 8)
24 |
25 | (check-equal? (MAKE-FRAME '(a) '(1)) '([a . 1]) "Smalest frame")
26 |
27 | (check-equal? (EVAL 1 '()) 1)
28 | (check-equal? (EVAL '(QUOTE symbol) '()) 'symbol)
29 | (check-equal? (EVAL '(QUOTE (1 2 3)) '()) '(1 2 3))
30 | (check-equal? (EVAL 'x (EXTEND-ENVIRONMENT '(x) '(8) '())) 8)
31 | (check-equal? (EVAL '(+ 3 4) (MAKE-BUILTINS)) 7)
32 | (check-equal? (EVAL '(+ (* 3 4) (* 5 6)) (MAKE-BUILTINS)) 42)
33 | (check-equal? (EVAL '((LAMBDA (x) (* x x)) 111) (MAKE-BUILTINS)) 12321)
34 | (check-equal? (EVAL '(((LAMBDA (x)
35 | (LAMBDA (y) (+ x y)))
36 | 3)
37 | 4)
38 | (MAKE-BUILTINS)) 7)
39 |
40 | (check-equal? (EVAL '((LAMBDA (x y)
41 | ((LAMBDA (y) (+ x y))
42 | (* x y)))
43 | 3 4)
44 | (MAKE-BUILTINS)) 15)
45 |
46 | (check-equal? (EVAL '((LAMBDA (x) (IF (< x 0) (- 0 x) x)) 2)
47 | (MAKE-BUILTINS)) 2)
48 |
49 | (check-equal? (EVAL '((LAMBDA (x) (IF (< x 0) (- 0 x) x)) -3)
50 | (MAKE-BUILTINS)) 3)
51 |
--------------------------------------------------------------------------------
/references/sicp/instructors-manual/concrete2-test.rkt:
--------------------------------------------------------------------------------
1 | #lang racket/base
2 |
3 | (require rackunit
4 | "concrete2.rkt")
5 |
6 | (check-false (ASSQ 'x '()) "Empty bindings")
7 | (check-equal? (ASSQ 'x '([x . 8])) '(x . 8) "First binding")
8 | (check-equal? (ASSQ 'x '([y . 7] [x . 8])) '(x . 8) "Older binding")
9 | (check-equal? (ASSQ 'z '([x . 8])) #f "No binding")
10 |
11 | (check-equal? (MAKE-FRAME '(a) '(1)) '([a . 1]) "Smalest frame")
12 | (check-equal? (MAKE-FRAME '(x y z) '(21 22 23)) '((x . 21) (y . 22) (z . 23)) "3 bindings")
13 |
14 | (check-equal? (EXTEND-ENVIRONMENT '(a b) '(1 2) '()) '(([a . 1] [b . 2])))
15 | (check-equal? (EXTEND-ENVIRONMENT '() '() '()) '(()) )
16 |
17 | (check-equal? (EXTEND-ENVIRONMENT '(a b) '(1 2)
18 | (EXTEND-ENVIRONMENT '(c d) '(3 4) '()))
19 | '(((a . 1) (b . 2))
20 | ((c . 3) (d . 4))) "Two frames, each with two bindings.")
21 |
22 | (check-exn (regexp "Unbound variable 'x") (lambda ()
23 | (LOOKUP-VARIABLE-VALUE 'x '())))
24 |
25 | (check-equal? (LOOKUP-VARIABLE-VALUE 'x '(([x . 8]))) 8)
26 |
27 | (check-equal? (EVAL 1 '()) 1)
28 | (check-equal? (EVAL '(QUOTE symbol) '()) 'symbol)
29 | (check-equal? (EVAL '(QUOTE (1 2 3)) '()) '(1 2 3))
30 | (check-equal? (EVAL 'x (EXTEND-ENVIRONMENT '(x) '(8) '())) 8)
31 | (check-equal? (EVAL '(+ 3 4) (MAKE-BUILTINS)) 7)
32 |
33 | ; WIP: replace MAKE-BUILTINS with MAKE-BUILTIN-ENVIRONMENT
34 | (check-equal? (car (MAKE-BUILTIN-ENVIRONMENT)) '(+ . +))
35 | (check-equal? (caar (MAKE-BUILTIN-ENVIRONMENT)) '+)
36 | ;(check-equal? (EVAL '(+ 3 4) (MAKE-BUILTIN-ENVIRONMENT)) 7)
37 |
38 | (check-equal? (EVAL '(+ (* 3 4) (* 5 6)) (MAKE-BUILTINS)) 42)
39 | (check-equal? (EVAL '((LAMBDA (x) (* x x)) 111) (MAKE-BUILTINS)) 12321)
40 | (check-equal? (EVAL '(((LAMBDA (x)
41 | (LAMBDA (y) (+ x y)))
42 | 3)
43 | 4)
44 | (MAKE-BUILTINS)) 7)
45 |
46 | (check-equal? (EVAL '((LAMBDA (x y)
47 | ((LAMBDA (y) (+ x y))
48 | (* x y)))
49 | 3 4)
50 | (MAKE-BUILTINS)) 15)
51 |
52 | (check-equal? (EVAL '((LAMBDA (x) (IF (< x 0) (- 0 x) x)) 2)
53 | (MAKE-BUILTINS)) 2)
54 |
55 | (check-equal? (EVAL '((LAMBDA (x) (IF (< x 0) (- 0 x) x)) -3)
56 | (MAKE-BUILTINS)) 3)
57 |
--------------------------------------------------------------------------------
/references/sicp/instructors-manual/scans/01-eval.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fluentpython/lispy/0c9eb43e60700b48d2a5ac046624bb7e692a9774/references/sicp/instructors-manual/scans/01-eval.png
--------------------------------------------------------------------------------
/references/sicp/instructors-manual/scans/02-apply.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fluentpython/lispy/0c9eb43e60700b48d2a5ac046624bb7e692a9774/references/sicp/instructors-manual/scans/02-apply.png
--------------------------------------------------------------------------------
/references/sicp/instructors-manual/scans/03-eval-if.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fluentpython/lispy/0c9eb43e60700b48d2a5ac046624bb7e692a9774/references/sicp/instructors-manual/scans/03-eval-if.png
--------------------------------------------------------------------------------
/references/sicp/instructors-manual/scans/04-environment.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fluentpython/lispy/0c9eb43e60700b48d2a5ac046624bb7e692a9774/references/sicp/instructors-manual/scans/04-environment.png
--------------------------------------------------------------------------------
/references/sicp/load-eceval-compiler.scm:
--------------------------------------------------------------------------------
1 | ;;;; LOADS THE EXPLICIT-CONTROL EVALUATOR FROM SECTION 5.4 OF
2 | ;;;; STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS, WITH
3 | ;;;; ALL THE SUPPORTING CODE IT NEEDS IN ORDER TO RUN.
4 |
5 | ;;;;This is like load-eceval.scm except that it loads the version
6 | ;;;; of eceval that interfaces with compiled code
7 | ;;;;It doesn't load the compiler itself -- loading the compiler is up to you.
8 |
9 | ;;;; **NB** The actual "load" calls are implementation dependent.
10 |
11 | (load "ch5-regsim") ;reg machine simulator
12 |
13 | ;; **NB** next file contains another "load"
14 | (load "ch5-eceval-support") ;simulation of machine operations
15 |
16 | ;;**NB** eceval-compiler *must* be loaded after eceval-support,
17 | ;; so that the version of user-print in eceval-compiler will override
18 | ;; the version in eceval-support
19 | (load "ch5-eceval-compiler") ;eceval itself
20 | ;and interface to compiled code
--------------------------------------------------------------------------------
/references/sicp/load-eceval.scm:
--------------------------------------------------------------------------------
1 | ;;;; LOADS THE EXPLICIT-CONTROL EVALUATOR FROM SECTION 5.4 OF
2 | ;;;; STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS, WITH
3 | ;;;; ALL THE SUPPORTING CODE IT NEEDS IN ORDER TO RUN.
4 |
5 | ;;;; **NB** The actual "load" calls are implementation dependent.
6 |
7 | (load "ch5-regsim") ;reg machine simulator
8 |
9 | ;; **NB** next file contains another "load"
10 | (load "ch5-eceval-support") ;simulation of machine operations
11 |
12 | (load "ch5-eceval") ;eceval itself
--------------------------------------------------------------------------------
/workshops/europython2022/CONTRIBUTORS.md:
--------------------------------------------------------------------------------
1 | # Contributors
2 |
3 | ## Authors
4 |
5 | * Peter Norvig (@norvig): wrote the original `lis.py` and `lispy.py`.
6 | * Luciano Ramalho (@ramalho): wrote `norvigs-lispy.ipynb` and the `mylis` forks.
7 |
8 | ## Facilitators
9 |
10 | * Geraldo Castro (@exageraldo): wrote `CONTRIBUTING.md`.
11 |
12 | ## Translators to PT-BR
13 |
14 | * Pablo Aguilar (@thepabloaguilar)
15 | * Geraldo Castro (@exageraldo)
16 | * Rafa Villalta (@rafavillalta)
17 | * Luciano Ramalho (@ramalho)
18 |
--------------------------------------------------------------------------------
/workshops/europython2022/LICENSE:
--------------------------------------------------------------------------------
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.
--------------------------------------------------------------------------------
/workshops/europython2022/README.md:
--------------------------------------------------------------------------------
1 | # Learning with lis.py
2 |
3 | > **TIP:** Click this button
4 | [](https://mybinder.org/v2/gh/fluentpython/lispy/HEAD?labpath=workshops%2Feuropython2022%2Fnorvigs-lispy.ipynb)
5 | to launch Jupyter Notebook on the cloud with Binder.
6 |
7 |
8 | This repo holds variations of the
9 | [lis.py interpreter](https://github.com/norvig/pytudes/blob/c33cd6835a506a57d9fe73e3a8317d49babb13e8/py/lis.py)
10 | for a subset of
11 | [Scheme](https://en.wikipedia.org/wiki/Scheme_(programming_language))
12 | created by Peter Norvig of Stanford University, described in his post
13 | [(How to Write a (Lisp) Interpreter (in Python))](https://norvig.com/lispy.html).
14 |
15 | ## Provenance and License
16 |
17 | `lis.py` was
18 | [published](https://github.com/norvig/pytudes/blob/c33cd6835a506a57d9fe73e3a8317d49babb13e8/py/lis.py)
19 | in the repo [`norvig/pytudes`](https://github.com/norvig/pytudes) on Github.
20 | The copyright owner is Peter Norvig.
21 | The code is licensed under the
22 | [MIT license](https://github.com/norvig/pytudes/blob/60168bce8cdfacf57c92a5b2979f0b2e95367753/LICENSE).
23 |
24 | Unless otherwise noted, I wrote the changes to Norvig's code.
25 | For details, see the README files in the directories.
26 |
27 | *Luciano Ramalho*
28 | Dublin, July 12, 2022
29 |
--------------------------------------------------------------------------------
/workshops/europython2022/ast.txt:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | '+'
5 | │
6 | [ ]
7 |
8 | (+)
9 |
10 |
11 | '+' 1 2 3 4
12 | └──┴──┼──┴──┘
13 | │
14 | [ ]
15 |
16 | (+ 1 2 3 4)
17 |
18 |
19 | '/' 9 5
20 | └───┼───┘
21 | │
22 | '*' [ ] 'c'
23 | └───┼───┘
24 | │
25 | '+' 32 [ ]
26 | └────┼────┘
27 | │
28 | [ ]
29 |
30 | (+ 32 (* (/ 9 5) c))
31 |
32 |
33 |
34 | / 'a' 'b'
35 | └───┼───┘
36 | │
37 | 'a' 'b' '*' [ ] 100
38 | └─┬─┘ └───┼───┘
39 | │ │
40 | 'lambda' [ ] [ ]
41 | └─────────┼──────────┘
42 | │
43 | [ ]
44 |
45 |
46 | (lambda (a b) (* (/ a b) 100))
47 |
48 |
49 |
50 |
51 | '<' 'x' 0
52 | └───┼───┘
53 | │
54 | 'if' [ ] 0 'x'
55 | └────┴──┬──┴────┘
56 | │
57 | [ ]
58 |
59 |
60 |
61 | (if (< x 0) 0 x)
62 |
63 |
64 | '<' 'x' 0 '-' 'v'
65 | └───┼───┘ └─┬─┘
66 | │ │
67 | 'if' [ ] [ ] 'v'
68 | └──────┴────┬────┴──────┘
69 | │
70 | [ ]
71 |
72 |
73 |
74 | (if (< x 0) (- v) v)
75 |
76 |
--------------------------------------------------------------------------------
/workshops/europython2022/babilonian-method.scm:
--------------------------------------------------------------------------------
1 | (define raiz2 (lambda (x)
2 | (raiz2-iter 1.0 x)))
3 |
4 | (define raiz2-iter (lambda (chute x)
5 | (if (próximo? chute x)
6 | chute
7 | (raiz2-iter (melhorar chute x) x))))
8 |
9 | (define próximo? (lambda (chute x)
10 | (< (abs (- (* chute chute) x)) 0.001)))
11 |
12 | (define melhorar (lambda (chute x)
13 | (média chute (/ x chute))))
14 |
15 | (define média (lambda (x y)
16 | (/ (+ x y) 2)))
17 |
18 | (display (raiz2 12345654321))
19 |
--------------------------------------------------------------------------------
/workshops/europython2022/lispy.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fluentpython/lispy/0c9eb43e60700b48d2a5ac046624bb7e692a9774/workshops/europython2022/lispy.png
--------------------------------------------------------------------------------
/workshops/europython2022/preview.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | jupytext --to notebook norvigs-lispy.py
3 | jupyter notebook norvigs-lispy.ipynb
4 |
--------------------------------------------------------------------------------
/workshops/europython2022/requirements.txt:
--------------------------------------------------------------------------------
1 | argcomplete==1.12.3
2 | argon2-cffi==21.1.0
3 | attrs==21.2.0
4 | backcall==0.2.0
5 | bleach==4.1.0
6 | cffi==1.14.6
7 | debugpy==1.5.0
8 | decorator==5.1.0
9 | defusedxml==0.7.1
10 | entrypoints==0.3
11 | importlib-metadata==4.8.1
12 | ipykernel==6.4.1
13 | ipython==7.28.0
14 | ipython-genutils==0.2.0
15 | ipywidgets==7.6.5
16 | jedi==0.18.0
17 | Jinja2==3.0.2
18 | jsonschema==4.0.1
19 | jupyter==1.0.0
20 | jupyter-client==7.0.6
21 | jupyter-console==6.4.0
22 | jupyter-core==4.8.1
23 | jupyterlab-pygments==0.1.2
24 | jupyterlab-widgets==1.0.2
25 | MarkupSafe==2.0.1
26 | matplotlib-inline==0.1.3
27 | mistune==0.8.4
28 | nbclient==0.5.4
29 | nbconvert==6.2.0
30 | nbformat==5.1.3
31 | nest-asyncio==1.5.1
32 | notebook==6.4.4
33 | packaging==21.0
34 | pandocfilters==1.5.0
35 | parso==0.8.2
36 | pexpect==4.8.0
37 | pickleshare==0.7.5
38 | prometheus-client==0.11.0
39 | prompt-toolkit==3.0.20
40 | ptyprocess==0.7.0
41 | pycparser==2.20
42 | Pygments==2.10.0
43 | pyparsing==2.4.7
44 | pyrsistent==0.18.0
45 | python-dateutil==2.8.2
46 | pyzmq==22.3.0
47 | qtconsole==5.1.1
48 | QtPy==1.11.2
49 | Send2Trash==1.8.0
50 | six==1.16.0
51 | terminado==0.12.1
52 | testpath==0.5.0
53 | tornado==6.1
54 | traitlets==5.1.0
55 | typing-extensions==3.10.0.2
56 | wcwidth==0.2.5
57 | webencodings==0.5.1
58 | widgetsnbextension==3.5.1
59 | zipp==3.6.0
60 |
--------------------------------------------------------------------------------
/workshops/pythonbrasil2021/CONTRIBUTORS.md:
--------------------------------------------------------------------------------
1 | # Contributors
2 |
3 | ## Authors
4 |
5 | * Peter Norvig (@norvig): wrote the original `lis.py` and `lispy.py`.
6 | * Luciano Ramalho (@ramalho): wrote `norvigs-lispy.ipynb` and the `mylis` forks.
7 |
8 | ## Facilitators
9 |
10 | * Geraldo Castro (@exageraldo): wrote `CONTRIBUTING.md`.
11 |
12 | ## Translators to PT-BR
13 |
14 | * Pablo Aguilar (@thepabloaguilar)
15 | * Geraldo Castro (@exageraldo)
16 | * Rafa Villalta (@rafavillalta)
17 | * Luciano Ramalho (@ramalho)
18 |
--------------------------------------------------------------------------------
/workshops/pythonbrasil2021/GLOSSÁRIO.md:
--------------------------------------------------------------------------------
1 | # GLOSSÁRIO
2 |
3 | Termos do domínio de _linguagens de programação_ adotados nesta oficina.
4 |
5 |
6 | **AST**: Abstract Syntax Tree (Árvore Sintática Abstrata); estrutura de dados que representa o programa
7 | de uma forma conveniente para o trabalho do _interpretador_ ou _compilador_.
8 | Em Python uma AST pode ser construída com listas, dicionários, e/ou objetos aninhados.
9 |
10 | **compilador**: programa que lê código-fonte e gera códigos binários para processadores específicos,
11 | na forma de binários executáveis ou bibliotecas.
12 | Compiladores usam _AST_ como representação do programa;
13 | a construção do binário é guiada pela AST.
14 | Ver: _interpretador_.
15 |
16 | **infixo**: símbolo que aparece entre duas expressões, por exemplo operadores aritméticos infixos: `a + b`.
17 | Ver: _prefixo_.
18 |
19 | **interpretador**: programa que lê código-fonte e o executa em memória,
20 | sem gerar um arquivo binário executável.
21 | Alguns interpretadores executam o programa lendo a AST,
22 | outros usam a AST para gerar _bytecode_ para uma _máquina virtual_.
23 | Ver: _compilador_.
24 |
25 | **parser**: função que recebe o código-fonte e devolve uma _AST_ formada por objetos da _linguagem hospedeira_.
26 | Ex: `parse('(* 2 pi)')` resulta em `['*', 2, 'pi']`.
27 | Traduções: _analisador sintático_ ou _analisador_.
28 |
29 | **prefixo**: símbolo que antecede uma ou mais expressões, por exemplo o operador aritmético prefixo
30 | `+ a b c d` devolve a soma dos quatro valores.
31 | Em Scheme, expressões prefixas são delimitadas por parêntesis `(+ a b c d)`. Ver: _infixo_.
32 |
33 | **prompt**: sinal gráfico que um programa exibe para indicar que está esperando uma entrada.
34 | Por exemplo, o prompt padrão do Python é `>>>`.
35 |
36 | **REPL**: REPL ou Read-Eval-Print-Loop é um programa interativo quem exibe um _prompt_
37 | onde digitamos comandos ou expressões e o programa interpreta o que digitamos,
38 | mostra o resultado, e volta para o estado inicial exibindo o prompt e esperando uma nova instrução do usuário.
39 |
40 | **varíadico**: um parâmetro de função que aceita zero ou mais argumentos.
41 | Ex.: `def soma(*p): return sum(p)` cria uma função com o parâmetro variádico `p`, podendo ser usada como `soma()`, `soma(1, 2)`, ou `soma(1, 2, 3, 4)`, ou `soma(*range(1_000_000))`.
42 | Em Scheme, alguns operadores como `+`, `*`, `=`, `<=` são variádicos. Por exemplo, `(+ 1 2 3 4)` devolve o valor `10`.
43 |
--------------------------------------------------------------------------------
/workshops/pythonbrasil2021/LICENSE:
--------------------------------------------------------------------------------
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.
--------------------------------------------------------------------------------
/workshops/pythonbrasil2021/README.md:
--------------------------------------------------------------------------------
1 | # Aprendendo com lis.py
2 |
3 | > **DICA:** Clique neste botão
4 | [](https://mybinder.org/v2/gh/fluentpython/lispy/HEAD?labpath=workshops%2Fpythonbrasil2021%2Fnorvigs-lispy.ipynb)
5 | para executar o Jupyter Notebook na nuvem através do Binder.
6 |
7 |
8 | Este repositório contém variações do
9 | [interpretador lis.py](https://github.com/norvig/pytudes/blob/c33cd6835a506a57d9fe73e3a8317d49babb13e8/py/lis.py)
10 | para um subconjunto de
11 | [Scheme](https://en.wikipedia.org/wiki/Scheme_(programming_language))
12 | criado por Peter Norvig, descrito em seu texto
13 | [(How to Write a (Lisp) Interpreter (in Python))](https://norvig.com/lispy.html).
14 |
15 | ## Proveniência, Direitos Autorais e Licença
16 |
17 | `lis.py` foi
18 | [publicado](https://github.com/norvig/pytudes/blob/c33cd6835a506a57d9fe73e3a8317d49babb13e8/py/lis.py)
19 | no repositório [`norvig/pytudes`](https://github.com/norvig/pytudes) no Github.
20 | O detentor do direito autoral é Peter Norvig e o código é licenciado sob a
21 | [licença do MIT](https://github.com/norvig/pytudes/blob/60168bce8cdfacf57c92a5b2979f0b2e95367753/LICENSE).
22 |
23 | A menos que seja observado o contrário,
24 | escrevi as mudanças e adições descritas nos arquivos README em cada diretório.
25 |
26 | *Luciano Ramalho*
27 | São Paulo, 23 de agosto de 2021
28 |
--------------------------------------------------------------------------------
/workshops/pythonbrasil2021/ast.txt:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | '+'
5 | │
6 | [ ]
7 |
8 | (+)
9 |
10 |
11 | '+' 1 2 3 4
12 | └──┴──┼──┴──┘
13 | │
14 | [ ]
15 |
16 | (+ 1 2 3 4)
17 |
18 |
19 | '/' 9 5
20 | └───┼───┘
21 | │
22 | '*' [ ] 'c'
23 | └───┼───┘
24 | │
25 | '+' 32 [ ]
26 | └────┼────┘
27 | │
28 | [ ]
29 |
30 | (+ 32 (* (/ 9 5) c))
31 |
32 |
33 |
34 | / 'a' 'b'
35 | └───┼───┘
36 | │
37 | 'a' 'b' '*' [ ] 100
38 | └─┬─┘ └───┼───┘
39 | │ │
40 | 'lambda' [ ] [ ]
41 | └─────────┼──────────┘
42 | │
43 | [ ]
44 |
45 |
46 | (lambda (a b) (* (/ a b) 100))
47 |
48 |
49 |
50 |
51 | '<' 'x' 0
52 | └───┼───┘
53 | │
54 | 'if' [ ] 0 'x'
55 | └────┴──┬──┴────┘
56 | │
57 | [ ]
58 |
59 |
60 |
61 | (if (< x 0) 0 x)
62 |
63 |
64 | '<' 'x' 0 '-' 'v'
65 | └───┼───┘ └─┬─┘
66 | │ │
67 | 'if' [ ] [ ] 'v'
68 | └──────┴────┬────┴──────┘
69 | │
70 | [ ]
71 |
72 |
73 |
74 | (if (< x 0) (- v) v)
75 |
76 |
--------------------------------------------------------------------------------
/workshops/pythonbrasil2021/babilonian-method.scm:
--------------------------------------------------------------------------------
1 | (define raiz2 (lambda (x)
2 | (raiz2-iter 1.0 x)))
3 |
4 | (define raiz2-iter (lambda (chute x)
5 | (if (próximo? chute x)
6 | chute
7 | (raiz2-iter (melhorar chute x) x))))
8 |
9 | (define próximo? (lambda (chute x)
10 | (< (abs (- (* chute chute) x)) 0.001)))
11 |
12 | (define melhorar (lambda (chute x)
13 | (média chute (/ x chute))))
14 |
15 | (define média (lambda (x y)
16 | (/ (+ x y) 2)))
17 |
18 | (display (raiz2 12345654321))
19 |
--------------------------------------------------------------------------------
/workshops/pythonbrasil2021/lispy.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fluentpython/lispy/0c9eb43e60700b48d2a5ac046624bb7e692a9774/workshops/pythonbrasil2021/lispy.png
--------------------------------------------------------------------------------
/workshops/pythonbrasil2021/preview.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | jupytext --to notebook norvigs-lispy.py
3 | jupyter notebook norvigs-lispy.ipynb
4 |
--------------------------------------------------------------------------------
/workshops/pythonbrasil2021/requirements.txt:
--------------------------------------------------------------------------------
1 | argcomplete==1.12.3
2 | argon2-cffi==21.1.0
3 | attrs==21.2.0
4 | backcall==0.2.0
5 | bleach==4.1.0
6 | cffi==1.14.6
7 | debugpy==1.5.0
8 | decorator==5.1.0
9 | defusedxml==0.7.1
10 | entrypoints==0.3
11 | importlib-metadata==4.8.1
12 | ipykernel==6.4.1
13 | ipython==7.28.0
14 | ipython-genutils==0.2.0
15 | ipywidgets==7.6.5
16 | jedi==0.18.0
17 | Jinja2==3.0.2
18 | jsonschema==4.0.1
19 | jupyter==1.0.0
20 | jupyter-client==7.0.6
21 | jupyter-console==6.4.0
22 | jupyter-core==4.8.1
23 | jupyterlab-pygments==0.1.2
24 | jupyterlab-widgets==1.0.2
25 | MarkupSafe==2.0.1
26 | matplotlib-inline==0.1.3
27 | mistune==0.8.4
28 | nbclient==0.5.4
29 | nbconvert==6.2.0
30 | nbformat==5.1.3
31 | nest-asyncio==1.5.1
32 | notebook==6.4.4
33 | packaging==21.0
34 | pandocfilters==1.5.0
35 | parso==0.8.2
36 | pexpect==4.8.0
37 | pickleshare==0.7.5
38 | prometheus-client==0.11.0
39 | prompt-toolkit==3.0.20
40 | ptyprocess==0.7.0
41 | pycparser==2.20
42 | Pygments==2.10.0
43 | pyparsing==2.4.7
44 | pyrsistent==0.18.0
45 | python-dateutil==2.8.2
46 | pyzmq==22.3.0
47 | qtconsole==5.1.1
48 | QtPy==1.11.2
49 | Send2Trash==1.8.0
50 | six==1.16.0
51 | terminado==0.12.1
52 | testpath==0.5.0
53 | tornado==6.1
54 | traitlets==5.1.0
55 | typing-extensions==3.10.0.2
56 | wcwidth==0.2.5
57 | webencodings==0.5.1
58 | widgetsnbextension==3.5.1
59 | zipp==3.6.0
60 |
--------------------------------------------------------------------------------
/workshops/pythonbrasil2022/CONTRIBUTORS.md:
--------------------------------------------------------------------------------
1 | # Contributors
2 |
3 | ## Authors
4 |
5 | * Peter Norvig (@norvig): wrote the original `lis.py` and `lispy.py`.
6 | * Luciano Ramalho (@ramalho): wrote `norvigs-lispy.ipynb` and the `mylis` forks.
7 |
8 | ## Facilitators
9 |
10 | * Geraldo Castro (@exageraldo): wrote `CONTRIBUTING.md`.
11 |
12 | ## Translators to PT-BR
13 |
14 | * Pablo Aguilar (@thepabloaguilar)
15 | * Geraldo Castro (@exageraldo)
16 | * Rafa Villalta (@rafavillalta)
17 | * Luciano Ramalho (@ramalho)
18 |
--------------------------------------------------------------------------------
/workshops/pythonbrasil2022/GLOSSÁRIO.md:
--------------------------------------------------------------------------------
1 | # GLOSSÁRIO
2 |
3 | Termos do domínio de _linguagens de programação_ adotados nesta oficina.
4 |
5 |
6 | **AST**: Abstract Syntax Tree (Árvore Sintática Abstrata); estrutura de dados que representa o programa
7 | de uma forma conveniente para o trabalho do _interpretador_ ou _compilador_.
8 | Em Python uma AST pode ser construída com listas, dicionários, e/ou objetos aninhados.
9 |
10 | **compilador**: programa que lê código-fonte e gera códigos binários para processadores específicos,
11 | na forma de binários executáveis ou bibliotecas.
12 | Compiladores usam _AST_ como representação do programa;
13 | a construção do binário é guiada pela AST.
14 | Ver: _interpretador_.
15 |
16 | **infixo**: símbolo que aparece entre duas expressões, por exemplo operadores aritméticos infixos: `a + b`.
17 | Ver: _prefixo_.
18 |
19 | **interpretador**: programa que lê código-fonte e o executa em memória,
20 | sem gerar um arquivo binário executável.
21 | Alguns interpretadores executam o programa lendo a AST,
22 | outros usam a AST para gerar _bytecode_ para uma _máquina virtual_.
23 | Ver: _compilador_.
24 |
25 | **parser**: função que recebe o código-fonte e devolve uma _AST_ formada por objetos da _linguagem hospedeira_.
26 | Ex: `parse('(* 2 pi)')` resulta em `['*', 2, 'pi']`.
27 | Traduções: _analisador sintático_ ou _analisador_.
28 |
29 | **prefixo**: símbolo que antecede uma ou mais expressões, por exemplo o operador aritmético prefixo
30 | `+ a b c d` devolve a soma dos quatro valores.
31 | Em Scheme, expressões prefixas são delimitadas por parêntesis `(+ a b c d)`. Ver: _infixo_.
32 |
33 | **prompt**: sinal gráfico que um programa exibe para indicar que está esperando uma entrada.
34 | Por exemplo, o prompt padrão do Python é `>>>`.
35 |
36 | **REPL**: REPL ou Read-Eval-Print-Loop é um programa interativo quem exibe um _prompt_
37 | onde digitamos comandos ou expressões e o programa interpreta o que digitamos,
38 | mostra o resultado, e volta para o estado inicial exibindo o prompt e esperando uma nova instrução do usuário.
39 |
40 | **varíadico**: um parâmetro de função que aceita zero ou mais argumentos.
41 | Ex.: `def soma(*p): return sum(p)` cria uma função com o parâmetro variádico `p`, podendo ser usada como `soma()`, `soma(1, 2)`, ou `soma(1, 2, 3, 4)`, ou `soma(*range(1_000_000))`.
42 | Em Scheme, alguns operadores como `+`, `*`, `=`, `<=` são variádicos. Por exemplo, `(+ 1 2 3 4)` devolve o valor `10`.
43 |
--------------------------------------------------------------------------------
/workshops/pythonbrasil2022/LICENSE:
--------------------------------------------------------------------------------
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.
--------------------------------------------------------------------------------
/workshops/pythonbrasil2022/README.md:
--------------------------------------------------------------------------------
1 | # Aprendendo com lis.py
2 |
3 | > **DICA:** Clique neste botão
4 | [](https://mybinder.org/v2/gh/fluentpython/lispy/HEAD?labpath=workshops%2Fpythonbrasil2022%2Fnorvigs-lispy.ipynb)
5 | para executar o Jupyter Notebook na nuvem através da plataforma [Binder](https://mybinder.readthedocs.io/en/latest/).
6 |
7 |
8 | Este repositório contém variações do
9 | [interpretador lis.py](https://github.com/norvig/pytudes/blob/c33cd6835a506a57d9fe73e3a8317d49babb13e8/py/lis.py)
10 | para um subconjunto de
11 | [Scheme](https://en.wikipedia.org/wiki/Scheme_(programming_language))
12 | criado por Peter Norvig, descrito em seu texto
13 | [(How to Write a (Lisp) Interpreter (in Python))](https://norvig.com/lispy.html).
14 |
15 | ## Proveniência, Direitos Autorais e Licença
16 |
17 | `lis.py` foi
18 | [publicado](https://github.com/norvig/pytudes/blob/c33cd6835a506a57d9fe73e3a8317d49babb13e8/py/lis.py)
19 | no repositório [`norvig/pytudes`](https://github.com/norvig/pytudes) no Github.
20 | O detentor do direito autoral é Peter Norvig e o código é licenciado sob a
21 | [licença do MIT](https://github.com/norvig/pytudes/blob/60168bce8cdfacf57c92a5b2979f0b2e95367753/LICENSE).
22 |
23 | A menos que seja observado o contrário,
24 | escrevi as mudanças e adições descritas nos arquivos README em cada diretório.
25 |
26 | *Luciano Ramalho*
27 | São Paulo, 23 de agosto de 2021
28 |
--------------------------------------------------------------------------------
/workshops/pythonbrasil2022/ast.txt:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | '+'
5 | │
6 | [ ]
7 |
8 | (+)
9 |
10 |
11 | '+' 1 2 3 4
12 | └──┴──┼──┴──┘
13 | │
14 | [ ]
15 |
16 | (+ 1 2 3 4)
17 |
18 |
19 | '/' 9 5
20 | └───┼───┘
21 | │
22 | '*' [ ] 'c'
23 | └───┼───┘
24 | │
25 | '+' 32 [ ]
26 | └────┼────┘
27 | │
28 | [ ]
29 |
30 | (+ 32 (* (/ 9 5) c))
31 |
32 |
33 |
34 | / 'a' 'b'
35 | └───┼───┘
36 | │
37 | 'a' 'b' '*' [ ] 100
38 | └─┬─┘ └───┼───┘
39 | │ │
40 | 'lambda' [ ] [ ]
41 | └─────────┼──────────┘
42 | │
43 | [ ]
44 |
45 |
46 | (lambda (a b) (* (/ a b) 100))
47 |
48 |
49 |
50 |
51 | '<' 'x' 0
52 | └───┼───┘
53 | │
54 | 'if' [ ] 0 'x'
55 | └────┴──┬──┴────┘
56 | │
57 | [ ]
58 |
59 |
60 |
61 | (if (< x 0) 0 x)
62 |
63 |
64 | '<' 'x' 0 '-' 'v'
65 | └───┼───┘ └─┬─┘
66 | │ │
67 | 'if' [ ] [ ] 'v'
68 | └──────┴────┬────┴──────┘
69 | │
70 | [ ]
71 |
72 |
73 |
74 | (if (< x 0) (- v) v)
75 |
76 |
--------------------------------------------------------------------------------
/workshops/pythonbrasil2022/babilonian-method.scm:
--------------------------------------------------------------------------------
1 | (define raiz2 (lambda (x)
2 | (raiz2-iter 1.0 x)))
3 |
4 | (define raiz2-iter (lambda (chute x)
5 | (if (próximo? chute x)
6 | chute
7 | (raiz2-iter (melhorar chute x) x))))
8 |
9 | (define próximo? (lambda (chute x)
10 | (< (abs (- (* chute chute) x)) 0.001)))
11 |
12 | (define melhorar (lambda (chute x)
13 | (média chute (/ x chute))))
14 |
15 | (define média (lambda (x y)
16 | (/ (+ x y) 2)))
17 |
18 | (display (raiz2 12345654321))
19 |
--------------------------------------------------------------------------------
/workshops/pythonbrasil2022/lispy.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fluentpython/lispy/0c9eb43e60700b48d2a5ac046624bb7e692a9774/workshops/pythonbrasil2022/lispy.png
--------------------------------------------------------------------------------
/workshops/pythonbrasil2022/preview.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | jupytext --to notebook norvigs-lispy.py
3 | jupyter notebook norvigs-lispy.ipynb
4 |
--------------------------------------------------------------------------------
/workshops/pythonbrasil2022/requirements.txt:
--------------------------------------------------------------------------------
1 | argcomplete==1.12.3
2 | argon2-cffi==21.1.0
3 | attrs==21.2.0
4 | backcall==0.2.0
5 | bleach==4.1.0
6 | cffi==1.14.6
7 | debugpy==1.5.0
8 | decorator==5.1.0
9 | defusedxml==0.7.1
10 | entrypoints==0.3
11 | importlib-metadata==4.8.1
12 | ipykernel==6.4.1
13 | ipython==7.28.0
14 | ipython-genutils==0.2.0
15 | ipywidgets==7.6.5
16 | jedi==0.18.0
17 | Jinja2==3.0.2
18 | jsonschema==4.0.1
19 | jupyter==1.0.0
20 | jupyter-client==7.0.6
21 | jupyter-console==6.4.0
22 | jupyter-core==4.8.1
23 | jupyterlab-pygments==0.1.2
24 | jupyterlab-widgets==1.0.2
25 | MarkupSafe==2.0.1
26 | matplotlib-inline==0.1.3
27 | mistune==0.8.4
28 | nbclient==0.5.4
29 | nbconvert==6.2.0
30 | nbformat==5.1.3
31 | nest-asyncio==1.5.1
32 | notebook==6.4.4
33 | packaging==21.0
34 | pandocfilters==1.5.0
35 | parso==0.8.2
36 | pexpect==4.8.0
37 | pickleshare==0.7.5
38 | prometheus-client==0.11.0
39 | prompt-toolkit==3.0.20
40 | ptyprocess==0.7.0
41 | pycparser==2.20
42 | Pygments==2.10.0
43 | pyparsing==2.4.7
44 | pyrsistent==0.18.0
45 | python-dateutil==2.8.2
46 | pyzmq==22.3.0
47 | qtconsole==5.1.1
48 | QtPy==1.11.2
49 | Send2Trash==1.8.0
50 | six==1.16.0
51 | terminado==0.12.1
52 | testpath==0.5.0
53 | tornado==6.1
54 | traitlets==5.1.0
55 | typing-extensions==3.10.0.2
56 | wcwidth==0.2.5
57 | webencodings==0.5.1
58 | widgetsnbextension==3.5.1
59 | zipp==3.6.0
60 |
--------------------------------------------------------------------------------
/workshops/pythonbrasil2022/runtime.txt:
--------------------------------------------------------------------------------
1 | python-3.9
2 |
--------------------------------------------------------------------------------
/workshops/pythonsudeste2024/CONTRIBUTORS.md:
--------------------------------------------------------------------------------
1 | # Contributors
2 |
3 | ## Authors
4 |
5 | * Peter Norvig (@norvig): wrote the original `lis.py` and `lispy.py`.
6 | * Luciano Ramalho (@ramalho): wrote `norvigs-lispy.ipynb` and the `mylis` forks.
7 |
8 | ## Facilitators
9 |
10 | * Geraldo Castro (@exageraldo): wrote `CONTRIBUTING.md`.
11 |
12 | ## Translators to PT-BR
13 |
14 | * Pablo Aguilar (@thepabloaguilar)
15 | * Geraldo Castro (@exageraldo)
16 | * Rafa Villalta (@rafavillalta)
17 | * Luciano Ramalho (@ramalho)
18 |
--------------------------------------------------------------------------------
/workshops/pythonsudeste2024/LICENSE:
--------------------------------------------------------------------------------
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.
--------------------------------------------------------------------------------
/workshops/pythonsudeste2024/README.md:
--------------------------------------------------------------------------------
1 | # Aprendendo com lis.py
2 |
3 | > **DICA:** Clique neste botão
4 | [](https://mybinder.org/v2/gh/fluentpython/lispy/HEAD?labpath=workshops%2Fpythonsudeste2024%2Flispyturtle.ipynb)
5 | para executar o Jupyter Notebook na nuvem através da plataforma [Binder](https://mybinder.readthedocs.io/en/latest/).
6 |
7 |
8 | Este repositório contém variações do
9 | [interpretador lis.py](https://github.com/norvig/pytudes/blob/c33cd6835a506a57d9fe73e3a8317d49babb13e8/py/lis.py)
10 | para um subconjunto de
11 | [Scheme](https://en.wikipedia.org/wiki/Scheme_(programming_language))
12 | criado por Peter Norvig, descrito em seu texto
13 | [(How to Write a (Lisp) Interpreter (in Python))](https://norvig.com/lispy.html).
14 |
15 | ## Proveniência, Direitos Autorais e Licença
16 |
17 | `lis.py` foi
18 | [publicado](https://github.com/norvig/pytudes/blob/c33cd6835a506a57d9fe73e3a8317d49babb13e8/py/lis.py)
19 | no repositório [`norvig/pytudes`](https://github.com/norvig/pytudes) no Github.
20 | O detentor do direito autoral é Peter Norvig e o código é licenciado sob a
21 | [licença do MIT](https://github.com/norvig/pytudes/blob/60168bce8cdfacf57c92a5b2979f0b2e95367753/LICENSE).
22 |
23 | A menos que seja observado o contrário,
24 | escrevi as mudanças e adições descritas nos arquivos README em cada diretório.
25 |
26 | *Luciano Ramalho*
27 | São Paulo, 5 de julho de 2024
28 |
--------------------------------------------------------------------------------
/workshops/pythonsudeste2024/ast.txt:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | '+'
5 | │
6 | [ ]
7 |
8 | (+)
9 |
10 |
11 | '+' 1 2 3 4
12 | └──┴──┼──┴──┘
13 | │
14 | [ ]
15 |
16 | (+ 1 2 3 4)
17 |
18 |
19 | '/' 9 5
20 | └───┼───┘
21 | │
22 | '*' [ ] 'c'
23 | └───┼───┘
24 | │
25 | '+' 32 [ ]
26 | └────┼────┘
27 | │
28 | [ ]
29 |
30 | (+ 32 (* (/ 9 5) c))
31 |
32 |
33 |
34 | / 'a' 'b'
35 | └───┼───┘
36 | │
37 | 'a' 'b' '*' [ ] 100
38 | └─┬─┘ └───┼───┘
39 | │ │
40 | 'lambda' [ ] [ ]
41 | └─────────┼──────────┘
42 | │
43 | [ ]
44 |
45 |
46 | (lambda (a b) (* (/ a b) 100))
47 |
48 |
49 |
50 |
51 | '<' 'x' 0
52 | └───┼───┘
53 | │
54 | 'if' [ ] 0 'x'
55 | └────┴──┬──┴────┘
56 | │
57 | [ ]
58 |
59 |
60 |
61 | (if (< x 0) 0 x)
62 |
63 |
64 | '<' 'x' 0 '-' 'v'
65 | └───┼───┘ └─┬─┘
66 | │ │
67 | 'if' [ ] [ ] 'v'
68 | └──────┴────┬────┴──────┘
69 | │
70 | [ ]
71 |
72 |
73 |
74 | (if (< x 0) (- v) v)
75 |
76 |
--------------------------------------------------------------------------------
/workshops/pythonsudeste2024/babilonian-method.scm:
--------------------------------------------------------------------------------
1 | (define raiz2 (lambda (x)
2 | (raiz2-iter 1.0 x)))
3 |
4 | (define raiz2-iter (lambda (chute x)
5 | (if (próximo? chute x)
6 | chute
7 | (raiz2-iter (melhorar chute x) x))))
8 |
9 | (define próximo? (lambda (chute x)
10 | (< (abs (- (* chute chute) x)) 0.001)))
11 |
12 | (define melhorar (lambda (chute x)
13 | (média chute (/ x chute))))
14 |
15 | (define média (lambda (x y)
16 | (/ (+ x y) 2)))
17 |
18 | (display (raiz2 12345654321))
19 |
--------------------------------------------------------------------------------
/workshops/pythonsudeste2024/lispy.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fluentpython/lispy/0c9eb43e60700b48d2a5ac046624bb7e692a9774/workshops/pythonsudeste2024/lispy.png
--------------------------------------------------------------------------------
/workshops/pythonsudeste2024/requirements.txt:
--------------------------------------------------------------------------------
1 | argcomplete==1.12.3
2 | argon2-cffi==21.1.0
3 | attrs==21.2.0
4 | backcall==0.2.0
5 | bleach==4.1.0
6 | cffi==1.14.6
7 | debugpy==1.5.0
8 | decorator==5.1.0
9 | defusedxml==0.7.1
10 | entrypoints==0.3
11 | importlib-metadata==4.8.1
12 | ipykernel==6.4.1
13 | ipython==7.28.0
14 | ipython-genutils==0.2.0
15 | ipywidgets==7.6.5
16 | jedi==0.18.0
17 | Jinja2==3.0.2
18 | jsonschema==4.0.1
19 | jupyter==1.0.0
20 | jupyter-client==7.0.6
21 | jupyter-console==6.4.0
22 | jupyter-core==4.8.1
23 | jupyterlab-pygments==0.1.2
24 | jupyterlab-widgets==1.0.2
25 | MarkupSafe==2.0.1
26 | matplotlib-inline==0.1.3
27 | mistune==0.8.4
28 | nbclient==0.5.4
29 | nbconvert==6.2.0
30 | nbformat==5.1.3
31 | nest-asyncio==1.5.1
32 | notebook==6.4.4
33 | packaging==21.0
34 | pandocfilters==1.5.0
35 | parso==0.8.2
36 | pexpect==4.8.0
37 | pickleshare==0.7.5
38 | prometheus-client==0.11.0
39 | prompt-toolkit==3.0.20
40 | ptyprocess==0.7.0
41 | pycparser==2.20
42 | Pygments==2.10.0
43 | pyparsing==2.4.7
44 | pyrsistent==0.18.0
45 | python-dateutil==2.8.2
46 | pyzmq==22.3.0
47 | qtconsole==5.1.1
48 | QtPy==1.11.2
49 | Send2Trash==1.8.0
50 | six==1.16.0
51 | terminado==0.12.1
52 | testpath==0.5.0
53 | tornado==6.1
54 | traitlets==5.1.0
55 | typing-extensions==3.10.0.2
56 | wcwidth==0.2.5
57 | webencodings==0.5.1
58 | widgetsnbextension==3.5.1
59 | zipp==3.6.0
60 |
--------------------------------------------------------------------------------
/workshops/thoughtworks2021/LICENSE:
--------------------------------------------------------------------------------
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.
--------------------------------------------------------------------------------
/workshops/thoughtworks2021/README.md:
--------------------------------------------------------------------------------
1 | # Norvig's `lis.py`
2 |
3 | Click [](https://mybinder.org/v2/gh/fluentpython/lispy/HEAD?labpath=workshops%2Fthoughtworks2021%2Fnorvigs-lispy.ipynb)
4 | to run Jupyter Notebook at Binder.
5 |
--------------------------------------------------------------------------------
/workshops/thoughtworks2021/cc-by-88x31.png:
--------------------------------------------------------------------------------
1 |
2 | 301 Moved Permanently
3 |
4 | 301 Moved Permanently
5 |
nginx
6 |
7 |
8 |
--------------------------------------------------------------------------------
/workshops/thoughtworks2021/lispy.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fluentpython/lispy/0c9eb43e60700b48d2a5ac046624bb7e692a9774/workshops/thoughtworks2021/lispy.png
--------------------------------------------------------------------------------
/workshops/thoughtworks2021/requirements.txt:
--------------------------------------------------------------------------------
1 | argcomplete==1.12.3
2 | argon2-cffi==21.1.0
3 | attrs==21.2.0
4 | backcall==0.2.0
5 | bleach==4.1.0
6 | cffi==1.14.6
7 | debugpy==1.5.0
8 | decorator==5.1.0
9 | defusedxml==0.7.1
10 | entrypoints==0.3
11 | importlib-metadata==4.8.1
12 | ipykernel==6.4.1
13 | ipython==7.28.0
14 | ipython-genutils==0.2.0
15 | ipywidgets==7.6.5
16 | jedi==0.18.0
17 | Jinja2==3.0.2
18 | jsonschema==4.0.1
19 | jupyter==1.0.0
20 | jupyter-client==7.0.6
21 | jupyter-console==6.4.0
22 | jupyter-core==4.8.1
23 | jupyterlab-pygments==0.1.2
24 | jupyterlab-widgets==1.0.2
25 | MarkupSafe==2.0.1
26 | matplotlib-inline==0.1.3
27 | mistune==0.8.4
28 | nbclient==0.5.4
29 | nbconvert==6.2.0
30 | nbformat==5.1.3
31 | nest-asyncio==1.5.1
32 | notebook==6.4.4
33 | packaging==21.0
34 | pandocfilters==1.5.0
35 | parso==0.8.2
36 | pexpect==4.8.0
37 | pickleshare==0.7.5
38 | prometheus-client==0.11.0
39 | prompt-toolkit==3.0.20
40 | ptyprocess==0.7.0
41 | pycparser==2.20
42 | Pygments==2.10.0
43 | pyparsing==2.4.7
44 | pyrsistent==0.18.0
45 | python-dateutil==2.8.2
46 | pyzmq==22.3.0
47 | qtconsole==5.1.1
48 | QtPy==1.11.2
49 | Send2Trash==1.8.0
50 | six==1.16.0
51 | terminado==0.12.1
52 | testpath==0.5.0
53 | tornado==6.1
54 | traitlets==5.1.0
55 | typing-extensions==3.10.0.2
56 | wcwidth==0.2.5
57 | webencodings==0.5.1
58 | widgetsnbextension==3.5.1
59 | zipp==3.6.0
60 |
--------------------------------------------------------------------------------