A tiny (< 400 lines) Scheme written in Python.
Originally written as a warmup exercise to prepare for this class.
smallscheme
is not a complete R7RS Scheme -- it implements
everything needed to follow along in
SICP
up to page 63. smallscheme
is very lightweight and can be
installed on any Python 3 installation as follows:
pip install smallscheme
Use rlwrap
if you want arrow-key history, line editing, and that sort of thing:
$ rlwrap smallscheme
scheme> (define (fact n) (if (< n 2) n (* n (fact (- n 1)))))
scheme> (fact 50)
30414093201713378043612608166064768844377641568960512000000000000
scheme> ^D
$
See tests.scm for many more examples.
Example:
$ cat fact.scm
(define (fact n)
(if (< n 2)
n
(* n (fact (- n 1)))))
(define f100 (fact 100))
(display f100)
(newline)
$ smallscheme fact.scm
933262154439441526816992388562667004907159682643816214685929638
952175999932299156089414639761565182862536979208272237582511852
10916864000000000000000000000000
$
Implemented so far:
*
+
-
/
<
=
>
car
cdr
cons
display
newline
not
random
remainder
runtime
There are also two simple functions used in the Scheme-language tests: is
and test
; test
currently behaves like a progn
or do
in other lisps, in that it collects multiple forms to be evaluated and returns the result of the last evaluation. is
is basically assert
.
and
cond
define
if
lambda
or
quote
For explanation of these, and of Scheme in general, I recommend reading Structure and Intepretation of Computer Programs, in print or (free!) online.
pip install -r requirements.txt
Python tests are run with pytest
.
Scheme tests are run with ./smallscheme/main.py -t tests.scm
. These
tests are particularly helpful in seeing what's been implemented so
far.
Some code fragments used in tests are copied directly from SICP, which is licensed with the Creative Commons "Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)" license.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.