Read This! | Picolisp | Picolisp Machine | Pil Sources | Linux | BASH | C-Programmming | Javascipt | Python | Racket | Scheme | Operating Systems | AssemblyLanguage | Computer Security | Firewalls | Exploitation | Social Engineering | Metasploit | Emacs | vim | Pharo Smalltalk | Databases
Scheme
I am partial to scheme, I always really appreciated it.
(define (max-of-list lst)
(foldr max (first lst) (rest lst)))
That little function says why. It finds the largest number in a list by folding max across it, in one line, with no loop counter and no temporary variable. That is Scheme’s whole character: small, sharp ideas that combine.
What it is, in plain words
Here is the truth about Scheme: it is the Lisp that was designed by subtraction. Guy Steele and Gerald Sussman created it at MIT in 1975 to understand the actor model, and found that a tiny Lisp with lexical scope and first-class functions already did everything they needed.
Scheme gives you:
- Lexical scope and closures – a function remembers the variables around where it was defined.
- Proper tail calls – a recursive call in tail position is a jump, so recursion is iteration and never blows the stack.
- First-class continuations (
call/cc) – you can capture “the rest of the computation” as a value, which is how generators, coroutines and exceptions can be built in the language itself. - Hygienic macros (
syntax-rules) – macros that cannot accidentally capture your variable names. - One namespace for functions and variables (a “Lisp-1”), unlike Common Lisp.
The standards are the RnRS reports. R5RS (1998) is tiny and still widely taught. R6RS (2007, see below) is larger and stricter. R7RS-small (2013) is what most implementations target today; R7RS-large is being built piece by piece through SRFIs (Scheme Requests for Implementation).
Why it matters
- It teaches computation itself. SICP, the most famous programming textbook ever written, uses Scheme because the language gets out of the way of the ideas.
- It is everywhere quietly. GNU Guile is the extension language of GNU Guix and GnuCash; Chez Scheme is the engine under Racket; Scheme dialects script GIMP (Script-Fu) and LilyPond.
- Its ideas spread. Closures in JavaScript, tail calls in functional languages, hygienic macros in Rust: all owe something to Scheme.
An everyday analogy: Lego Technic
Scheme is a small box of Lego Technic pieces: beams, pins, gears and axles. There is no pre-moulded car body in the box. But because the pieces connect in every direction, you can build a gearbox, a crane or a car, and you understand exactly how each one works because you built it.
A hands-on example
Install a Scheme on Vikix: xbps-install -S chez-scheme (R6RS, very fast) or guile (R7RS mostly, GNU), or use Racket’s #lang r7rs.
;; Tail recursion: this loop runs in constant stack space
(define (sum-to n)
(let loop ((i 0) (acc 0))
(if (> i n)
acc
(loop (+ i 1) (+ acc i)))))
(sum-to 1000000) ; => 500000500000
;; Closures: a counter that remembers its own state
(define (make-counter)
(let ((n 0))
(lambda () (set! n (+ n 1)) n)))
(define c (make-counter))
(c) (c) ; => 2
;; Higher-order functions
(map (lambda (x) (* x x)) '(1 2 3 4)) ; => (1 4 9 16)
;; The fold from the top of the page, in portable R7RS
(define (max-of-list lst)
(let loop ((best (car lst)) (rest (cdr lst)))
(if (null? rest)
best
(loop (max best (car rest)) (cdr rest)))))
(max-of-list '(3 9 2 7)) ; => 9
Note foldr is a Racket name; portable Scheme gets folds from SRFI 1 (fold, fold-right). The PicoLisp version of the same idea is just (apply max '(3 9 2 7)).
Common misconceptions
- “Scheme is only for teaching.” Chez Scheme is one of the fastest dynamic language implementations around, and Guix is a whole Linux distribution configured in Guile.
- “Recursion is slow and dangerous.” Not with proper tail calls, which the standard requires.
- “Scheme and Common Lisp are the same thing.” They share parentheses. Scheme is minimal, Lisp-1 and standard-driven; Common Lisp is large, Lisp-2 and industrial.
- "
call/ccis just a curiosity." It is how you understand every control structure as a special case of one idea.
Connections
- Racket, Common Lisp, PicoLisp, Lambda Calculus.
- Functional Programming, Haskell, Data Structures and Algorithms.
- Computer Languages – where Scheme sits in the family tree.
R6RS - Revision 6 Scheme Standard
Programming languages should be designed not by piling feature on top of feature, but by removing the weaknesses and restrictions that make additional features appear necessary. Scheme demonstrates that a very small number of rules for forming expressions, with no restrictions on how they are composed, suffice to form a practical and efficient programming language that is flexible enough to support most of the major … Read more.
