• Stars
    star
    662
  • Rank 68,103 (Top 2 %)
  • Language
    Scheme
  • Created almost 15 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

All the Scheme code examples from the book "The Little Schemer"
This repository contains all the code examples from the book "The Little
Schemer." The code in this book is presented in a subset version of the Scheme
programming language. The book is a dialogue between you and the authors about
interesting examples of Scheme programs and it teaches you to think
recursively.

If you're interested, get the book from Amazon: http://bit.ly/4GjWdP

The code examples were copied (and completed where necessary) from
"The Little Schemer" by Peteris Krumins ([email protected]).

His blog is at http://www.catonmat.net  --  good coders code, great reuse.

------------------------------------------------------------------------------

Table of contents:
    [01] Chapter  1: Toys
         01-toys.ss
    [02] Chapter  2: Do It, Do It Again, and Again, and Again...
         02-do-it-again.ss
    [03] Chapter  3: Cons the Magnificent
         03-cons-the-magnificent.ss
    [04] Chapter  4: Numbers Games
         04-numbers-games.ss
    [05] Chapter  5: *Oh My Gawd*: It's Full of Stars
         05-full-of-stars.ss
    [06] Chapter  6: Shadows
         06-shadows.ss
    [07] Chapter  7: Shadows
         07-friends-and-relations.ss
    [08] Chapter  8: Lambda the Ultimate
         08-lambda-the-ultimate.ss
    [09] Chapter  9: ... and Again, and Again, and Again ...
         09-and-again.ss
    [10] Chapter 10: What Is the Value of All of This?
         10-value-of-all-of-this.ss


[01]-Chapter-1-Toys-----------------------------------------------------------

See 01-toys.ss file for code examples.

Chapter 1 introduces language primitives, operations and tests on them.

The primitives include: atoms, lists and s-expressions.
Operations include: car, cdr, cons.
Tests include: null?, atom?, eq?.

It also defines five rules on using car, cdr, cons, null? and eq?.

The law of car:   The primitive car is defined only for non-empty lists.
The law of cdr:   The primitive cdr is defined only for non-empty lists.
                  The cdr of any non-empty list is always another list.
The law of cons:  The primitive cons takes two arguments.
                  The second argument to cons must be a list.
                  The result is a list.
The law of null?: The primitive null? is defined only for lists.
The law of eq?:   The primitive eq? takes two arguments.
                  Each must be a non-numeric atom.


.----------------------------------------------------------------------------.
|                                                                            |
|                         This space reserved for                            |
|                              JELLY STAINS!                                 |
|                                                                            |
'----------------------------------------------------------------------------'


[02]-Chapter-2-Do-It-Do-It-Again-and-Again-and-Again--------------------------

See 02-do-it-again.ss file for code examples.

Chapter 2 introduces two recursive functions and steps through them again, and
again, and again until you understand recursion.

The first function introduced is lat? that tests if the given list consists
only of atoms (lat stands for list of atoms).

The second function introduced is member? that tests if an element is in a
lat.

It also defines a preliminary version of the first commandment that always
should be followed when programming recursively.

.----------------------------------------------------------------------------.
| The first commandment: (preliminary version)                               |
|                                                                            |
| Always ask null? as the first question in expressing any function.         |
'----------------------------------------------------------------------------'


[03]-Chapter-3-Cons-the-Magnificent-------------------------------------------

See 03-cons-the-magnificent.ss file for code examples.

Chapter 3 explains how to build lists with cons. It's done via showing how to
write a function that removes an element from the list. Then the second
commandment is presented.

.----------------------------------------------------------------------------.
| The second commandment:                                                    |
|                                                                            |
| Use cons to build lists.                                                   |
'----------------------------------------------------------------------------'

Next, it's precisely explained how to do recursion and when to stop recursing,
this leads to the third commandment and a preliminary version of the fourth
commandment. The examples include a function that inserts an element in a list
to the right and to the left of the given element, and a function that removes
the first occurrence of an element from a list.

.----------------------------------------------------------------------------.
| The third commandment:                                                     |
|                                                                            |
| When building lists, describe the first typical element, and then cons it  |
| onto the natural recursion.                                                |
'----------------------------------------------------------------------------'

Next the multi-versions of the same functions are written that insert element
to the right and to the left of all occurrences of the given element in a list,
and a function that removes all occurrences of an element from a list.

.----------------------------------------------------------------------------.
| The fourth commandment: (preliminary version)                              |
|                                                                            |
| Always change at least one argument while recurring. It must be changed to |
| be closer to termination. The changing argument must be tested in the      |
| termination condition: when using cdr, test the termination with null?.    |
'----------------------------------------------------------------------------'

