Little books in Javascript?
There is a very nice web-based javascript version of
the great SICP textbook.
Can it also be done for
the Little Books?
- The Little Schemer Chapter 1-4 Exercises: Try them in the Sandbox 1.
Pay attention to str2sx(my_string) and sx2str(my_s_expression) in the example code in the Input Box.
They are convenience functions for entering and viewing s-expressions.
If you really want to inspect the Javascript implementation use JSON.stringify(my_s_expression).
- Chapter 5 and 6: Use Sandbox 5 with solutions from Chapters 2–4 included.
- Chapter 7 and 8: Use Sandbox 7 with isEqual from Chapter 5 replacing isEq.
- Chapter 9 and Beyond: Use Sandbox 9 with answers from Chapter 7 and 8 included.
Here are the solutions for Chapter 8 and
Chapter 9 and
Chapter 10.
And of course a little Scheme interpreter, without define, just like in the book.
The Little Javascripter by Douglas Crockford has a table
with transformation rules from Scheme to Javascript. For instance we use null instead of ‘() in Scheme.
And cdr(a, s) instead of (cdr a s).
All primitives (car cdr cons isAtom isEq isNull) from Chapter 1 “Toys” are available in Sandbox 1 so you can do the exercises in Chapter 2 “Do It, Do It Again, and Again, and Again…” and the exercises in Chapter 3 “Cons the Magnificent” and in Chapter 4 “Numbers Games”.
Do not use “or” in Chapter 2, instead use || in Javascript. Same for “and” and &&.
For Chapter 5 “Oh My Gawd: It’s Full of Stars”, you need the solutions from Chapters 1-4, so use Sandbox 5.
Use isEq(a, b) for non-numeric atoms, isEqn(n, m) for numbers, and isEqan(a, b) for any two atoms.
Spoiler alert: Chapter 5 brings isEqual(s1, s2), for any two s-expressions.
All the solutions in Javascript up to Chapter 10 by Douglas Crockford are
here.
Surprises:
To inspect the function code of e.g. car you can type “car;” as the last line in the Input Box.
If you want to try Scheme:
The Little Javascripter by Douglas Crockford
is also hosted here.
You can try Scheme code there in the Input Box.
In Scheme a quote character must be a real quote.
On iPad turn off smart quotes: Go to Settings - General - Keyboard, and toggle off “Smart Punctuation”.
Or use (define my_atom (quote atom)).
Surprises:
(isList ‘()): false.