[04]-Chapter-4-Numbers-Games--------------------------------------------------

See 04-numbers-games.ss file for code examples.

Chapter 4 builds the arithmetic system from the primitives add1 and sub1.

Using add1 the usual + addition operation on two numbers is developed, next
using sub1 the usual - subtraction operation is developed, then multiplication
and exponentiation are written.

Along the way the first and fourth commandments are revisited:

.----------------------------------------------------------------------------.
| The first commandment (first revision)                                     |
|                                                                            |
| When recurring on a list of atoms, lat, ask two questions about it:        |
| (null? lat) and else.                                                      |
| When recurring on a number, n, ask two questions about it: (zero? n) and   |
| else.                                                                      |
'----------------------------------------------------------------------------'

.----------------------------------------------------------------------------.
| The fourth commandment (first revision)                                    |
|                                                                            |
| Always change at least one argument while recurring. It must be changed to |
| be closer to termination. The changing argument must be tested in the      |
| termination condition:                                                     |
| when using cdr, test the termination with null? and                        |
| when using sub1, test termination with zero?.                              |
'----------------------------------------------------------------------------'

And the fifth commandment is postulated:

.----------------------------------------------------------------------------.
| The fifth commandment                                                      |
|                                                                            |
| When building a value with o+, always use 0 for the value of the           |
| terminating line, for adding 0 does not change the value of an addition.   |
|                                                                            |
| When building a value with o*, always use 1 for the value of the           |
| terminating line, for multiplying by 1 does not change the value of a      |
| multiplication.                                                            |
|                                                                            |
| When building a value with cons, always consider () for the value of the   |
| terminating line.                                                          |
'----------------------------------------------------------------------------'

Next the < greater than and > less than operations are derived, then the =
equals operation and quotient operation.

Then various utility functions are written, such as length that determines the
length of a list, pick that picks the n-th element from the list, rempick that
removes the n-th element from the list, no-nums that extracts all non-numeric
elements from the list, all-nums that does the opposite and extracts all
numeric elements from the list.

[05]-Chapter-5-Oh-My-Gawd-It's-Full-of-Stars----------------------------------

See 05-full-of-stars.ss file for code examples.

Chapter 5 introduces you to S-expressions and functions that manipulate them.

The first commandment is finalized:

.----------------------------------------------------------------------------.
| The first commandment (final version)                                      |
|                                                                            |
| When recurring on a list of atoms, lat, ask two questions about it:        |
| (null? lat) and else.                                                      |
| When recurring on a number, n, ask two questions about it: (zero? n) and   |
| else.                                                                      |
| When recurring on a list of S-expressions, l, ask three questions about    |
| it: (null? l), (atom? (car l)), and else.                                  |
'----------------------------------------------------------------------------'

And the fourth commandment is stated:

.----------------------------------------------------------------------------.
| The fourth commandment (final version)                                     |
|                                                                            |
| Always change at least one argument while recurring. When recurring on a   |
| list of atoms, lat, use (cdr l). When recurring on a number, n, use        |
| (sub1 n). And when recurring on a list of S-expressions, l, use (car l)    |
| and (cdr l) if neither (null? l) nor (atom? (car l)) are true.             |
|                                                                            |
| It must be changed to be closer to termination. The changing argument must |
| be tested in the termination condition:                                    |
| * when using cdr, test the termination with null? and                      |
| * when using sub1, test termination with zero?.                            |
'----------------------------------------------------------------------------'

Functions rember, insertR, insertL, occur, subst, member are then rewritten to
manipulate S-expressions and not just lists of atoms.

Then functions for comparing two S-expressions are written, and rewritten
several times to teach you Scheme for great good.

Finally the sixth commandment is presented:

.----------------------------------------------------------------------------.
| The sixth commandment                                                      |
|                                                                            |
| Simplify only after the function is correct.                               |
'----------------------------------------------------------------------------'

[06]-Chapter-6-Shadows--------------------------------------------------------

See 06-shadows.ss file for code examples.

Chapter 6 develops an evaluator for simple arithmetic expressions involving
only +, * and exp.

The seventh commandment is formulated as evaluator is developed:

.----------------------------------------------------------------------------.
| The seventh commandment                                                    |
|                                                                            |
| Recur on the subparts that are of the same nature:                         |
| * On the sublists of a list.                                               |
| * On the subexpressions of an arithmetic expression.                       |
'----------------------------------------------------------------------------'

Next different representations for arithmetic expressions are introduced. An
expression (1 + 2) can be written as (+ 1 2) and (1 2 +) or even (plus 1 2).

The concept of abstraction from representations is introduced. The eighth
commandment follows:

.----------------------------------------------------------------------------.
| The eighth commandment                                                     |
|                                                                            |
| Use help functions to abstract from representations.                       |
'----------------------------------------------------------------------------'

Finally chapter shows how numbers 0, 1, 2, ... can be represented purely as
lists. The number 0 becomes (), number 1 becomes (()), 2 becomes (() ()),
3 becomes (() () ()), ... . Then functions for adding, subtracting and
checking if the new number is zero are written. But beware of shadows, the
lat? function doesn't work on a list of these numbers.


[07]-Chapter-7-Friends-and-Relations------------------------------------------

See 07-friends-and-relations.ss file for code examples.

Chapter 7 is all about sets. It defines functions to test if the given list is
a set, to construct a set from a list, to test if set1 is a subset of set2, to
determine if two sets are equal, to find intersect of two sets, to fund
intersect of a bunch of sets, to find union of two sets.

Then it introduces pairs and writes several helper functions: first that
returns the first element of a pair, second that returns the second element of
a pair, and build that builds a pair. (Remember commandment eight.)

Then it plays with mathematical functions. It introduces relations, creates
functions to reverse relations, and test if the given list of relations is a
function and is it a full function.


[08]-Chapter-8-Lambda-the-Ultimate--------------------------------------------

See 08-lambda-the-ultimate.ss file for code examples.

Chapter 8 introduces the concept that functions can be passed and returned
from functions. It also introduces currying.

Next it reviews several functions from Chapter 3 and shows how they differ
only by two lines. Those lines can be factored out as separate functions that
simplifies the whole thing.

The ninth commandment follows.

.----------------------------------------------------------------------------.
| The ninth commandment                                                      |
|                                                                            |
| Abstract common patterns with a new function.                              |
'----------------------------------------------------------------------------'

Finally continuations are introduced via a bunch of examples, for example,
multirember&co function collects the elements to be removed in one list, and
the elements that were not removed in the other. After it's done, the
collector is called, which is your own function so you may do anything you
wish with those two lists.

The final, tenth commandment, is stated.

.----------------------------------------------------------------------------.
| The tenth commandment                                                      |
|                                                                            |
| Build functions to collect more than one value at a time.                  |
'----------------------------------------------------------------------------'

And remember, an apple a day keeps the doctor away.


[09]-Chapter-9-and-Again-and-Again-and-Again----------------------------------

See 09-and-again.ss file for code examples.

...

Used this chapter to write an article on derivation of Y-Combinator:
http://www.catonmat.net/blog/derivation-of-ycombinator/

...


[10]-Chapter-10-What-Is-the-Value-of-All-of-This------------------------------

See 10-value-of-all-of-this.ss file for code examples.

Chapter 10 implements Scheme in Scheme. That's it.

It was a great adventure and now it's time for banquet!


------------------------------------------------------------------------------

That's it. I hope you find these examples useful when reading "The Little
Schemer" yourself! Go get it at http://bit.ly/4GjWdP, if you haven't already!


Sincerely,
Peteris Krumins
http://www.catonmat.net

More Repositories

1

node-lazy

lazy lists for node.js
JavaScript
490
star
2

nodejs-proxy

A HTTP proxy server written in node.js
JavaScript
416
star
3

node-tree-kill

kill trees of processes
JavaScript
330
star
4

bash-redirections-cheat-sheet

Bash redirections cheat sheet
309
star
5

perl1line.txt

collection of handy perl one-liner scripts
241
star
6

xgoogle

Python library to Google services (google search, google sets, google translate, sponsored links)
Python
217
star
7

node-gif

A node.js C++ module for creating GIF images and animated GIFs from RGB or RGBA buffers.
C++
212
star
8

node-video

A node.js module for streaming and recording HTML5 Theora videos
C++
202
star
9

node-png

A nodejs C++ module that given a buffer with RGB or RGBA values creates a PNG image (in memory).
C
193
star
10

stackvm

Configure, network, and interact with virtual machines entirely over the web
JavaScript
141
star
11

hacker-top

A top-like program for monitoring hacker news from the console
Python
133
star
12

node-iptables

basic iptables control via nodejs
JavaScript
112
star
13

node-image

Unifies node-png, node-jpeg and node-gif (for great good)
JavaScript
99
star
14

the-seasoned-schemer

All the Scheme code examples from the book "The Seasoned Schemer"
Scheme
97
star
15

bash-vi-editing-mode-cheat-sheet

Bash has two input modes - emacs and vi. This is vi input/editing mode keyboard shortcut cheat sheet.
91
star
16

the-little-mler

All the ML code examples from the book "The Little MLer"
OCaml
84
star
17

awk-cheat-sheet

This is AWK programming language cheat sheet.
76
star
18

bithacks.h

bithacks.h is a C header file containing useful bit manipulation macros
C
73
star
19

node-jpeg

A nodejs C++ module that given a buffer with RGB or RGBA values creates a JPEG image in memory.
C++
65
star
20

node-base64

A base64 encoding and decoding C++ module for node.js that actually works! (node now has it's own base64 encoding, see docs!)
C
64
star
21

perl-tcp-proxy

A simple TCP proxy written in Perl. Uses IO::Socket::INET and IO::Select for multiplexing.
Perl
60
star
22

node-jsmin

javascript minimizer for node.js
JavaScript
52
star
23

reddit-top

A top-like program for monitoring reddit from the console
Python
52
star
24

bash-history-cheat-sheet

This is the bash history cheat sheet. It summarizes everything there is to know about working efficiently with command line history in bash.
52
star
25

the-reasoned-schemer

All the logic programming code examples from the book "The Reasoned Schemer"
Scheme
49
star
26

catonmat.net

The new catonmat.net website.
Python
46
star
27

node-browser

Provides a Browser for easy web browsing from node.js
JavaScript
44
star
28

sed-cheat-sheet

This is sed (unix stream editor) cheat sheet.
44
star
29

node-supermarket

A key/value store based on sqlite for node.js that actually works.
JavaScript
43
star
30

screen-cheat-sheet

This is the screen terminal emulator cheat sheet. It lists the default keyboard shortcuts for working with screen.
42
star
31

bash-emacs-editing-mode-cheat-sheet

Bash has two input modes - emacs and vi. This is emacs input/editing mode keyboard shortcut cheat sheet.
33
star
32

set-operations-in-unix-shell

This is an implementation of 14 set operations by using only Unix utilities such as sort, uniq, diff, comm, cat, head, tail, awk, and others.
32
star
33

social-scraper

Social scraper is a Perl program that scrapes reddit, digg, stumbleupon, delicious, furl, flickr, simpy, boingboing, wired for content that matches the given patterns.
Perl
29
star
34

node-async

An example async library for node.js
C++
27
star
35

bash-one-liners

Bash one-liners
26
star
36

perl-tcp-proxy2

Program for my "A TCP Proxy in Perl" article
Perl
24
star
37

the-little-prover

All code examples from "The Little Prover" book
Scheme
22
star
38

adns

Asynchronous DNS resultion in Python by using adns C library.
21
star
39

busy-beaver

Implementation of a Turing Machine that runs the Busy Beaver programs.
C++
19
star
40

social-submitter

Submit your stories to Reddit, Hacker News, Twitter, Plurk, Identi.ca, Facebook at once!
JavaScript
19
star
41

youtube-uploader

A Perl program that uploads videos to YouTube without any APIs.
Perl
18
star
42

invoice

generate pdf invoices from latex via pdflatex
JavaScript
17
star
43

node-passwd

Node.js module to manage /etc/passwd
JavaScript
17
star
44

gnu-awk-youtube-downloader

A program written in GNU Awk that downloads YouTube Videos. Proof of concept that AWK can do binary IO and networking effectively.
17
star
45

ed-cheat-sheet

This is ed (the unix text editor) cheat sheet. It lists all the commands and how to do line addressing.
16
star
46

gnu-coreutils-cheat-sheet

Gnu Coreutils Cheat Sheet
15
star
47

load-status-server

Windows load status server that returns cpu load, memory usage and disk usage through JSON
C++
15
star
48

util-linux-cheat-sheet

Util-Linux Cheat Sheet
15
star
49

node-chess

Node chess - Node.js knockout competition
JavaScript
12
star
50

ssh-key-manager

manage ssh keys on the server side (can be used with ssh-key-widget)
JavaScript
12
star
51

node-des

A C++ node.js module that does DES encryption and actually works (node's crypto module didn't work.)
C++
12
star
52

node-rfb

implements the client-side of the rfb protocol that vnc uses
JavaScript
12
star
53

perl-youtube-downloader-one-liner

This is a Perl one-liner that downloads YouTube videos.
12
star
54

node-bufferdiff

A C++ module for node-js to test if two buffers are equal, fast (could add diff later).
C++
12
star
55

vbscript-youtube-downloader

A program written in VBScript that downloads YouTube videos.
Visual Basic
12
star
56

supermarket-cart

Connect session store using supermarket
JavaScript
11
star
57

keyboard

provides english keyboard (used as a widget for browserling)
JavaScript
11
star
58

speak-text-files-to-wav

Speaks text files to wav using Microsoft Speech API
C++
10
star
59

perl-predefined-variable-cheat-sheet

This is Perl special variable (predefined variable) cheat sheet. It lists all the Perl variables.
10
star
60

dnode

Simple asynchronous remote method invocation for node.js
JavaScript
10
star
61

node-time

This module provides some time functions for node.js (i forgot about Date() so this module is useless)
C++
8
star
62

reddit-comment-finder

A program that finds all the comments a given reddit user has made.
Python
8
star
63

dotfiles

Vim Script
8
star
64

bitly

shorten urls with bitly without api
Perl
7
star
65

node-quine

A node.js module that exports a function that prints itself
7
star
66

firefox-update-dialog-killer

a simple win32 program that detects and closes the firefox update dialog
C++
6
star
67

node-number-range

number ranges
JavaScript
6
star
68

node-png-sync

sync part of node-png that works on windows with node-gyp on node 0.6
C
6
star
69

youtube-video-downloader-in-perl

Wrote this real quick as I needed to get some vids
Perl
6
star
70

chrome-dialog-killer

Kills the nasty "your profile can not be used because it is from a newer version of Google Chrome" dialog by clicking OK button
C++
5
star
71

hwnd-finder

find hwnds easily
C++
5
star
72

rfb-protocols

A node.js module for various RFB encoding (RRE, Hextile, Tight, etc.) conversion to RGB buffers.
C++
5
star
73

TextToWav

Converts text files to wav using Loquendo text-to-speech sdk
C
5
star
74

perl-pack-unpack-printf-sprintf-cheat-sheet

This is Perl pack/unpack template parameter and printf/sprintf format specifier cheat sheet
4
star
75

quick-cd

Quick cd is a utility for bash that keeps track of your most often used directories and allows you to cd to them with as few keystrokes as possible.
4
star
76

feedburner-graph-generator

Current feedburner graphs suck. I wrote this Perl program to generate the nice graphs they used to have in 2008.
4
star
77

install-computer

Shell
3
star
78

plurk-command-line-plurker

A Perl program for plurking from command line
Perl
3
star
79

node-bufferlist

Create linked lists of Buffer objects
JavaScript
3
star
80

webdev-template

Web development template with reset.css
3
star
81

reddit-river

The old reddit river website for mobile devices
3
star
82

http-async-retry

HTTP::Async with retry
Perl
2
star
83

quick-history

Better interface to bash's (and other shell) CTRL+R for reverse-search-history
2
star
84

dockerfiles

Dockerfile
2
star
85

node-parse-users-exe-output

parses output from users.exe on windows
JavaScript
2
star
86

rackspace-tools

some tools to manage rackspace cloud servers
JavaScript
2
star
87

codinghorror-keyword-analyzer

A Perl program that parses public statcounter data for codinghorror.com blog and stores the search keywords in an SQLite database.
2
star
88

node-rdesktop

Client side of the RDP protocol that Windows uses for remote deskop
JavaScript
2
star
89

node-crashing-async-buf

for ry - a crashing Buffer::New example in eio_custom.
C++
2
star
90

stacked-linux

A small linux distribution for use on routers
2
star
91

utf8-length

return the number of bytes in a unicode string
JavaScript
2
star
92

lulzbot

An IRC bot for node.js
JavaScript
2
star
93

latex2html

convert latex to html (works for me)
Perl
2
star
94

utf8-bytes

return an array of bytes from a unicode string
JavaScript
2
star
95

lwp-protocol-http-socketeer

http protocol implementation for LWP that uses a proxy
Perl
1
star
96

startupsupper.github.com

Recipes for Bootstrappers & Hungry Hackers
JavaScript
1
star
97

testling-ci-badge-checker

This perl program checks github repos for testling-ci badges
Perl
1
star
98

testling-ci-test-example

testling-ci-test-example
JavaScript
1
star
99

picurls.com

This is repository of picurls.com website. picurls: picture buzz! buzziest pictures on the net!
1
star
100

digpicz

The old digpicz.com website
1
star